print_pdf_wkhtmltopdf.module 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * @file
  4. * Generate a PDF for the print_pdf module using the wkhtmltopdf library.
  5. *
  6. * @ingroup print
  7. */
  8. define('PRINT_PDF_WKHTMLTOPDF_OPTIONS', "--footer-font-size 7 --footer-right '[page]'");
  9. define('PRINT_PDF_WKHTMLTOPDF_VERSION_DEFAULT', '');
  10. define('PRINT_PDF_WKHTMLTOPDF_USE_INPUT_FILE_DEFAULT', FALSE);
  11. /**
  12. * Implements hook_pdf_tool_info().
  13. */
  14. function print_pdf_wkhtmltopdf_pdf_tool_info() {
  15. return array(
  16. 'name' => 'wkhtmltopdf',
  17. 'min_version' => '0.9.6',
  18. 'url' => 'http://wkhtmltopdf.org/downloads.html',
  19. 'expand_css' => FALSE,
  20. );
  21. }
  22. /**
  23. * Implements hook_menu().
  24. */
  25. function print_pdf_wkhtmltopdf_menu() {
  26. $items = array();
  27. $items['admin/config/user-interface/print/pdf/wkhtmltopdf'] = array(
  28. 'title' => 'wkhtmltopdf',
  29. 'description' => 'Configure the wkhtmltopdf options.',
  30. 'page callback' => 'drupal_get_form',
  31. 'page arguments' => array('print_pdf_wkhtmltopdf_settings'),
  32. 'access arguments' => array('administer print'),
  33. 'type' => MENU_LOCAL_TASK,
  34. 'file' => 'print_pdf_wkhtmltopdf.admin.inc',
  35. );
  36. return $items;
  37. }
  38. /**
  39. * Implements hook_flush_caches().
  40. */
  41. function print_pdf_wkhtmltopdf_flush_caches() {
  42. // Delete the cached version info during cache clear.
  43. variable_del('print_pdf_wkhtmltopdf_version');
  44. return array();
  45. }
  46. /**
  47. * Implements hook_pdf_tool_version().
  48. */
  49. function print_pdf_wkhtmltopdf_pdf_tool_version($pdf_tool, $reset = TRUE) {
  50. $version = variable_get('print_pdf_wkhtmltopdf_version', PRINT_PDF_WKHTMLTOPDF_VERSION_DEFAULT);
  51. if ($reset || empty($version)) {
  52. // Ask the version information from the executable.
  53. $descriptor = array(
  54. 0 => array('pipe', 'r'),
  55. 1 => array('pipe', 'w'),
  56. 2 => array('pipe', 'w'),
  57. );
  58. $cmd = '"' . realpath($pdf_tool) . '" --version';
  59. $process = proc_open($cmd, $descriptor, $pipes, NULL, NULL);
  60. if (is_resource($process)) {
  61. $content = stream_get_contents($pipes[1]);
  62. preg_match('!.*?(\d+\.\d+\.\d+).*$!m', $content, $matches);
  63. fclose($pipes[0]);
  64. fclose($pipes[1]);
  65. fclose($pipes[2]);
  66. proc_close($process);
  67. // Cache the results of this expensive operation.
  68. variable_set('print_pdf_wkhtmltopdf_version', $matches[1]);
  69. return ($matches[1]);
  70. }
  71. }
  72. else {
  73. // For performance sake, usually use the cached value.
  74. return $version;
  75. }
  76. return 'unknown';
  77. }
  78. /**
  79. * Implements hook_print_pdf_available_libs_alter().
  80. */
  81. function print_pdf_wkhtmltopdf_print_pdf_available_libs_alter(&$pdf_tools) {
  82. module_load_include('inc', 'print', 'includes/print');
  83. $tools = _print_scan_libs('wkhtmltopdf', '!^wkhtmltopdf!');
  84. // See if there is a binary version of wkhtmltopdf available.
  85. if (drupal_substr(php_uname('s'), 0, 3) !== 'Win') {
  86. exec('export PATH="$PATH:/usr/local/bin" ; which wkhtmltopdf', $binary_output, $binary_status);
  87. if (count($binary_output) > 0 && $binary_status == 0) {
  88. $tools[] = $binary_output[0];
  89. }
  90. }
  91. foreach ($tools as $tool) {
  92. $version = print_pdf_wkhtmltopdf_pdf_tool_version($tool, TRUE);
  93. $pdf_tools['print_pdf_wkhtmltopdf|' . $tool] = 'wkhtmltopdf ' . $version . ' (' . $tool . ')';
  94. }
  95. }