Xav*_*ero 2 pdf rendering wkhtmltopdf knp-snappy
随着knp-snappy-bundle
我无法生成的PDF头,而我实际上可以产生一个页脚。
这是一个错误,一个功能,还是我做错了什么?
我正在测试knp-snappy-bundle
,我还wkhtmltopdf
从h4cc
. 这是我的一部分composer.json
:
"h4cc/wkhtmltopdf-amd64": "^0.12.3",
"knplabs/knp-snappy-bundle": "^1.5",
Run Code Online (Sandbox Code Playgroud)
生成的二进制文件是这样wkhtmltopdf
说的:
$ vendor/bin/wkhtmltopdf-amd64 --version
wkhtmltopdf 0.12.3 (with patched qt)
Run Code Online (Sandbox Code Playgroud)
我已经设置了一个使用 的控制器knp-snappy-bundle
并且它可以工作:
这是我的PdfController
:
public function downloadPdfAction( string $docName ) : Response
{
$pdf = $this->getPdf( $docName );
return new PdfResponse( $pdf, $this->getFilename( $docName ) );
}
private function getPdf( string $docName ) : string
{
$html = $this->renderView( 'AppBundle:documents:' . $docName . '.pdf.twig' );
$options = [];
$generator = $this->getPdfGenerator();
$pdf = $generator->getOutputFromHtml( $html, $options );
return $pdf;
}
private function getPdfGenerator() : Pdf
{
$generator = $this->get( 'knp_snappy.pdf' );
return $generator;
}
Run Code Online (Sandbox Code Playgroud)
它基本上:
downloadPdf
动作
'helloWorld'
.Response
使用PdfResponse
包中的类返回使用 PDF 内容创建的新文件。twig
引擎呈现视图。getPdfGenerator()
)。getOutputFromHtml()
而不传入任何选项。这是我的helloWorld.pdf.twig
:
<html>
<body>
<div class="pdfPageBody">
<h1>
Hello, World!
</h1>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是生成的 PDF,完全符合预期:
所以我现在添加页眉和页脚。为此,我只需添加几根树枝,将 HTML 渲染为几个变量,并将它们传递到 Pdf 渲染器的选项中:
private function getPdf( string $docName ) : string
{
$html = $this->renderView( 'AppBundle:documents:' . $docName . '.pdf.twig' );
$header = $this->renderView( 'AppBundle:documents:header.pdf.twig' );
$footer = $this->renderView( 'AppBundle:documents:footer.pdf.twig' );
$options = [
'header-html' => $header,
'footer-html' => $footer,
];
$generator = $this->getPdfGenerator();
$pdf = $generator->getOutputFromHtml( $html, $options );
return $pdf;
}
Run Code Online (Sandbox Code Playgroud)
页眉和页脚彼此相同,除了包含的文本:
这是我的header.pdf.twig
:
<html>
<body>
<div style="border: 5px dashed crimson; color: maroon; background-color: darksalmon">
This is a header
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
和footer.pdf.twig
:
<html>
<body>
<div style="border: 5px dashed crimson; color: maroon; background-color: darksalmon">
This is a footer
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
哇!!!页脚被渲染,但页眉没有!!
这就是我得到的:
图中要注意:
解决了!
好吧,似乎wkhtmltopdf
考虑 doctype 真的很严格,如果不是,它会做一些奇怪的事情。
灵感来源:https : //stackoverflow.com/a/28343079/1315009
所以,我改变了所有的树枝,包括<!DOCTYPE html>
:
<!DOCTYPE html>
<html>
<body>
<div class="pdfPageBody">
<h1>
Hello, World!
</h1>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html>
<body>
<div style="border: 5px dashed crimson; color: maroon; background-color: darksalmon">
This is a header
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html>
<body>
<div style="border: 5px dashed crimson; color: maroon; background-color: darksalmon">
This is a footer
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我终于得到了这个:
它恰好有:
希望有所帮助!