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:

About these ads

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s