If only working with PDFs in Drupal 8 was as easy as handling images in media libraries.
Now, who's with me for a "PDFs in Drupal core" initiative?
No need to "despair", though: if you need to generate and handle PDF files on your Drupal 8 website, you still have a handful of tools (meaning useful PDF Drupal modules), Composer and PDF generating libraries at hand.
In this respect, inspired by an older Drupalcon session on "PDFs in Drupal" and Mediacurrent's synthesizing blog post on this topic, I've decided to put together some sort of compilation of the best available tools.
Those that make handling PDFs in Drupal 8 surprisingly straightforward.
There are 3 heavy-weighting answers to this question, which are also the very reasons why you started reading this post in the first place:
There was a "warning" question in the YouTube video of that DrupalCon session that I've just referred to, that. got me thinking:
"Are you really sure you need to generate PDFs in code?"
You could still rely on modern browsers' built-in Print-to-PDF functionality, you know. A functionality that comes along with full control over the print style sheets.
Do think about that before rushing to create PDFs in code.
You should consider generating PDFs only under the following exceptional circumstances:
That being said, here are some of the most widely-used PDF rendering libraries, ranging from server-side to client-side, to libraries that support the use of PDF files as templates, etc.
But before I jump to "exposing" you the 3 most useful PDF modules in Drupal 8, let me briefly mention their Drupal 7 correspondent, so to say: The Print module, the most popular tool for creating PDFs on Drupal 7 websites.
Now, getting back to Drupal 8, here are the available tools:
Why would you use it? Because it dramatically reduces the overhead of rendering your PDF files.
Built upon the Print module to enable PDF rendering in Drupal 8, it uses the PDF API, which is not yet stable.
Note: it still requires the older version of DOMPDF to. work its magic.
The essential module to use when working with PDFs in Drupal 8!
OK, so we've passed in review the tools at hand that make working with PDFs in Drupal 8 conveniently simple and effective.
But how do you use them precisely? So you can handle your PDF files (more) efficiently.
In short: what cool PDF tricks can you do in Drupal 8?
Here are the 3 most useful PDF tricks that you can pull off in Drupal:
How? By using this combo:
A PDFtk library & a wrapper like mikehaertl/php-pdftk.
And here's how the code supporting this "trick" would look like:
$pdf = new Pdf([ 'A' => '/path/file1.pdf', // A is alias for file1.pdf 'B' => ['/path/file2.pdf','pass**word'], // B is alias for file2.pdf ]); $pdf->send();
This one's plain straightforward. You only need to "unlock" the FillPDF module's power:
$pdf = new Pdf([‘PATH_TO_PDF’]); $pdf->fillForm([ ‘name_of_text_field’ => ‘Some value’ ]) ->needAppearances() ->send(‘filled.pdf’);
Just rely on an Entity Print & DOMPDF or on an Entity Print & Wkhtmltopdf combo for pulling off this PDF "trick".
Now, if it's off an entity that you want to create your PDF file, you can always. put together a custom controller!
One that generates this output:
$dompdf = new Dompdf(); // Pass the HTML markup. $dompdf->loadHtml($markup); // Render the HTML as PDF. $dompdf->render(); // Stream the generated PDF back to user via browser. $dompdf->stream();
The END! These are the top tools, tips, and tricks to rely on when working with PDFs in Drupal 8.
How about you? What other solutions have you found for solving various PDF handling challenges in Drupal?