Closed stephanvierkant closed 7 years ago
setup it like
$container
->get('knp_snappy.pdf')
->setOption('header-html', 'http://www.yahoo.com')
->setOption('footer-html', 'http://www.msn.com')
->generate('http://www.google.fr', '/path/to/the/file.pdf')
;
Is this really the best way to do it? You can't add html to it?
you should put only rendered html by the given url, but not html as is
I don't understand. You can't do setOption('footer-html', 'Header') ?
nope, you should put the url where is your header is locate
this is not working. How do I set a header?
@florinutz result produce error? or pdf without header?
this is buggy, had to switch to tcpdf, I'm out.
Maybe to late?
tested with wkhtmltopdf 0.12.2.1 (with patched qt)
This works without HTML
return new Response(
$this->get('knp_snappy.pdf')->getOutputFromHtml($html,array('orientation'=>'Landscape',
'footer-right'=>utf8_decode('Seite [page] von [topage] - '.date('\ d.m.Y\ H:i')),'footer-left'=>utf8_decode('Klaus MΓΌller')
)), 200, array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="'.$filename.'"'
)
);
$html = $this->renderView('CoreBundle:PdfGenerator:generate.html.twig', array(
'userId' => $userId,
));
$snappy = $this->get('knp_snappy.pdf');
$snappy->setOption('header-html', $this->container->get('templating.helper.assets')->getUrl('bundles/core/pdfHtml/header.html'));
$snappy->setOption('footer-html', $this->container->get('templating.helper.assets')->getUrl('bundles/core/pdfHtml/footer.html'));
return new Response(
$snappy->getOutputFromHtml($html),
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="file.pdf"'
)
);
This is my code, but still I'm not getting headers footers, where I am doing it wrong? Please help!
Would be really helpful to get the footer/header issue solved. I've tried all the above examples but couldn't get running any of them.
The following code works fine in one of my services. The generator
is DI of knp_snappy.pdf
and twig
id DI of templating
:
$footerHtml = $this->twig->render(
'AppBundle:Pdf:_footer.html.twig', [
'bar' => $bar
]);
$this->generator->setOption('footer-html', $footerHtml);
Most important: NEVER FORGET THE DOCTYPE!
Thank for your reply but my code still doesn't work. Here's what I have:
$html = $this->render(
':pdf:pdf_template.html.twig', [
'user' => 'body lorem ipsum',
]);
$footerHtml = $this->render(
':pdf:pdf_footer.html.twig',
'footer' => 'footer lorem ipsum'
]);
$pdf = $this->get('knp_snappy.pdf');
$pdf->setOption('footer-html', $footerHtml);
$pdf->generateFromHtml($html, 'mypath/file'.time().'.pdf');
I've discovered the following:
Method 'setOption' not found in \Knp\Snappy\GeneratorInterface less... Referenced method is not found in subject class.
Thanks in advance!
And Yes I've set the doctype in both templates...
You can try also this way: https://github.com/KnpLabs/KnpSnappyBundle/issues/75 practically it'd recommend that solution
For anyone finding this:
If you want to add a footer, for example from a different Twig Template, make sure that the Twig Template is a full HTML page, including html and body tags, otherwise you'll get an empty pdf.
Example:
$html = $this->renderView('AcmeBundle:Pdf:show.html.twig', array(
//Variables for the template
));
$footer = $this->renderView('AcmeBundle:Pdf:footer.html.twig', array(
//Variables for the template
));
return new Response(
$this->get('knp_snappy.pdf')->getOutputFromHtml($html, array(
'footer-html' => $footer,
)),
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="file.pdf"'
)
);
Footer:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
Footer Content
</body>
</html>
Ok, I tried setOption('footer-html', $url) with a url and with html itself. Both failed, one yeilding a white page, another a failed pdf
finally @Koalabaerchen 's method worked when I used getOutputFromHtml($html, array( 'footer-html' => $footer_html, )),
with a full html page doctype and everything as $footer_html
Thanks.
In addition to all those comments, I'd like to add that the version of the underlying library has its importance. Yesterday, I tried to add a header and a footer without success eventhough I followed all tips provided here.
After investigations, I found out that the library from my distribution repository did not support header and footer because of the unpatched qt. For information, I have Ubuntu 16.04 and wkhtmltopdf 0.12.2.4-1. I fixed that by installing the latest stable version directly from the official download page.
Hope it helps.
Still doesn't work for with wkhtmltopdf 0.12.3 (with patched qt). I just tried everything described here. Set a doctype, used a full html page but without success.
Any suggestions?
Thanks for your help
This is a nice bundle/library, until you really need to rely on it. Sadly, after almost a full day of tries to create a "nice-formatted" multipage pdf out of some database information, Im skipping the usage of this library in favor to other options. Quick reasons: zero documentation, no control over pages, headers or footers (I know it claims it has). I would only use this library to perform web scraping, but not for enterprise level software development.
For those still faithful π
you have got to use wkhtml compiled against a patched version of qt. The same code works on my mac but it was not working on ubuntu, but doing the right homework on docker we should be able to make it work there as well since it is working on the mac with exactly the same code.
We can close this π
css
.footer {
position: absolute;
right: 0;
bottom: 0;
left: 0;
padding: 1rem;
background-color: #efefef;
text-align: center;
}
html `
<div class="page-break"></div>
`
@pferenc Hard to help based on 2 words and two (incomlete?) code excerpts... If your html is truly what you use as a header/footer, then re-read the other answers here: you need to provide valid html code. By valid I mean with proper doctype, html, head and body tags. If you don't met those requirements it'll fail.
Also, I could confirm cordoval's answer: headers and footers are not managed by version of whtmltopdf not compiled with patched Qt. See here.
Note that since this commit two years ago, we can directly provide html content to header-html
/footer-html
: Snappy will detect if it's a valid path and put it in a temp file if it is not.
I close this issue since there's nothing pointing out that Snappy is failing to do its job. But I'll add a note about patched vs unpatched Qt and required tags.
for what is worth, i moved to microservice on node to print, it is actually pretty decent π way faster than this, just need to sort more fonts
For anyone finding this:
If you want to add a footer, for example from a different Twig Template, make sure that the Twig Template is a full HTML page, including html and body tags, otherwise you'll get an empty pdf.
Example:
$html = $this->renderView('AcmeBundle:Pdf:show.html.twig', array( //Variables for the template )); $footer = $this->renderView('AcmeBundle:Pdf:footer.html.twig', array( //Variables for the template )); return new Response( $this->get('knp_snappy.pdf')->getOutputFromHtml($html, array( 'footer-html' => $footer, )), 200, array( 'Content-Type' => 'application/pdf', 'Content-Disposition' => 'attachment; filename="file.pdf"' ) );
Footer:
<!doctype html> <html> <head> <meta charset="UTF-8"> </head> <body> Footer Content </body> </html>
The right solution. π π―
Hi, for me <!doctype html> helped to show footer on my local on mac, but not on the ubunu sever. I did "wkhtmltopdf --version", for local it was "wkhtmltopdf 0.12.4 (with patched qt)", for the server just "wkhtmltopdf 0.12.4". After wkhtmltopdf reinstallation on the server it showed "wkhtmltopdf 0.12.4 (with patched qt)" and footer appeared.
Hello All, I have this version wkhtmltopdf 0.12.5 (with patched qt).
It's my code:
$enTete = $this->render('@FacturationFacture/Facture/format/en_tete.html.twig', [ 'societe'=>$infosSociete, 'logo_url' => $logo_url ]);
$piedPage = $this->renderView('@FacturationFacture/Facture/format/pied_page.html.twig', [
'societe'=>$infosSociete]);
$snappyPDF = $this->get('knp_snappy.pdf');
return new Response(
$snappyPDF->getOutputFromHtml($body, [
'footer-html' => $piedPage,
'header-html' => $enTete
]),
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="facture-' . $facture->getID() . '.pdf"',
'orientation' => 'Landscape'
)
);
I get blank page.
I'm in Mac.
Thanks
For anyone finding this: If you want to add a footer, for example from a different Twig Template, make sure that the Twig Template is a full HTML page, including html and body tags, otherwise you'll get an empty pdf. Example:
$html = $this->renderView('AcmeBundle:Pdf:show.html.twig', array( //Variables for the template )); $footer = $this->renderView('AcmeBundle:Pdf:footer.html.twig', array( //Variables for the template )); return new Response( $this->get('knp_snappy.pdf')->getOutputFromHtml($html, array( 'footer-html' => $footer, )), 200, array( 'Content-Type' => 'application/pdf', 'Content-Disposition' => 'attachment; filename="file.pdf"' ) );
Footer:
<!doctype html> <html> <head> <meta charset="UTF-8"> </head> <body> Footer Content </body> </html>
The right solution. π π―
Can you please let me know what is $this here as, if I am using SnappyPDF::
or app('snappy.pdf.wrapper')
it is showing get()
is undefined method.
I would like to add a header and footer to all pages, but I can't figure out how.
Is there any example available?