print_pdf_wkhtmltopdf.pages.inc 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * @file
  4. * Generates the PDF version using wkhtmltopdf
  5. *
  6. * This file is included by the print_pdf_wkhtmltopdf module and includes the
  7. * functions that interface with the wkhtmltopdf library
  8. *
  9. * @ingroup print
  10. */
  11. /**
  12. * Implements hook_print_pdf_generate().
  13. */
  14. function print_pdf_wkhtmltopdf_print_pdf_generate($html, $meta, $paper_size = NULL, $page_orientation = NULL) {
  15. $pdf_tool = explode('|', variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT));
  16. if (empty($paper_size)) {
  17. $paper_size = variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT);
  18. }
  19. if (empty($page_orientation)) {
  20. $page_orientation = variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT);
  21. }
  22. $content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT);
  23. $wkhtmltopdf_options = variable_get('print_pdf_wkhtmltopdf_options', PRINT_PDF_WKHTMLTOPDF_OPTIONS);
  24. $dpi = 96;
  25. if (!empty($wkhtmltopdf_options) && isset($meta['node'])) {
  26. $wkhtmltopdf_options = token_replace($wkhtmltopdf_options, array('node' => $meta['node']), array('clear' => TRUE));
  27. }
  28. $version = print_pdf_wkhtmltopdf_pdf_tool_version($pdf_tool[1]);
  29. // 0.10.0 beta2 identifies itself as 0.9.9
  30. if (version_compare($version, '0.9.9', '>=')) {
  31. $wkhtmltopdf_options = '--disable-local-file-access ' . $wkhtmltopdf_options;
  32. }
  33. elseif (version_compare($version, '0.9.6', '>=')) {
  34. $wkhtmltopdf_options = '--disallow-local-file-access ' . $wkhtmltopdf_options;
  35. }
  36. else {
  37. drupal_goto($meta['url']);
  38. exit;
  39. }
  40. // use basic http authentication to fetch included CSS, etc
  41. if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
  42. $wkhtmltopdf_options .= ' --username ' . check_plain($_SERVER['PHP_AUTH_USER']) . ' --password ' . check_plain($_SERVER['PHP_AUTH_PW']);
  43. }
  44. $descriptor = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'a'));
  45. $cmd = '"' . realpath($pdf_tool[1]) . "\" --page-size $paper_size --orientation $page_orientation --dpi $dpi $wkhtmltopdf_options - -";
  46. $process = proc_open($cmd, $descriptor, $pipes, NULL, NULL);
  47. if (is_resource($process)) {
  48. fwrite($pipes[0], $html);
  49. fclose($pipes[0]);
  50. $pdf = stream_get_contents($pipes[1]);
  51. fclose($pipes[1]);
  52. stream_set_blocking($pipes[2], 0);
  53. $error = stream_get_contents($pipes[2]);
  54. fclose($pipes[2]);
  55. $retval = proc_close($process);
  56. if (!empty($error) || ($retval != 0)) {
  57. if (empty($error)) {
  58. $error = 'No stderr output available.';
  59. }
  60. watchdog('print_pdf', 'wkhtmltopdf [%cmd] (returned %ret): %error', array('%cmd' => $cmd, '%ret' => $retval, '%error' => $error));
  61. }
  62. }
  63. if (!empty($pdf)) {
  64. return $pdf;
  65. }
  66. else {
  67. drupal_set_message(t('Unable to generate PDF file.'), 'error');
  68. drupal_goto($meta['url']);
  69. return NULL;
  70. }
  71. }