You are here

function _print_pdf_dompdf in Printer, email and PDF versions 6

Same name and namespace in other branches
  1. 5.4 print_pdf/print_pdf.pages.inc \_print_pdf_dompdf()
  2. 5.3 print_pdf/print_pdf.pages.inc \_print_pdf_dompdf()
  3. 7 print_pdf/print_pdf.pages.inc \_print_pdf_dompdf()
  4. 5.x print_pdf/print_pdf.pages.inc \_print_pdf_dompdf()

Generate the PDF file using the dompdf library

Parameters

$print: array containing the configured data

$html: contents of the post-processed template already with the node data

$filename: name of the PDF file to be generated

See also

print_pdf_controller()

1 call to _print_pdf_dompdf()
print_pdf_generate_html in print_pdf/print_pdf.pages.inc

File

print_pdf/print_pdf.pages.inc, line 180

Code

function _print_pdf_dompdf($print, $html, $filename = NULL) {
  $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
  $print_pdf_paper_size = variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT);
  $print_pdf_page_orientation = variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT);
  $print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT);
  if (variable_get('print_pdf_autoconfig', PRINT_PDF_AUTOCONFIG_DEFAULT)) {
    if (!defined('DOMPDF_ENABLE_PHP')) {
      define("DOMPDF_ENABLE_PHP", FALSE);
    }
    if (!defined('DOMPDF_ENABLE_REMOTE')) {
      define("DOMPDF_ENABLE_REMOTE", TRUE);
    }
    if (!defined('DOMPDF_TEMP_DIR')) {
      define("DOMPDF_TEMP_DIR", file_directory_temp());
    }
    if (!defined('DOMPDF_UNICODE_ENABLED')) {
      define("DOMPDF_UNICODE_ENABLED", variable_get('print_pdf_dompdf_unicode', PRINT_PDF_DOMPDF_UNICODE_DEFAULT));
    }
    if (!defined('DOMPDF_FONT_CACHE')) {
      define("DOMPDF_FONT_CACHE", file_directory_path() . '/' . PRINT_PDF_DOMPDF_CACHE_DIR_DEFAULT . '/fonts/');
    }
  }
  require_once $print_pdf_pdf_tool;
  if (function_exists('spl_autoload_register')) {
    spl_autoload_register('DOMPDF_autoload');
  }

  // Try to use local file access for image files
  $html = _print_pdf_file_access_images($html);

  // Spaces in img URLs must be replaced with %20
  $pattern = '!<(img\\s[^>]*?)>!is';
  $html = preg_replace_callback($pattern, '_print_replace_spaces', $html);

  // dompdf seems to have problems with something in system.css so let's not use it
  $html = preg_replace('!<link.*?modules/system/system.css.*?/>!', '', $html);
  $url_array = parse_url($print['url']);
  $protocol = $url_array['scheme'] . '://';
  $host = $url_array['host'];
  $path = dirname($url_array['path']) . '/';
  $dompdf = new DOMPDF();
  $dompdf
    ->set_base_path($path);
  $dompdf
    ->set_host($host);
  $dompdf
    ->set_paper(drupal_strtolower($print_pdf_paper_size), $print_pdf_page_orientation);
  $dompdf
    ->set_protocol($protocol);

  // dompdf can't handle footers cleanly, so disable the following
  //  $html = theme('print_pdf_dompdf_footer', $html);
  // If dompdf Unicode support is disabled, try to convert to ISO-8859-1 and then to HTML entities
  if (!variable_get('print_pdf_dompdf_unicode', PRINT_PDF_DOMPDF_UNICODE_DEFAULT)) {

    // Convert the euro sign to an HTML entity
    $html = str_replace('€', '&#0128;', $html);

    // Convert from UTF-8 to ISO 8859-1 and then to HTML entities
    if (function_exists('utf8_decode')) {
      $html = utf8_decode($html);
    }
    elseif (function_exists('mb_convert_encoding')) {
      $html = mb_convert_encoding($html, 'ISO-8859-1', 'UTF-8');
    }
    elseif (function_exists('recode_string')) {
      $html = recode_string('UTF-8..ISO_8859-1', $html);
    }
    $html = htmlspecialchars_decode(htmlentities($html, ENT_NOQUOTES, 'ISO-8859-1'), ENT_NOQUOTES);
  }
  else {

    // Otherwise, ensure the content is properly formatted Unicode.
    $html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
  }

  // Must get rid of tbody (dompdf goes into recursion)
  $html = preg_replace('!<tbody[^>]*?>|</tbody>!i', '', $html);
  $dompdf
    ->load_html($html);
  $dompdf
    ->render();
  if ($filename) {
    $dompdf
      ->stream($filename, array(
      'Attachment' => $print_pdf_content_disposition == 2,
    ));
    return TRUE;
  }
  else {
    return $dompdf
      ->output();
  }
}