print_pdf_dompdf.pages.inc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * @file
  4. * Generates the PDF version using dompdf
  5. *
  6. * This file is included by the print_pdf_dompdf module and includes the
  7. * functions that interface with the dompdf library
  8. *
  9. * @ingroup print
  10. */
  11. /**
  12. * Implements hook_print_pdf_generate().
  13. */
  14. function print_pdf_dompdf_print_pdf_generate($html, $meta, $paper_size = NULL, $page_orientation = NULL) {
  15. module_load_include('inc', 'print', 'includes/print');
  16. $pdf_tool = explode('|', variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT));
  17. if (empty($paper_size)) {
  18. $paper_size = variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT);
  19. }
  20. if (empty($page_orientation)) {
  21. $page_orientation = variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT);
  22. }
  23. $content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT);
  24. $images_via_file = variable_get('print_pdf_images_via_file', PRINT_PDF_IMAGES_VIA_FILE_DEFAULT);
  25. $unicode = TRUE;
  26. if (variable_get('print_pdf_autoconfig', PRINT_PDF_AUTOCONFIG_DEFAULT)) {
  27. $unicode = variable_get('print_pdf_dompdf_unicode', PRINT_PDF_DOMPDF_UNICODE_DEFAULT);
  28. $font_subsetting = variable_get('print_pdf_dompdf_font_subsetting', PRINT_PDF_DOMPDF_FONT_SUBSETTING_DEFAULT);
  29. if (!defined('DOMPDF_ENABLE_PHP')) define('DOMPDF_ENABLE_PHP', FALSE);
  30. if (!defined('DOMPDF_ENABLE_REMOTE')) define('DOMPDF_ENABLE_REMOTE', TRUE);
  31. if (!defined('DOMPDF_TEMP_DIR')) define('DOMPDF_TEMP_DIR', file_directory_temp());
  32. if (!defined('DOMPDF_UNICODE_ENABLED')) define('DOMPDF_UNICODE_ENABLED', $unicode);
  33. if (!defined('DOMPDF_ENABLE_FONTSUBSETTING')) define('DOMPDF_ENABLE_FONTSUBSETTING', $font_subsetting);
  34. if (!defined('DOMPDF_FONT_CACHE')) define('DOMPDF_FONT_CACHE', drupal_realpath('public://print_pdf/print_pdf_dompdf/fonts/'));
  35. }
  36. require_once(DRUPAL_ROOT . '/' . $pdf_tool[1]);
  37. spl_autoload_register('DOMPDF_autoload');
  38. // Try to use local file access for image files
  39. $html = _print_access_images_via_file($html, $images_via_file);
  40. // Spaces in img URLs must be replaced with %20, when using external access
  41. if (!$images_via_file) {
  42. $pattern = '!<(img\s[^>]*?)>!is';
  43. $html = preg_replace_callback($pattern, '_print_replace_spaces', $html);
  44. }
  45. // dompdf seems to have problems with something in system.css so let's not use it
  46. $html = preg_replace('!<link.*?modules/system/system.css.*?/>!', '', $html);
  47. $url_array = parse_url($meta['url']);
  48. $protocol = $url_array['scheme'] . '://';
  49. $host = $url_array['host'];
  50. $path = dirname($url_array['path']) . '/';
  51. $dompdf = new DOMPDF();
  52. $dompdf->set_base_path($path);
  53. $dompdf->set_host($host);
  54. $dompdf->set_paper(drupal_strtolower($paper_size), $page_orientation);
  55. $dompdf->set_protocol($protocol);
  56. // dompdf can't handle footers cleanly, so disable the following
  57. // $html = theme('print_pdf_dompdf_footer', array('html' => $html));
  58. // If dompdf Unicode support is disabled, try to convert to ISO-8859-1 and then to HTML entities
  59. if (!$unicode) {
  60. // Convert the euro sign to an HTML entity
  61. $html = str_replace('€', '&#0128;', $html);
  62. // Convert from UTF-8 to ISO 8859-1 and then to HTML entities
  63. if (function_exists('utf8_decode')) {
  64. $html = utf8_decode($html);
  65. }
  66. // iconv fails silently when it encounters something that it doesn't know, so don't use it
  67. // else if (function_exists('iconv')) {
  68. // $html = iconv('UTF-8', 'ISO-8859-1', $html);
  69. // }
  70. elseif (function_exists('mb_convert_encoding')) {
  71. $html = mb_convert_encoding($html, 'ISO-8859-1', 'UTF-8');
  72. }
  73. elseif (function_exists('recode_string')) {
  74. $html = recode_string('UTF-8..ISO_8859-1', $html);
  75. }
  76. $html = htmlspecialchars_decode(htmlentities($html, ENT_NOQUOTES, 'ISO-8859-1'), ENT_NOQUOTES);
  77. }
  78. else {
  79. // Otherwise, ensure the content is properly formatted Unicode.
  80. $html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
  81. }
  82. // Must get rid of tbody (dompdf goes into recursion)
  83. $html = preg_replace('!<tbody[^>]*?>|</tbody>!i', '', $html);
  84. $dompdf->load_html($html);
  85. $dompdf->render();
  86. return $dompdf->output();
  87. }
  88. /**
  89. * Format the dompdf footer contents
  90. *
  91. * @param array $vars
  92. * An associative array containing:
  93. * - $html: contents of the body of the HTML from the original node
  94. *
  95. * @return string
  96. * customized HTML text
  97. *
  98. * @ingroup themeable
  99. * @ingroup print_themeable
  100. */
  101. function theme_print_pdf_dompdf_footer($vars) {
  102. preg_match('!<div class="print-footer">(.*?)</div>!si', $vars['html'], $tpl_footer);
  103. if (isset($tpl_footer[1])) {
  104. $html = str_replace($tpl_footer[0], '', $vars['html']);
  105. $text = '<script type="text/php">
  106. if (isset($pdf)) {
  107. $font = Font_Metrics::get_font("verdana");;
  108. $size = 10;
  109. $color = array(0,0,0);
  110. $text_height = Font_Metrics::get_font_height($font, $size);
  111. $w = $pdf->get_width();
  112. $h = $pdf->get_height();
  113. $footer = $pdf->open_object();
  114. // Draw a line along the bottom
  115. $y = $h - 25;
  116. $pdf->line(15, $y, $w - 15, $y, $color, 1);
  117. $y += $text_height / 2;
  118. $pdf->page_text(15, $y, \'' . addslashes(strip_tags($tpl_footer[1])) . '\', $font, $size, $color);
  119. $pdf->close_object();
  120. $pdf->add_object($footer, "all");
  121. // Center the text
  122. $width = Font_Metrics::get_text_width("Page 1 of 2", $font, $size);
  123. $pagenumtxt = t("Page !n of !total", array("!n" => "{PAGE_NUM}", "!total" => "{PAGE_COUNT}"));
  124. $pdf->page_text($w - 15 - $width, $y, $pagenumtxt, $font, $size, $color);
  125. }
  126. </script>';
  127. return str_replace("<body>", "<body>" . $text, $html);
  128. } else {
  129. return $vars['html'];
  130. }
  131. }