Except when using PrinceXML or PhantomJS (does not support the awesome CSS printing features though), printing a HTML page to a PDF from PHP won’t give really good results. TCPDF creates crazy large files when using the HTML mode.
Thanks @1n3JgKl9pQ6cUMrW and @lukasbestle
I’ve been toying around the idea for a while and I’m trying to figure out the best way to do this.
I think it’s a bit beyond my coding capabilities at the moment, but I’ll try and dig into it!
We built the Coffee Variety Catalog this Summer, and uses print stylesheets extensively.
The PDFs you download here were all “generated” by an administrator by using the standard “Print” dialog in Chrome.
While it doesn’t auto-generate the PDF, there isn’t really much need, given that an editor will be publishing them at particular intervals (and users will only download a particular release), rather than an up-to-date-and-on-the-fly version.
We created a separate “release” template that threaded together content from throughout the site into a single page (which was backed by a blueprint that allowed a bunch of customizations and additional content to be added.
During development, we made extensive use of Chrome’s “Emulate CSS Media” setting (see below).
We hope to open-source this project soon, but will need sign-off from the organization.
Thanks for the example! What I want to achieve is simplier : I just want to print single pages (ex.: a blog post), and not generate a PDF from several pages combined. The PDF is the content of the page, with a layout generated from a stylesheet “styles.css”. If someone has time to make a plugin, I think it can be helpfull for many people!
In the end, I used Prince XML to generate the PDF by hand. It was for a pretty long doc (~60 pages) with content table, page numbers, footnotes and all.
Then ran it through the ‘print PDF’ command on Chrome to take off the watermark. Worked really well but it’s still not ‘automatic’…
I don’t think you need a plugin for this. You can make your print stylesheet apply to print using the media directive in the <link> tag and then add a print button to the page (window.print()). It’s a bit of manual work, but definitely the easiest way to print single pages to PDF.
That’s definitely true. I had a similar use-case before and simply “printing to PDF” was a good solution there (some operating systems like macOS support that by default). Of course that’s not always viable.
Much more complex but doable is using some kind of PHP PDF library like proposed above.
I used TCPDF and its a nightmare. Its fine for things like tickets and one pagers - where you generate the elements directly without HTML conversion. HTML mode is extremely slow and just horrible. mPDF was better similar PHP library. Because TCPDF in html mode sometimes silently errors for strange reasons and does nothing (hard to debug). With mPDF you have some kind of security that the libary generates at least some PDF file or error output.
In the end its not worth it i think. Use print styles where you can.
If you end up going the PHP generator HTML route - be sure to feed it valid html (run it through php tidy or something like that).