6 * Generates the PDF versions of the pages
8 * This file is included by the print_pdf module and includes the
9 * functions that interface with the PDF generation packages.
12 require_once(DRUPAL_ROOT .
'/' .
drupal_get_path('module', 'print') .
'/print.pages.inc');
15 * Generate a PDF version of the printer-friendly page
17 * @see print_controller()
18 * @see _print_get_template()
19 * @see _print_pdf_dompdf()
20 * @see _print_pdf_tcpdf()
22 function print_pdf_controller() {
23 global $base_url, $language;
25 // Disable caching for generated PDFs, as Drupal doesn't ouput the proper headers from the cache
26 $GLOBALS['conf']['cache'] = FALSE
;
28 $args = func_get_args();
29 $path = implode('/', $args);
30 $cid = isset($_GET['comment']) ?
(int)$_GET['comment'] : NULL
;
32 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT
);
34 $print = print_controller($path, $cid, PRINT_PDF_FORMAT
);
35 if ($print === FALSE
) {
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
;
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']);
63 $node = $print['node'];
65 include_once(DRUPAL_ROOT .
'/' .
_print_get_template(PRINT_PDF_FORMAT
, $print['type']));
66 $html = ob_get_contents();
69 // Convert the a href elements
70 $pattern = '!<(a\s[^>]*?)>!is';
71 $html = preg_replace_callback($pattern, '_print_rewrite_urls', $html);
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';
78 $pdf_filename = str_replace('/', '_', $path) .
'.pdf';
80 if (basename($print_pdf_pdf_tool) == 'dompdf_config.inc.php') {
81 _print_pdf_dompdf($print, $html, $pdf_filename);
83 elseif (basename($print_pdf_pdf_tool) == 'tcpdf.php') {
84 _print_pdf_tcpdf($print, $html, $pdf_filename);
86 elseif (basename($print_pdf_pdf_tool) == 'wkhtmltopdf') {
87 _print_pdf_wkhtmltopdf($print, $html, $pdf_filename);
90 return drupal_not_found();
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))
98 'timestamp' => REQUEST_TIME
,
100 ->expression('totalcount', 'totalcount + :inc', array(':inc' => 1))
105 * Generate the PDF file using the dompdf library
108 * array containing the configured data
110 * contents of the post-processed template already with the node data
112 * name of the PDF file to be generated
113 * @see print_pdf_controller()
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);
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);
125 $url_array = parse_url($print['url']);
127 $protocol = $url_array['scheme'] .
'://';
128 $host = $url_array['host'];
129 $path = dirname($url_array['path']) .
'/';
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);
137 // dompdf can't handle footers cleanly, so disable the following
138 // $html = theme('print_pdf_dompdf_footer', $html);
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);
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);
148 elseif (function_exists('mb_convert_encoding')) {
149 $html = mb_convert_encoding($html, 'ISO-8859-1', 'UTF-8');
151 elseif (function_exists('recode_string')) {
152 $html = recode_string('UTF-8..ISO_8859-1', $html);
154 $html = htmlspecialchars_decode(htmlentities($html, ENT_NOQUOTES
, 'ISO-8859-1'), ENT_NOQUOTES
);
156 //must get rid of tbody (dompdf goes into recursion)
157 $html = preg_replace('!<tbody[^>]*?>|</tbody>!i', '', $html);
159 $dompdf->load_html($html);
162 $dompdf->stream($filename, array('Attachment' => ($print_pdf_content_disposition == 2)));
166 * Generate the PDF file using the TCPDF library
169 * array containing the configured data
171 * contents of the post-processed template already with the node data
173 * name of the PDF file to be generated
174 * @see print_pdf_controller()
176 function _print_pdf_tcpdf($print, $html, $filename) {
177 global $base_url, $language;
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
);
184 $pdf_tool_path = realpath(dirname($print_pdf_pdf_tool));
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);
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);
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');
208 drupal_set_message(t("The PHP4 version of TCPDF is not supported. Please upgrade it."), 'error', FALSE
);
209 drupal_goto($_GET['q']);
213 variable_get('print_pdf_font_family', PRINT_PDF_FONT_FAMILY_DEFAULT
),
215 variable_get('print_pdf_font_size', PRINT_PDF_FONT_SIZE_DEFAULT
),
217 $orientation = drupal_strtoupper($print_pdf_page_orientation[0]);
219 // create new PDF document
220 $pdf = new
PrintTCPDF($orientation , 'mm', $print_pdf_paper_size, TRUE
);
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');
230 if (defined('LANGUAGE_RTL') && $language->direction
== LANGUAGE_RTL
) {
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);
238 //initialize document
239 $pdf->AliasNbPages();
244 $pdf = theme('print_pdf_tcpdf_content', $pdf, $html, $font);
246 // reset pointer to the last page
249 // try to recover from any warning/error
252 //Close and output PDF document
253 $output_dest = ($print_pdf_content_disposition == 2) ?
'D' : 'I';
254 $pdf->Output($filename, $output_dest);
258 * Generate the PDF file using wkhtmltopdf
261 * array containing the configured data
263 * contents of the post-processed template already with the node data
265 * name of the PDF file to be generated
266 * @see print_pdf_controller()
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
);
276 $xvfb_binary = key(file_scan_directory(drupal_get_path('module', 'print'), '!^Xvfb$!'));
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);
285 $xcmd = realpath($xvfb_binary) .
" :$xdisplay -screen 0 320x200x24 -dpi $dpi -terminate -nolisten tcp -tst $print_pdf_xvfb_options";
287 $xdescriptor = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
288 $xprocess = proc_open($xcmd, $xdescriptor, $xpipes, NULL
, NULL
);
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 - -";
294 $process = proc_open($cmd, $descriptor, $pipes, NULL
, isset($xvfb_binary) ?
array('DISPLAY' => ':' .
$xdisplay) : NULL
);
296 if (is_resource($process)) {
297 fwrite($pipes[0], $html);
300 $pdf = stream_get_contents($pipes[1]);
303 stream_set_blocking($xpipes[2], 0);
304 $error = stream_get_contents($pipes[2]);
305 if (!empty($error)) {
306 watchdog('print_pdf', 'wkhtmltopdf: ' .
$error);
310 $retval = proc_terminate($process);
312 if (isset($xvfb_binary)) {
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);
322 proc_terminate($xprocess);
326 if (headers_sent()) {
327 die("Unable to stream pdf: headers already sent");
329 header("Cache-Control: private");
330 header("Content-Type: application/pdf");
332 $attachment = ($print_pdf_content_disposition == 2) ?
"attachment" : "inline";
334 header("Content-Disposition: $attachment; filename=\"$filename\"");
340 drupal_goto($print['url']);
346 * Format the dompdf footer contents
349 * contents of the body of the HTML from the original node
350 * @see theme_print_pdf_tcpdf_footer()
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);
356 $text = '<script type="text/php">
358 $font = Font_Metrics::get_font("verdana");;
360 $color = array(0,0,0);
361 $text_height = Font_Metrics::get_font_height($font, $size);
363 $w = $pdf->get_width();
364 $h = $pdf->get_height();
366 $footer = $pdf->open_object();
368 // Draw a line along the bottom
370 $pdf->line(15, $y, $w - 15, $y, $color, 1);
372 $y += $text_height / 2;
373 $pdf->page_text(15, $y, \'' .
addslashes(strip_tags($tpl_footer[1])) .
'\', $font, $size, $color);
375 $pdf->close_object();
376 $pdf->add_object($footer, "all");
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);
385 return str_replace("<body>", "<body>" .
$text, $html);
389 * Format the TCPDF header
392 * current TCPDF object
394 * contents of the body of the HTML from the original node
396 * array with the font definition (font name, styles and size)
397 * @see theme_print_pdf_tcpdf_header()
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);
406 $logo_ret = preg_match('!src\s*=\s*(\'.*?\'|".*?"|[^\s]*)!i', $tpl_logo[1], $matches);
408 $logo = trim($matches[1], '\'"');
409 $size = getimagesize($logo);
410 $ratio = $size ?
($size[0] / $size[1]) : 0;
414 $pdf->setHeaderFont($font);
416 $pdf->SetHeaderMargin(5);
418 $pdf->SetHeaderData($logo, 10 * $ratio, html_entity_decode($tpl_title[1], ENT_QUOTES
, 'UTF-8'), strip_tags($tpl_site_name[1]));
424 * Format the TCPDF page settings (margins, etc)
427 * current TCPDF object
428 * @see theme_print_pdf_tcpdf_page()
430 function theme_print_pdf_tcpdf_page(&$pdf) {
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);
444 * Format the TCPDF page content
447 * current TCPDF object
449 * contents of the body of the HTML from the original node
451 * array with the font definition (font name, styles and size)
452 * @see theme_print_pdf_tcpdf_content()
454 function theme_print_pdf_tcpdf_content(&$pdf, &$html, $font) {
456 $pdf->setFont($font[0], $font[1], $font[2]);
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]);
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]);
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]);
472 $matches[1] = preg_replace('!(</span>)<br />(\s*?</span><br />)!s', '$1$2', $matches[1]);
473 } while ($prev != $matches[1]);
475 @
$pdf->writeHTML($matches[1]);
481 * Format the TCPDF footer contents
484 * current TCPDF object
486 * contents of the body of the HTML from the original node
488 * array with the font definition (font name, styles and size)
489 * @see theme_print_pdf_tcpdf_footer()
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]));
497 $pdf->setFooterFont($font);
499 $pdf->SetFooterMargin(10);
501 $pdf->SetFooterData($footer);
507 * Format the TCPDF footer layout
510 * current TCPDF object
511 * @see theme_print_pdf_tcpdf_footer2()
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
, '');
517 $ormargins = $pdf->getOriginalMargins();
518 $pagenumtxt = t('Page !n of !total', array('!n' => $pdf->PageNo(), '!total' => $pdf->getAliasNbPages()));
520 if ($pdf->getRTL()) {
521 $pdf->SetX($ormargins['right']);
522 $pdf->Cell(0, 10, $pagenumtxt, 'T', 0, 'L');
525 $pdf->SetX($ormargins['left']);
526 $pdf->Cell(0, 10, $pagenumtxt, 'T', 0, 'R');