Fix #684678: Support dompdf 0.6 adding some required defines
[project/print.git] / print_pdf / print_pdf.pages.inc
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Generates the PDF versions of the pages
7 *
8 * This file is included by the print_pdf module and includes the
9 * functions that interface with the PDF generation packages.
10 *
11 * @ingroup print
12 */
13
14 require_once(DRUPAL_ROOT . '/' . drupal_get_path('module', 'print') . '/print.pages.inc');
15
16 /**
17 * Generate a PDF version of the printer-friendly page
18 *
19 * @see print_controller()
20 * @see _print_get_template()
21 * @see _print_pdf_dompdf()
22 * @see _print_pdf_tcpdf()
23 */
24 function print_pdf_controller() {
25 global $base_url, $language;
26
27 // Disable caching for generated PDFs, as Drupal doesn't ouput the proper headers from the cache
28 $GLOBALS['conf']['cache'] = FALSE;
29
30 $args = func_get_args();
31 $path = implode('/', $args);
32 $cid = isset($_GET['comment']) ? (int)$_GET['comment'] : NULL;
33
34 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
35
36 $print = print_controller($path, $cid, PRINT_PDF_FORMAT);
37 if ($print === FALSE) {
38 return;
39 }
40
41 // Img elements must be set to absolute
42 $pattern = '!<(img\s[^>]*?)>!is';
43 $print['content'] = preg_replace_callback($pattern, '_print_rewrite_urls', $print['content']);
44 $print['logo'] = preg_replace_callback($pattern, '_print_rewrite_urls', $print['logo']);
45 $print['footer_message'] = preg_replace_callback($pattern, '_print_rewrite_urls', $print['footer_message']);
46 // And converted from private to public paths
47 $file_downloads = variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC);
48 if ($file_downloads == FILE_DOWNLOADS_PRIVATE) {
49 switch (variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE)) {
50 case LANGUAGE_NEGOTIATION_PATH_DEFAULT:
51 case LANGUAGE_NEGOTIATION_PATH:
52 $lang = $language->language;
53 break;
54 default:
55 $lang = '';
56 break;
57 }
58 $pattern = "!(<img\s[^>]*?src\s*?=\s*?['\"]?)${base_url}/(?:(?:index.php)?\?q=)?(?:${lang}/)?system/files(/[^>]*?>)!is";
59 $replacement = '$1file://' . realpath(file_directory_path()) . '$2';
60 $print['content'] = preg_replace($pattern, $replacement, $print['content']);
61 $print['logo'] = preg_replace($pattern, $replacement, $print['logo']);
62 $print['footer_message'] = preg_replace($pattern, $replacement, $print['footer_message']);
63 }
64 // Send to printer option causes problems with PDF
65 $print['sendtoprinter'] = '';
66
67 $node = $print['node'];
68 ob_start();
69 include_once(DRUPAL_ROOT . '/' . _print_get_template(PRINT_PDF_FORMAT, $print['type']));
70 $html = ob_get_contents();
71 ob_end_clean();
72
73 // Convert the a href elements, to make sure no relative links remain
74 $pattern = '!<(a\s[^>]*?)>!is';
75 $html = preg_replace_callback($pattern, '_print_rewrite_urls', $html);
76 // And make anchor links relative again, to permit in-PDF navigation
77 $html = preg_replace("!${base_url}/" . PRINTPDF_PATH . "/.*?%2523!", '#', $html);
78
79 $pdf_filename = variable_get('print_pdf_filename', PRINT_PDF_FILENAME_DEFAULT);
80 if (function_exists('token_replace') && !empty($pdf_filename)) {
81 $pdf_filename = token_replace($pdf_filename, 'node', $node) . '.pdf';
82 }
83 else {
84 $pdf_filename = str_replace('/', '_', $path) . '.pdf';
85 }
86 if (basename($print_pdf_pdf_tool) == 'dompdf_config.inc.php') {
87 _print_pdf_dompdf($print, $html, $pdf_filename);
88 }
89 elseif (basename($print_pdf_pdf_tool) == 'tcpdf.php') {
90 _print_pdf_tcpdf($print, $html, $pdf_filename);
91 }
92 elseif (substr(basename($print_pdf_pdf_tool, '.exe'), 0, 11) == 'wkhtmltopdf') {
93 _print_pdf_wkhtmltopdf($print, $html, $pdf_filename);
94 }
95 else {
96 return drupal_not_found();
97 }
98
99 $nodepath = (isset($node->path)) ? drupal_get_normal_path($node->path) : 'node/' . $path;
100 db_merge('print_pdf_page_counter')
101 ->key(array('path' => $nodepath))
102 ->fields(array(
103 'totalcount' => 1,
104 'timestamp' => REQUEST_TIME,
105 ))
106 ->expression('totalcount', 'totalcount + :inc', array(':inc' => 1))
107 ->execute();
108 }
109
110 /**
111 * Generate the PDF file using the dompdf library
112 *
113 * @param $print
114 * array containing the configured data
115 * @param $html
116 * contents of the post-processed template already with the node data
117 * @param $filename
118 * name of the PDF file to be generated
119 * @see print_pdf_controller()
120 */
121 function _print_pdf_dompdf($print, $html, $filename) {
122 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
123 $print_pdf_paper_size = variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT);
124 $print_pdf_page_orientation = variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT);
125 $print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT);
126
127 if (variable_get('print_pdf_autoconfig', PRINT_PDF_AUTOCONFIG_DEFAULT)) {
128 define("DOMPDF_ENABLE_PHP", FALSE);
129 define("DOMPDF_ENABLE_REMOTE", TRUE);
130 define("DOMPDF_TEMP_DIR", file_directory_temp());
131 }
132
133 require_once(DRUPAL_ROOT . '/' . $print_pdf_pdf_tool);
134 spl_autoload_register('DOMPDF_autoload');
135
136 // dompdf seems to have problems with something in system.css so let's not use it
137 $html = preg_replace('!<link.*?modules/system/system.css.*?/>!', '', $html);
138
139 $url_array = parse_url($print['url']);
140
141 $protocol = $url_array['scheme'] . '://';
142 $host = $url_array['host'];
143 $path = dirname($url_array['path']) . '/';
144
145 $dompdf = new DOMPDF();
146 $dompdf->set_base_path($path);
147 $dompdf->set_host($host);
148 $dompdf->set_paper(drupal_strtolower($print_pdf_paper_size), $print_pdf_page_orientation);
149 $dompdf->set_protocol($protocol);
150
151 // dompdf can't handle footers cleanly, so disable the following
152 // $html = theme('print_pdf_dompdf_footer', $html);
153
154 // Convert the euro sign to an HTML entity
155 $html = str_replace('€', '&#0128;', $html);
156
157 // Convert from UTF-8 to ISO 8859-1 and then to HTML entities
158 if (function_exists('utf8_decode')) {
159 $html = utf8_decode($html);
160 }
161 // iconv fails silently when it encounters something that it doesn't know, so don't use it
162 // else if (function_exists('iconv')) {
163 // $html = iconv('UTF-8', 'ISO-8859-1', $html);
164 // }
165 elseif (function_exists('mb_convert_encoding')) {
166 $html = mb_convert_encoding($html, 'ISO-8859-1', 'UTF-8');
167 }
168 elseif (function_exists('recode_string')) {
169 $html = recode_string('UTF-8..ISO_8859-1', $html);
170 }
171 $html = htmlspecialchars_decode(htmlentities($html, ENT_NOQUOTES, 'ISO-8859-1'), ENT_NOQUOTES);
172
173 //must get rid of tbody (dompdf goes into recursion)
174 $html = preg_replace('!<tbody[^>]*?>|</tbody>!i', '', $html);
175
176 $dompdf->load_html($html);
177
178 $dompdf->render();
179 $dompdf->stream($filename, array('Attachment' => ($print_pdf_content_disposition == 2)));
180 }
181
182 /**
183 * Generate the PDF file using the TCPDF library
184 *
185 * @param $print
186 * array containing the configured data
187 * @param $html
188 * contents of the post-processed template already with the node data
189 * @param $filename
190 * name of the PDF file to be generated
191 * @see print_pdf_controller()
192 */
193 function _print_pdf_tcpdf($print, $html, $filename) {
194 global $base_url, $language;
195
196 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
197 $print_pdf_paper_size = variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT);
198 $print_pdf_page_orientation = variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT);
199 $print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT);
200
201 $pdf_tool_path = realpath(dirname($print_pdf_pdf_tool));
202
203 if (variable_get('print_pdf_autoconfig', PRINT_PDF_AUTOCONFIG_DEFAULT)) {
204 define('K_TCPDF_EXTERNAL_CONFIG', TRUE);
205 define('K_PATH_MAIN', dirname($_SERVER['SCRIPT_FILENAME']));
206 define('K_PATH_URL', $base_url);
207 define('K_PATH_FONTS', $pdf_tool_path . '/fonts/');
208 define('K_PATH_CACHE', $pdf_tool_path . '/cache/');
209 define('K_PATH_IMAGES', '');
210 define('K_BLANK_IMAGE', $pdf_tool_path . '/images/_blank.png');
211 define('K_CELL_HEIGHT_RATIO', 1.25);
212 define('K_SMALL_RATIO', 2/3);
213 }
214
215 // Decode HTML entities in image filenames
216 $pattern = "!<img\s[^>]*?src\s*?=\s*?['\"]?{$base_url}[^>]*?>!is";
217 $html = preg_replace_callback($pattern, create_function('$matches', 'return html_entity_decode($matches[0], ENT_QUOTES);'), $html);
218 // Remove queries from the image URL
219 $pattern = "!(<img\s[^>]*?src\s*?=\s*?['\"]?{$base_url}[^>]*?)(?:%3F|\?)[\w=&]+([^>]*?>)!is";
220 $html = preg_replace($pattern, '$1$2', $html);
221
222 require_once(DRUPAL_ROOT . '/' . $print_pdf_pdf_tool);
223 if (strpos(PDF_PRODUCER, 'PHP4') === FALSE) {
224 require_once(DRUPAL_ROOT . '/' . drupal_get_path('module', 'print_pdf') . '/print_pdf.class.inc');
225 }
226 else {
227 drupal_set_message(t("The PHP4 version of TCPDF is not supported. Please upgrade it."), 'error', FALSE);
228 drupal_goto($_GET['q']);
229 }
230
231 $font = Array(
232 check_plain(variable_get('print_pdf_font_family', PRINT_PDF_FONT_FAMILY_DEFAULT)),
233 '',
234 check_plain(variable_get('print_pdf_font_size', PRINT_PDF_FONT_SIZE_DEFAULT)),
235 );
236 $orientation = drupal_strtoupper($print_pdf_page_orientation[0]);
237
238 // create new PDF document
239 $pdf = new PrintTCPDF($orientation , 'mm', $print_pdf_paper_size, TRUE);
240
241 // set document information
242 $pdf->SetAuthor(strip_tags($print['submitted']));
243 $pdf->SetCreator(variable_get('site_name', 'Drupal'));
244 $pdf->SetTitle(html_entity_decode($print['title'], ENT_QUOTES, 'UTF-8'));
245 $keys = implode(' ', explode("\n", trim(strip_tags($print['taxonomy']))));
246 $pdf->SetKeywords($keys);
247 $pdf->setPDFVersion('1.6');
248
249 if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
250 $pdf->setRTL(TRUE);
251 }
252
253 $pdf = theme('print_pdf_tcpdf_header', $pdf, $html, $font);
254 $pdf = theme('print_pdf_tcpdf_footer', $pdf, $html, $font);
255 $pdf = theme('print_pdf_tcpdf_page', $pdf);
256
257 //initialize document
258 $pdf->AliasNbPages();
259
260 // add a page
261 $pdf->AddPage();
262
263 $pdf = theme('print_pdf_tcpdf_content', $pdf, $html, $font);
264
265 // reset pointer to the last page
266 $pdf->lastPage();
267
268 // try to recover from any warning/error
269 ob_clean();
270
271 //Close and output PDF document
272 $output_dest = ($print_pdf_content_disposition == 2) ? 'D' : 'I';
273 $pdf->Output($filename, $output_dest);
274 }
275
276 /**
277 * Generate the PDF file using wkhtmltopdf
278 *
279 * @param $print
280 * array containing the configured data
281 * @param $html
282 * contents of the post-processed template already with the node data
283 * @param $filename
284 * name of the PDF file to be generated
285 * @see print_pdf_controller()
286 */
287 function _print_pdf_wkhtmltopdf($print, $html, $filename) {
288 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
289 $print_pdf_paper_size = drupal_strtolower(variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT));
290 $print_pdf_page_orientation = variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT);
291 $print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT);
292 $print_pdf_wkhtmltopdf_options = variable_get('print_pdf_wkhtmltopdf_options', PRINT_PDF_WKHTMLTOPDF_OPTIONS);
293
294 $dpi = 96;
295
296 if (function_exists('token_replace') && !empty($print_pdf_wkhtmltopdf_options)) {
297 $print_pdf_wkhtmltopdf_options = token_replace($print_pdf_wkhtmltopdf_options, 'node', $print['node']);
298 }
299
300 $descriptor = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
301 $pdf_output = (stristr(PHP_OS, 'Win')) ? tempnam(variable_get('file_temporary_path', conf_path() . '/private/temp'), 'pdf') : '-';
302 $cmd = realpath($print_pdf_pdf_tool) . " --page-size $print_pdf_paper_size --orientation $print_pdf_page_orientation --dpi $dpi $print_pdf_wkhtmltopdf_options - $pdf_output";
303
304 $process = proc_open($cmd, $descriptor, $pipes, NULL, NULL);
305
306 if (is_resource($process)) {
307 fwrite($pipes[0], $html);
308 fclose($pipes[0]);
309
310 if (stristr(PHP_OS, 'Win')) {
311 // It seems Windows can't use the pipe properly so get the contents from a file and then delete it.
312 $pdf = file_get_contents($pdf_output);
313 unlink($pdf_output);
314 }
315 else {
316 $pdf = stream_get_contents($pipes[1]);
317 }
318 fclose($pipes[1]);
319
320 stream_set_blocking($pipes[2], 0);
321 $error = stream_get_contents($pipes[2]);
322 if (!empty($error)) {
323 watchdog('print_pdf', 'wkhtmltopdf: ' . $error);
324 }
325 fclose($pipes[2]);
326
327 $retval = proc_terminate($process);
328 }
329
330 if (!empty($pdf)) {
331 if (headers_sent()) {
332 die("Unable to stream pdf: headers already sent");
333 }
334 header("Cache-Control: private");
335 header("Content-Type: application/pdf");
336
337 $attachment = ($print_pdf_content_disposition == 2) ? "attachment" : "inline";
338
339 header("Content-Disposition: $attachment; filename=\"$filename\"");
340
341 echo $pdf;
342 flush();
343 }
344 else {
345 drupal_goto($print['url']);
346 exit;
347 }
348 }
349
350 /**
351 * Format the dompdf footer contents
352 *
353 * @param $html
354 * contents of the body of the HTML from the original node
355 * @see theme_print_pdf_tcpdf_footer()
356 */
357 function theme_print_pdf_dompdf_footer(&$html) {
358 preg_match('!<div class="print-footer">(.*?)</div>!si', $html, $tpl_footer);
359 $html = str_replace($tpl_footer[0], '', $html);
360
361 $text = '<script type="text/php">
362 if (isset($pdf)) {
363 $font = Font_Metrics::get_font("verdana");;
364 $size = 10;
365 $color = array(0,0,0);
366 $text_height = Font_Metrics::get_font_height($font, $size);
367
368 $w = $pdf->get_width();
369 $h = $pdf->get_height();
370
371 $footer = $pdf->open_object();
372
373 // Draw a line along the bottom
374 $y = $h - 25;
375 $pdf->line(15, $y, $w - 15, $y, $color, 1);
376
377 $y += $text_height / 2;
378 $pdf->page_text(15, $y, \'' . addslashes(strip_tags($tpl_footer[1])) . '\', $font, $size, $color);
379
380 $pdf->close_object();
381 $pdf->add_object($footer, "all");
382
383 // Center the text
384 $width = Font_Metrics::get_text_width("Page 1 of 2", $font, $size);
385 $pagenumtxt = t("Page !n of !total", array("!n" => "{PAGE_NUM}", "!total" => "{PAGE_COUNT}"));
386 $pdf->page_text($w - 15 - $width, $y, $pagenumtxt, $font, $size, $color);
387 }
388 </script>';
389
390 return str_replace("<body>", "<body>" . $text, $html);
391 }
392
393 /**
394 * Format the TCPDF header
395 *
396 * @param $pdf
397 * current TCPDF object
398 * @param $html
399 * contents of the body of the HTML from the original node
400 * @param $font
401 * array with the font definition (font name, styles and size)
402 * @see theme_print_pdf_tcpdf_header()
403 */
404 function theme_print_pdf_tcpdf_header(&$pdf, &$html, $font) {
405 preg_match('!<div class="print-logo">(.*?)</div>!si', $html, $tpl_logo);
406 preg_match('!<h1 class="print-title">(.*?)</h1>!si', $html, $tpl_title);
407 preg_match('!<div class="print-site_name">(.*?)</div>!si', $html, $tpl_site_name);
408
409 $ratio = 0;
410 $logo = '';
411 $logo_ret = preg_match('!src\s*=\s*(\'.*?\'|".*?"|[^\s]*)!i', $tpl_logo[1], $matches);
412 if ($logo_ret) {
413 $logo = trim($matches[1], '\'"');
414 $size = getimagesize($logo);
415 $ratio = $size ? ($size[0] / $size[1]) : 0;
416 }
417
418 // set header font
419 $pdf->setHeaderFont($font);
420 // set header margin
421 $pdf->SetHeaderMargin(5);
422 // set header data
423 $pdf->SetHeaderData($logo, 10 * $ratio, html_entity_decode($tpl_title[1], ENT_QUOTES, 'UTF-8'), strip_tags($tpl_site_name[1]));
424
425 return $pdf;
426 }
427
428 /**
429 * Format the TCPDF page settings (margins, etc)
430 *
431 * @param $pdf
432 * current TCPDF object
433 * @see theme_print_pdf_tcpdf_page()
434 */
435 function theme_print_pdf_tcpdf_page(&$pdf) {
436 // set margins
437 $pdf->SetMargins(15, 20, 15);
438 // set auto page breaks
439 $pdf->SetAutoPageBreak(TRUE, 15);
440 // set image scale factor
441 sscanf(PDF_PRODUCER, "TCPDF %d.%d.%d", $major, $minor, $build);
442 $imagescale = (($major >= 4) && ($minor >= 6) && ($build >= 2)) ? 1 : 4;
443 $pdf->setImageScale($imagescale);
444 // set image compression quality
445 $pdf->setJPEGQuality(100);
446
447 return $pdf;
448 }
449
450 /**
451 * Format the TCPDF page content
452 *
453 * @param $pdf
454 * current TCPDF object
455 * @param $html
456 * contents of the body of the HTML from the original node
457 * @param $font
458 * array with the font definition (font name, styles and size)
459 * @see theme_print_pdf_tcpdf_content()
460 */
461 function theme_print_pdf_tcpdf_content(&$pdf, &$html, $font) {
462 // set content font
463 $pdf->setFont($font[0], $font[1], $font[2]);
464
465 preg_match('!<body.*?>(.*)</body>!sim', $html, $matches);
466 $pattern = '!(?:<div class="print-(?:logo|site_name|breadcrumb|footer)">.*?</div>|<hr class="print-hr" />)!si';
467 $matches[1] = preg_replace($pattern, '', $matches[1]);
468
469 // Make CCK fields look better
470 $matches[1] = preg_replace('!(<div class="field.*?>)\s*!sm', '$1', $matches[1]);
471 $matches[1] = preg_replace('!(<div class="field.*?>.*?</div>)\s*!sm', '$1', $matches[1]);
472 $matches[1] = preg_replace('!<div( class="field-label.*?>.*?)</div>!sm', '<strong$1</strong>', $matches[1]);
473
474 // Since TCPDF's writeHTML is so bad with <p>, do everything possible to make it look nice
475 $matches[1] = preg_replace('!<(?:p(|\s+.*?)/?|/p)>!i', '<br$1 />', $matches[1]);
476 $matches[1] = str_replace(array('<div', 'div>'), array('<span', 'span><br />'), $matches[1]);
477 do {
478 $prev = $matches[1];
479 $matches[1] = preg_replace('!(</span>)<br />(\s*?</span><br />)!s', '$1$2', $matches[1]);
480 } while ($prev != $matches[1]);
481
482 @$pdf->writeHTML($matches[1]);
483
484 return $pdf;
485 }
486
487 /**
488 * Format the TCPDF footer contents
489 *
490 * @param $pdf
491 * current TCPDF object
492 * @param $html
493 * contents of the body of the HTML from the original node
494 * @param $font
495 * array with the font definition (font name, styles and size)
496 * @see theme_print_pdf_tcpdf_footer()
497 */
498 function theme_print_pdf_tcpdf_footer(&$pdf, &$html, $font) {
499 preg_match('!<div class="print-footer">(.*?)</div>!si', $html, $tpl_footer);
500 $footer = trim(preg_replace('!</?div[^>]*?>!i', '', $tpl_footer[1]));
501
502 // set footer font
503 $font[2] *= 0.8;
504 $pdf->setFooterFont($font);
505 // set footer margin
506 $pdf->SetFooterMargin(10);
507 // set footer data
508 $pdf->SetFooterData($footer);
509
510 return $pdf;
511 }
512
513 /**
514 * Format the TCPDF footer layout
515 *
516 * @param $pdf
517 * current TCPDF object
518 * @see theme_print_pdf_tcpdf_footer2()
519 */
520 function theme_print_pdf_tcpdf_footer2(&$pdf) {
521 //Position at 1.5 cm from bottom
522 $pdf->writeHTMLCell(0, 15, 15, -10, $pdf->footer, 0, 0, 0, TRUE, '');
523
524 $ormargins = $pdf->getOriginalMargins();
525 $pagenumtxt = t('Page !n of !total', array('!n' => $pdf->PageNo(), '!total' => $pdf->getAliasNbPages()));
526 //Print page number
527 if ($pdf->getRTL()) {
528 $pdf->SetX($ormargins['right']);
529 $pdf->Cell(0, 10, $pagenumtxt, 'T', 0, 'L');
530 }
531 else {
532 $pdf->SetX($ormargins['left']);
533 $pdf->Cell(0, 10, $pagenumtxt, 'T', 0, 'R');
534 }
535
536 return $pdf;
537 }