启用生成pdf(它在文件中为我提供计划html文本)-dompdf


$content['profile_data'] = file_get_contents(base64_decode($profile_data,true));
$pdf = PDF::loadHtml(htmlentities($content['profile_data']));
$pdf->setOptions(['setIsHtml5ParserEnabled'=>true]);
// $pdf->setPaper('A4','landscape');
$pdf->stream();
$pdf->save($path . '/' . $file_name.'.pdf');

整个代码看起来还可以。html来自一个url。我正试图将它保存在一个pdf文件中。

但当我尝试打开时,它给了我一个计划文本HTML。请帮我

感谢

不渲染(->render()(。

简称:

$html = ''; // Your html.
$size = 'A4';
$orientation = 'portrait';
$options = new Options(
[
'isHtml5ParserEnabled'    => true, // little faster
]
);
$domPdf = new Dompdf($options);
$domPdf->loadHtml($html);
$domPdf->setPaper($size, $orientation);
$domPdf->render();
$pdf = $domPdf->output();

在这里我发布了我用dompdf做的所有事情-
我搜索了很多,很多边做边学。。。也许它有帮助:

以下是版本v0.8.6

/**
* Returns pdf from html.
*
* @param string $html
* @param string $size
* @param string $orientation
*
* @return string
*/
public function htmlToPdf($html, $size = 'A4', $orientation = 'portrait')
{
$options = new Options(
[
//'logOutputFile'           => 'data/log.htm',
'isPhpEnabled'            => false,
'isRemoteEnabled'         => false,
'isJavascriptEnabled'     => false,
'isHtml5ParserEnabled'    => true, // little faster
'isFontSubsettingEnabled' => false,
'debugPng'                => false,
'debugKeepTemp'           => false,
'debugCss'                => false,
'debugLayout'             => false,
'debugLayoutLines'        => false,
'debugLayoutBlocks'       => false,
'debugLayoutInline'       => false,
'debugLayoutPaddingBox'   => false,
//'pdfBackend'              => 'CPDF',
]
);
$domPdf = new Dompdf($options);
$domPdf->loadHtml($this->minimizeHtml($html));
$domPdf->setPaper($size, $orientation);
$domPdf->render();
return $domPdf->output();
}
/**
* Minimizes the html source.
*
* @see http://stackoverflow.com/a/6225706/3411766
*
* @param string $html
*
* @return string
*/
public function minimizeHtml($html)
{
return preg_replace(
[
'/>[^S ]+/s',  // strip whitespaces after tags, except space
'/[^S ]+</s',  // strip whitespaces before tags, except space
'/(s)+/s'       // shorten multiple whitespace sequences
],
[
'>',
'<',
'\1'
],
$html
);
}

最新更新