print_pdf_wkhtmltopdf.module 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. /**
  10. * Implements hook_pdf_tool_info().
  11. */
  12. function print_pdf_wkhtmltopdf_pdf_tool_info() {
  13. return array(
  14. 'name' => 'wkhtmltopdf',
  15. 'min_version' => '0.9.6',
  16. 'url' => 'http://code.google.com/p/wkhtmltopdf/downloads/list',
  17. 'expand_css' => FALSE,
  18. );
  19. }
  20. /**
  21. * Implements hook_menu().
  22. */
  23. function print_pdf_wkhtmltopdf_menu() {
  24. $items = array();
  25. $items['admin/config/user-interface/print/pdf/wkhtmltopdf'] = array(
  26. 'title' => 'wkhtmltopdf',
  27. 'description' => 'Configure the wkhtmltopdf options.',
  28. 'page callback' => 'drupal_get_form',
  29. 'page arguments' => array('print_pdf_wkhtmltopdf_settings'),
  30. 'access arguments' => array('administer print'),
  31. 'type' => MENU_LOCAL_TASK,
  32. 'file' => 'print_pdf_wkhtmltopdf.admin.inc',
  33. );
  34. return $items;
  35. }
  36. /**
  37. * Implements hook_requirements().
  38. */
  39. function print_pdf_wkhtmltopdf_requirements($phase) {
  40. $requirements = array();
  41. $t = get_t();
  42. switch ($phase) {
  43. // At runtime, make sure that a PDF generation tool is selected
  44. case 'runtime':
  45. $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
  46. if (!empty($print_pdf_pdf_tool)) {
  47. $tool = explode('|', $print_pdf_pdf_tool);
  48. if (is_file($tool[1]) && is_readable($tool[1])) {
  49. if (drupal_substr(basename($tool[1], '.exe'), 0, 11) == 'wkhtmltopdf') {
  50. if (function_exists('is_executable') && !is_executable($tool[1])) {
  51. $requirements['print_pdf_wkhtmltopdf'] = array(
  52. 'title' => $t('wkhtmltopdf library'),
  53. 'value' => $t('Non-executable permissions'),
  54. 'description' => $t('You must modify the permissions of the wkhtmltopdf file (%file) to make it executable.', array('%file' => $tool[1])),
  55. 'severity' => REQUIREMENT_ERROR,
  56. );
  57. }
  58. }
  59. }
  60. }
  61. break;
  62. }
  63. return $requirements;
  64. }
  65. /**
  66. * Implements hook_pdf_tool_version().
  67. */
  68. function print_pdf_wkhtmltopdf_pdf_tool_version($pdf_tool) {
  69. $descriptor = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
  70. $cmd = '"' . realpath($pdf_tool) . '" --version';
  71. $process = proc_open($cmd, $descriptor, $pipes, NULL, NULL);
  72. if (is_resource($process)) {
  73. $content = stream_get_contents($pipes[1]);
  74. $out = preg_match('!.*?(\d+\.\d+\.\d+).*$!m', $content, $matches);
  75. fclose($pipes[0]);
  76. fclose($pipes[1]);
  77. fclose($pipes[2]);
  78. $retval = proc_close($process);
  79. }
  80. return ($matches[1]);
  81. }
  82. /**
  83. * Implements hook_print_pdf_available_libs_alter().
  84. */
  85. function print_pdf_wkhtmltopdf_print_pdf_available_libs_alter(&$pdf_tools) {
  86. module_load_include('inc', 'print', 'includes/print');
  87. $tools = _print_scan_libs('wkhtmltopdf', '!^wkhtmltopdf!');
  88. foreach ($tools as $tool) {
  89. $version = print_pdf_wkhtmltopdf_pdf_tool_version($tool);
  90. $pdf_tools['print_pdf_wkhtmltopdf|' . $tool] = 'wkhtmltopdf ' . $version . ' (' . $tool . ')';
  91. }
  92. }