2
votes

I'm trying to build invoice pdf with wkhtmltopdf library and a symfony2 wrapper KnpSnappyBundle.

I want to add a footer on every page with the wkhtmltopdf option footer-html. In command-line, everything works fine, my pdf is built and a footer is repeated on every page. But when I use the bundle service :

$snappy = $this->get('knp_snappy.pdf');
$snappy->setOption('footer-html', 'http://www.google.com');
$content = $snappy->getOutputFromHtml($html);

return new Response(
    $content,
    200,
    array(
        'Content-Type'          => 'application/pdf',
        'Content-Disposition'   => 'attachment; filename="file.pdf"'
    )
);

it returns an error :

The exit status code '1' says something went wrong:
stderr: "Loading pages (1/6)
[> ] 0%
[======> ] 10%
Error: Failed loading page http://www.google.com (sometimes it will work just to ignore this error with --load-error-handling ignore)
Exit with code 1 due to network error: HostNotFoundError
"
stdout: ""
command: C:\wkhtmltopdf\bin\wkhtmltopdf.exe --lowquality --dpi "300" --margin-bottom "0" --margin-left "0" --margin-right "0" --margin-top "0" --page-size "A4" --background --encoding "utf-8" --footer-html "http://www.google.com" "http://www.google.com" "test.pdf".

Did any already succeed in using footer-html option with KnpSnappyBundle ?

1
try setOption('footer-html', 'google.com'); and report back please - wkhtmltopdf falls back to http:// protocol by default.Nicolai Fröhlich
the error returned (a bit different) : pastebin.com/kqTE6RjWVaN
did you resolved it?goto
@goto no sorry, I never succeed to make this bundle work as I expected, and I kept using html2pdf. Are you facing the same problem ?VaN

1 Answers

0
votes

I think the problem is with this line:

$snappy->setOption('footer-html', 'http://www.google.com');

The second argument in the setOption() method is looking for a html file on the local filesystem. As it is written currently it would try to include the Google homepage as a footer on every page of your pdf. Try something like this instead:

$snappy->setOption('footer-html', 'path/to/footer.html');
$snappy->getOutput('https://google.com');

Be aware that the .html is important, non-standard extension names like .phtml will cause errors.