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