Here is our answer:
We are currently using wkhtmltopdf to create a PDF file from this html template.
Some background information:
We use Sulu CMF to create our back end, which is based on Symfony2 . The KnpSnappy Bundle is used as the symfony wrapper for wkhtmltopdf.
How we create PDF files:
Since many PDF files have the same header and footer, we created BasePDFBundle, which offers PDFManager for creating PDFs on the fly using this TWIG template. By default, a common header and footer (usually with a customer name and logo) are included.
Footer / page numbers in the footer (or header):
It is very useful to add page numbers to PDF files, for example. for orders, however, most of our content is added dynamically (for example, a list of products). Since the style of the PDF can change, and the content itself is dynamically added, there should be a quick and easy way to add the current and general page to the generated PDF file. Here's what we did:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html> <head> <base href="{{ app.request.schemeAndHttpHost }}" /> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <link rel="stylesheet" href="{{ asset('bundles/pdfbase/css/pdfstyles.css') }}"/> </head> <body class="footer"> <div class="footer-container"> <div class="footer-widget"> <b>FooBar Company Name</b> </div> <div class="text-align-right"> <span class="page"></span>/<span class="topage"></span> </div> </div> <script type="text/javascript"> (function() { </script> </body>
Yes, the page and topage variable names may be better, but they are the same as the KnpSnappy shell used when merging branch templates with the final PDF template. This is the easiest way to get the current and total page number, because you can let the shell do all the calculations.
In the end, you just need to replace the text of the html tags and here it is!
Differences between local machine and server:
Since wkhtmltopdf opens a virtual browser for "rendering" twig templates, this can lead to errors in your pdf generation on your server. We found that using event tags such as <body onload="doSomething()"> is not recommended, most likely you should run your javascript code, as we did in the above example.