You are here

function commerce_billy_pdf_prepare_dompdf in Commerce Billy 7

Helper function that instantiates new DOMPDF class and renders the HTML.

The returned dompdf object can then be used to either directly stream the output or to create a file.

1 call to commerce_billy_pdf_prepare_dompdf()
commerce_billy_pdf_dompdf in modules/commerce_billy_pdf/commerce_billy_pdf.module
DOMPDF converter

File

modules/commerce_billy_pdf/commerce_billy_pdf.module, line 336
Commerce Billy module file.

Code

function commerce_billy_pdf_prepare_dompdf($html, $filename) {

  // dompdf needs write access to its font directory.
  // Copy "libraries/dompdf/lib/fonts" to your public files directory in order
  // for this to work. Older versions of Dompdf use these constants.
  if (!defined('DOMPDF_FONT_DIR')) {
    $dir = drupal_realpath('public://');
    define('DOMPDF_FONT_DIR', $dir . '/fonts/');
  }
  if (!defined('DOMPDF_TEMP_DIR')) {
    define('DOMPDF_TEMP_DIR', file_directory_temp());
  }

  // First check if dompdf is already auto-loaded with composer, if not try to
  // load it with Libraries API.
  if (class_exists('\\Dompdf\\Dompdf')) {
    $dompdf = new \Dompdf\Dompdf();
    $font_dir = drupal_realpath('public://fonts');

    // Dompdf writes out font files for caching, make sure they are in a
    // writeable directory in the files folder.
    $dompdf
      ->set_option('fontDir', $font_dir);
    $dompdf
      ->set_option('fontCache', $font_dir);
  }
  else {
    $path = libraries_get_path('dompdf');
    if (empty($path)) {
      return FALSE;
    }

    // Support for Dompdf 0.7.0 and newer, using PHP 5.3 namespaces.
    if (file_exists(DRUPAL_ROOT . '/' . $path . '/autoload.inc.php')) {
      require_once DRUPAL_ROOT . '/' . $path . '/autoload.inc.php';
      $dompdf = new \Dompdf\Dompdf();
    }
    else {
      require_once DRUPAL_ROOT . '/' . $path . '/dompdf_config.inc.php';
      spl_autoload_register('DOMPDF_autoload');
      $dompdf = new DOMPDF();
    }
  }
  $dompdf
    ->set_paper('a4', 'portrait');
  $html = htmlspecialchars_decode(htmlentities($html, ENT_NOQUOTES, 'UTF-8'), ENT_NOQUOTES);
  $dompdf
    ->load_html($html);
  $dompdf
    ->render();
  return $dompdf;
}