[Drupal 7] How to generate a PDF file from an HTML template using dompdf?

| | 2 min read

One of our clients requested us to come up with a Drupal based web application to generate a PDF file from an HTML template. The client's site which runs on Drupal 7 allows users to create envelopes based on the custom designs submitted by the user through that Drupal website. The online envelope editor uses an HTML template during the edit phase of the envelope and generates a PDF file of the sample template when the edit process is completed. If you are looking to know how to generate a PDF file from an HTML template using dompdf then read on.

To bring out the Drupal application we decided to use the dompdf library. The dompdf library is an HTML to PDF converter written in PHP. It is capable of reading external style sheets, inline style tags and the style attributes of individual HTML elements and includes support for most presentational HTML attributes.

Follow the steps below to setup the functionality in your Drupal site.

  • First download the Drupal Print module along with the dompdf library. (The print module can be used to print a pdf file but to generate a pdf from a field of a node you need to use the dompdf library)
  • Simply download and place the Print module in
    sites/all/modules

    directory of your Drupal site. There is no need to enable the module

  • Now place the uncompressed dompdf library inside
    sites/all/modules/print/lib
  • Now place the following code in one of your custom modules
    require_once("sites/all/modules/print/lib/dompdf/dompdf_config.inc.php");
    $html = 'content' // you may add your content here
    $dompdf = new DOMPDF;
    $dompdf->load_html($html);
    $dompdf->render();
    // This does not save the pdf field and instead it opens a dialog box asking whether you have to save the pdf or not
    $dompdf->stream("sample.pdf");
  • If you want to save the pdf somewhere safe in your Drupal site use the code below
    require_once("dompdf_config.inc.php");
    require_once("sites/all/modules/print/lib/dompdf/dompdf_config.inc.php");
    $html = 'content' // you may add your content here
    $dompdf = new DOMPDF;
    $dompdf->load_html($html);
    $dompdf->render();
    $pdfoutput = $dompdf->output();
    //  Checks whether there is an output folder inside sites/default/files
    if (!is_dir('public://output')) {
      mkdir("public://output", 0777);
    //  Creates a folder and changes its permissions}
      $filename = 'sites/default/files/output/' . 'sample.pdf'
      $fp = fopen($filename, "w+");
      fwrite($fp, $pdfoutput);
    //  Writes the pdf output to a file
      fclose($fp);

Now you should be able to genrates PDF files easily from any HTML template files in your Drupal site.

Are you looking for a way to build a similar high quality web application using Drupal? We are Drupal experts and we can provide a wide range of Drupal module development services to get you started with your application. Get in touch with us to know more.