With knp-snappy-bundle
I can't generate a header in the PDF, while I can actually generate a footer.
Is it a bug, a feature, or I'm doing something wrong?
I'm testing the knp-snappy-bundle
, I've also installed the wkhtmltopdf
binary from h4cc
. This is part of my composer.json
:
"h4cc/wkhtmltopdf-amd64": "^0.12.3",
"knplabs/knp-snappy-bundle": "^1.5",
The resulting binary of wkhtmltopdf
says this:
$ vendor/bin/wkhtmltopdf-amd64 --version
wkhtmltopdf 0.12.3 (with patched qt)
I've setup a controller that uses the knp-snappy-bundle
and it works:
This is my 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;
}
It basically:
downloadPdf
action that
'helloWorld'
.Response
created with the content of the PDF, using the PdfResponse
class in the bundle.twig
engine.getPdfGenerator()
).getOutputFromHtml()
with no options passed-in.This is my helloWorld.pdf.twig
:
<html>
<body>
<div class="pdfPageBody">
<h1>
Hello, World!
</h1>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</div>
</body>
</html>
And here it is the resulting PDF, exactly as expected:
So I now add the header and footer. To do so, I just add a couple of twigs, render the HTMLs into a couple of variables, and pass them in into the options of the Pdf renderer:
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;
}
The header and footer are identical one to each other, except for the contained text:
This is my header.pdf.twig
:
<html>
<body>
<div style="border: 5px dashed crimson; color: maroon; background-color: darksalmon">
This is a header
</div>
</body>
</html>
And the footer.pdf.twig
:
<html>
<body>
<div style="border: 5px dashed crimson; color: maroon; background-color: darksalmon">
This is a footer
</div>
</body>
</html>
And wow!!! The footer gets rendered but the header does not!!
This is what I get:
To be noted in the image:
Solved!
Well, it seems that wkhtmltopdf
is really strict to consider the doctype and it does weird things if not.
Inspired here: https://stackoverflow.com/a/28343079/1315009
So, I changed all the twigs to include <!DOCTYPE html>
:
<!DOCTYPE html>
<html>
<body>
<div class="pdfPageBody">
<h1>
Hello, World!
</h1>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<div style="border: 5px dashed crimson; color: maroon; background-color: darksalmon">
This is a header
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<div style="border: 5px dashed crimson; color: maroon; background-color: darksalmon">
This is a footer
</div>
</body>
</html>
I finally got this:
It happens to have:
Hope to help!