Drupal – Create PDF file with dompdf

dompdf is a HTML to PDF converter in PHP. In the GShop project, I need it for sending email with PDF attachment by the Ubercart Conditional actions in Drupal. The following example is done in Drupal 6 with Ubercart.

1. Download and extract the dompdf library to <drupal>/sites/all/libraries.

2. It is found that dompdf has some problem when working the Devel module which may lead to the following PHP error.

  • PHP Fatal error: require_once(): Failed opening required ‘/var/www/drupal/sites/all/libraries/dompdf/include/firephp.cls.php’ (include_path=’.:/usr/share/php:/usr/share/pear’) in /var/www/drupal/sites/all/libraries/dompdf/dompdf_config.inc.php on line 208 …

This could be fixed by editing the DOMPDF_autoload function in dompdf_config.inc.php.

...
function DOMPDF_autoload($class) {
  /* Add this checking - START */
  if (mb_strtolower($class)== 'firephp'){
    return;
  }
  /* Add this checking - END */

  $filename = mb_strtolower($class) . ".cls.php";
  require_once(DOMPDF_INC_DIR . "/$filename");
}
...

 

3. Go to Store administration -> Conditional actions and create a predicate. In this example, i will use Order status gets updated as the action trigger.

4. Add a condition for the trigger. ex. Check the order status. Set the trigger when the updated order status Canceled.

5. Add the Execute custom PHP code action under the action tab with the following PHP code.

require_once(realpath(".") . "/sites/all/libraries/dompdf/dompdf_config.inc.php");
spl_autoload_register('DOMPDF_autoload'); 

$html  = '<html>';
$html .= '  <body>';
$html .= '    <p>Hello Eureka!</p>';
$html .= '  </body>';
$html .= '</html>';

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$pdf = $dompdf->output();
file_put_contents(realpath(".") . "/sites/all/libraries/dompdf/eureka.pdf", $pdf);

 

6. Now you can test the dompdf. Create a dummny order and then update its status to Canceled. The PDF file is created!

Done =)

Reference:

5 thoughts on “Drupal – Create PDF file with dompdf”

  1. I am using dompdf for generating pdf in Drupal 7. its working very fine on localhost. But its not working on online server. There it is not generating page also. It just reload the page again. Anybody please help me.

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.