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