print_pdf_mpdf.pages.inc 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * @file
  4. * Generates the PDF version using mPDF.
  5. *
  6. * This file is included by the print_pdf_mpdf module and includes the
  7. * functions that interface with the mPDF library.
  8. *
  9. * @ingroup print
  10. */
  11. /**
  12. * Implements hook_print_pdf_generate().
  13. */
  14. function print_pdf_mpdf_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. // Version 7 of the mpdf library uses a composer autoloader.
  18. // Also there no longer is way to truly detect the library version, so this
  19. // seems like the best alternative.
  20. $mpdf_version_7_plus = strpos($pdf_tool[1], 'autoload.php') !== FALSE;
  21. if (empty($paper_size)) {
  22. $paper_size = variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT);
  23. }
  24. if (empty($page_orientation)) {
  25. $page_orientation = variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT);
  26. }
  27. $images_via_file = variable_get('print_pdf_images_via_file', PRINT_PDF_IMAGES_VIA_FILE_DEFAULT);
  28. $config = array();
  29. if ($mpdf_version_7_plus) {
  30. $config['tempDir'] = drupal_realpath('public://print_pdf/print_pdf_mpdf/');
  31. }
  32. else {
  33. // Deprecated since mpdf v7.x.
  34. if (variable_get('print_pdf_autoconfig', PRINT_PDF_AUTOCONFIG_DEFAULT)) {
  35. if (!defined('_MPDF_TTFONTDATAPATH')) {
  36. define('_MPDF_TTFONTDATAPATH', drupal_realpath('public://print_pdf/print_pdf_mpdf/ttfontdata/'));
  37. }
  38. if (!defined('_MPDF_TEMP_PATH')) {
  39. define('_MPDF_TEMP_PATH', drupal_realpath('public://print_pdf/print_pdf_mpdf/tmp/'));
  40. }
  41. }
  42. }
  43. $tool_path = DRUPAL_ROOT . '/' . $pdf_tool[1];
  44. if (file_exists($tool_path)) {
  45. require_once $tool_path;
  46. }
  47. else {
  48. watchdog('print_pdf', 'Configured PDF tool does not exist at path: %path', array('%path' => $tool_path), WATCHDOG_ERROR);
  49. throw new Exception("Configured PDF tool does not exist, unable to generate PDF.");
  50. }
  51. $format = ($page_orientation == "landscape") ? $paper_size . "-L" : $paper_size;
  52. // Try to use local file access for image files.
  53. $html = _print_access_images_via_file($html, $images_via_file);
  54. // Set document information.
  55. if ($mpdf_version_7_plus) {
  56. $config['mode'] = 'utf-8';
  57. $config['format'] = $format;
  58. $mpdf = new \Mpdf\Mpdf($config);
  59. }
  60. else {
  61. $mpdf = new mPDF('UTF-8', $format);
  62. }
  63. if (isset($meta['name'])) {
  64. $mpdf->SetAuthor(strip_tags($meta['name']));
  65. }
  66. $mpdf->SetCreator(variable_get('site_name', 'Drupal'));
  67. /*
  68. // Pulled from the HTML meta data
  69. $mpdf->SetTitle(html_entity_decode($meta['title'], ENT_QUOTES, 'UTF-8'));
  70. $keys = implode(' ', explode("\n", trim(strip_tags($print['taxonomy']))));
  71. $mpdf->SetKeywords($keys);
  72. // Encrypt the file and grant permissions to the user to copy and print
  73. // No password is required to open the document
  74. // Owner has full rights using the password "MyPassword"
  75. $mpdf->SetProtection(array('copy', 'print'), '', 'MyPassword');
  76. $mpdf->SetProtection(array('copy', 'print', 'print-highres'), '', '');
  77. */
  78. drupal_alter('print_pdf_mpdf', $mpdf, $html, $meta);
  79. $mpdf->WriteHTML($html);
  80. // Try to recover from any warning/error.
  81. ob_clean();
  82. return $mpdf->Output('', 'S');
  83. }