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