3

We are looking for a way to convert the Drupal pages into PDF on click of a button for D8. Initially we thought we could use Print Module, but that only has a D7 version.

How do you create a button in D8 so that on click, it will convert the Drupal node into a PDF keeping the current theme styling and elements appropriately? We have been unable to find any D8 modules or process that can allow for this.

5
  • 1
    You’re looking for Entity Print. If you have some technical skills, you might be able to implement a print service like AthenaPDF.
    – Kevin
    Jan 8, 2018 at 22:27
  • If the requirement is to print each pages, then use javascript code : <a class="print-button" href="javascript:window.print()">print</a>
    – developer
    Jan 9, 2018 at 4:59
  • @Kevin when i reviewed AthenaPDF it looks like there is a version available for a Docker image. Do you know if it works on a Drupal instance? I cannot find any documentation outside of ones found on Docker's site regarding it Jan 19, 2018 at 16:56
  • 1
    It acts as a microservice, so you would run it in its own instance and call its API when necessary to return a PDF. I think it can also be run on-demand on the same server, but its been a while since I tried.
    – Kevin
    Jan 19, 2018 at 17:08
  • @Kevin i am going to research the on-demand approach to see if i can get that to run because not sure we can get a new instance. thanks for the suggestion. Jan 19, 2018 at 20:13

2 Answers 2

7

You can achieve with several solutions

Use Entity Print Module

Entity Print allows you to print any Drupal entity (Drupal 7 and 8) or View (Drupal 8 only) to PDF.

The module is lightweight compared to other modules like the Print module, has full test coverage and is ready to be used in production for both D7 and D8.

Use new host of print module (Printable Module)

This is the new home of the print module. The module makes use of pdf_api module to generate PDFs.

Use PrintFriendly Module (it supports localization very good)

The #1 Print and PDF button for your Drupal site. Printer Friendly pages without coding, css, or print.css. Fast, easy, and professional.

The Print Friendly & PDF button saves paper and ink when printing or creating a PDF. It's fast, easy, and looks great when printed. Add the button now, and your users will see the difference.

How Print Friendly & PDF Works The Print Friendly & PDF button automatically creates printer friendly and PDF versions of your pages without the hassle of having to create a print CSS file. No coding, hacking or programming required. Simply install the Print Friendly & PDF plugin, activate, and choose settings for full customization. It also gives your user the ability to remove images and paragraphs of text, so they really only have to print exactly what they want.

And final solution is kind of tricky solution, instead of converting the page to PDF just provide a print theme style for your nodes, when a user clicks on the print button (or link) trigger javascript:window.print() action. most of the new OS has PDF printer build-in

By the way, I recommend PrintFriendly Module

5
  • 1
    I am testing the entity_print module and have not found a good way to theme it. I will test the other modules as well. Jan 10, 2018 at 17:33
  • 1
    @user2951257 ,good, inform me with the new results.
    – Yuseferi
    Jan 10, 2018 at 18:17
  • 1
    i have found a way to theme the entity_print module. I tried the others but since entity_print provides a PDF view mode this will suffice for my needs. Jan 10, 2018 at 23:14
  • 1
    I do agree that PrintFriendly is good, but our use case is to download the PDF right away. PrintFriendly gives a popup which may not be acceptable by the product owner. Jan 10, 2018 at 23:37
  • 1
    @user2951257 there isa configuration to achieve it.
    – Yuseferi
    Jan 11, 2018 at 10:57
1

Having a D8 custom module, a very easy way is:

  1. install the mpdf library

    composer require mpdf/mpdf
    
  2. pass your output to mpdf

    $html = 'content for my pdf';
    $mpdf = new \Mpdf\Mpdf(['tempDir' => 'sites/default/files/tmp']);
    $mpdf->WriteHTML($output);
    $mpdf->Output('file.pdf', 'D');
    exit;
    

You should also add mpdf as dependency in your composer.json. See https://www.drupal.org/docs/creating-custom-modules/add-a-composerjson-file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.