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