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.
14 require_once(DRUPAL_ROOT .
'/' .
drupal_get_path('module', 'print') .
'/print.pages.inc');
17 * Generate a PDF version of the printer-friendly page
19 * @see print_controller()
20 * @see _print_get_template()
21 * @see _print_pdf_dompdf()
22 * @see _print_pdf_tcpdf()
24 function print_pdf_controller() {
25 global $base_url, $language;
27 // Disable caching for generated PDFs, as Drupal doesn't ouput the proper headers from the cache
28 $GLOBALS['conf']['cache'] = FALSE
;
30 $args = func_get_args();
31 $path = implode('/', $args);
32 $cid = isset($_GET['comment']) ?
(int)$_GET['comment'] : NULL
;
34 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT
);
36 $print = print_controller($path, $cid, PRINT_PDF_FORMAT
);
37 if ($print === FALSE
) {
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
;
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']);
64 // Send to printer option causes problems with PDF
65 $print['sendtoprinter'] = '';
67 $node = $print['node'];
69 include_once(DRUPAL_ROOT .
'/' .
_print_get_template(PRINT_PDF_FORMAT
, $print['type']));
70 $html = ob_get_contents();
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);
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';
84 $pdf_filename = str_replace('/', '_', $path) .
'.pdf';
86 if (basename($print_pdf_pdf_tool) == 'dompdf_config.inc.php') {
87 _print_pdf_dompdf($print, $html, $pdf_filename);
89 elseif (basename($print_pdf_pdf_tool) == 'tcpdf.php') {
90 _print_pdf_tcpdf($print, $html, $pdf_filename);
92 elseif (substr(basename($print_pdf_pdf_tool, '.exe'), 0, 11) == 'wkhtmltopdf') {
93 _print_pdf_wkhtmltopdf($print, $html, $pdf_filename);
96 return drupal_not_found();
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))
104 'timestamp' => REQUEST_TIME
,
106 ->expression('totalcount', 'totalcount + :inc', array(':inc' => 1))
111 * Generate the PDF file using the dompdf library
114 * array containing the configured data
116 * contents of the post-processed template already with the node data
118 * name of the PDF file to be generated
119 * @see print_pdf_controller()
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
);
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());
133 require_once(DRUPAL_ROOT .
'/' .
$print_pdf_pdf_tool);
134 spl_autoload_register('DOMPDF_autoload');
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);
139 $url_array = parse_url($print['url']);
141 $protocol = $url_array['scheme'] .
'://';
142 $host = $url_array['host'];
143 $path = dirname($url_array['path']) .
'/';
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);
151 // dompdf can't handle footers cleanly, so disable the following
152 // $html = theme('print_pdf_dompdf_footer', $html);
154 // Convert the euro sign to an HTML entity
155 $html = str_replace('€', '€', $html);
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);
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);
165 elseif (function_exists('mb_convert_encoding')) {
166 $html = mb_convert_encoding($html, 'ISO-8859-1', 'UTF-8');
168 elseif (function_exists('recode_string')) {
169 $html = recode_string('UTF-8..ISO_8859-1', $html);
171 $html = htmlspecialchars_decode(htmlentities($html, ENT_NOQUOTES
, 'ISO-8859-1'), ENT_NOQUOTES
);
173 //must get rid of tbody (dompdf goes into recursion)
174 $html = preg_replace('!<tbody[^>]*?>|</tbody>!i', '', $html);
176 $dompdf->load_html($html);
179 $dompdf->stream($filename, array('Attachment' => ($print_pdf_content_disposition == 2)));
183 * Generate the PDF file using the TCPDF library
186 * array containing the configured data
188 * contents of the post-processed template already with the node data
190 * name of the PDF file to be generated
191 * @see print_pdf_controller()
193 function _print_pdf_tcpdf($print, $html, $filename) {
194 global $base_url, $language;
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
);
201 $pdf_tool_path = realpath(dirname($print_pdf_pdf_tool));
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);
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);
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');
227 drupal_set_message(t("The PHP4 version of TCPDF is not supported. Please upgrade it."), 'error', FALSE
);
228 drupal_goto($_GET['q']);
232 check_plain(variable_get('print_pdf_font_family', PRINT_PDF_FONT_FAMILY_DEFAULT
)),
234 check_plain(variable_get('print_pdf_font_size', PRINT_PDF_FONT_SIZE_DEFAULT
)),
236 $orientation = drupal_strtoupper($print_pdf_page_orientation[0]);
238 // create new PDF document
239 $pdf = new
PrintTCPDF($orientation , 'mm', $print_pdf_paper_size, TRUE
);
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');
249 if (defined('LANGUAGE_RTL') && $language->direction
== LANGUAGE_RTL
) {
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);
257 //initialize document
258 $pdf->AliasNbPages();
263 $pdf = theme('print_pdf_tcpdf_content', $pdf, $html, $font);
265 // reset pointer to the last page
268 // try to recover from any warning/error
271 //Close and output PDF document
272 $output_dest = ($print_pdf_content_disposition == 2) ?
'D' : 'I';
273 $pdf->Output($filename, $output_dest);
277 * Generate the PDF file using wkhtmltopdf
280 * array containing the configured data
282 * contents of the post-processed template already with the node data
284 * name of the PDF file to be generated
285 * @see print_pdf_controller()
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
);
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']);
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";
304 $process = proc_open($cmd, $descriptor, $pipes, NULL
, NULL
);
306 if (is_resource($process)) {
307 fwrite($pipes[0], $html);
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);
316 $pdf = stream_get_contents($pipes[1]);
320 stream_set_blocking($pipes[2], 0);
321 $error = stream_get_contents($pipes[2]);
322 if (!empty($error)) {
323 watchdog('print_pdf', 'wkhtmltopdf: ' .
$error);
327 $retval = proc_terminate($process);
331 if (headers_sent()) {
332 die("Unable to stream pdf: headers already sent");
334 header("Cache-Control: private");
335 header("Content-Type: application/pdf");
337 $attachment = ($print_pdf_content_disposition == 2) ?
"attachment" : "inline";
339 header("Content-Disposition: $attachment; filename=\"$filename\"");
345 drupal_goto($print['url']);
351 * Format the dompdf footer contents
354 * contents of the body of the HTML from the original node
355 * @see theme_print_pdf_tcpdf_footer()
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);
361 $text = '<script type="text/php">
363 $font = Font_Metrics::get_font("verdana");;
365 $color = array(0,0,0);
366 $text_height = Font_Metrics::get_font_height($font, $size);
368 $w = $pdf->get_width();
369 $h = $pdf->get_height();
371 $footer = $pdf->open_object();
373 // Draw a line along the bottom
375 $pdf->line(15, $y, $w - 15, $y, $color, 1);
377 $y += $text_height / 2;
378 $pdf->page_text(15, $y, \'' .
addslashes(strip_tags($tpl_footer[1])) .
'\', $font, $size, $color);
380 $pdf->close_object();
381 $pdf->add_object($footer, "all");
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);
390 return str_replace("<body>", "<body>" .
$text, $html);
394 * Format the TCPDF header
397 * current TCPDF object
399 * contents of the body of the HTML from the original node
401 * array with the font definition (font name, styles and size)
402 * @see theme_print_pdf_tcpdf_header()
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);
411 $logo_ret = preg_match('!src\s*=\s*(\'.*?\'|".*?"|[^\s]*)!i', $tpl_logo[1], $matches);
413 $logo = trim($matches[1], '\'"');
414 $size = getimagesize($logo);
415 $ratio = $size ?
($size[0] / $size[1]) : 0;
419 $pdf->setHeaderFont($font);
421 $pdf->SetHeaderMargin(5);
423 $pdf->SetHeaderData($logo, 10 * $ratio, html_entity_decode($tpl_title[1], ENT_QUOTES
, 'UTF-8'), strip_tags($tpl_site_name[1]));
429 * Format the TCPDF page settings (margins, etc)
432 * current TCPDF object
433 * @see theme_print_pdf_tcpdf_page()
435 function theme_print_pdf_tcpdf_page(&$pdf) {
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);
451 * Format the TCPDF page content
454 * current TCPDF object
456 * contents of the body of the HTML from the original node
458 * array with the font definition (font name, styles and size)
459 * @see theme_print_pdf_tcpdf_content()
461 function theme_print_pdf_tcpdf_content(&$pdf, &$html, $font) {
463 $pdf->setFont($font[0], $font[1], $font[2]);
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]);
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]);
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]);
479 $matches[1] = preg_replace('!(</span>)<br />(\s*?</span><br />)!s', '$1$2', $matches[1]);
480 } while ($prev != $matches[1]);
482 @
$pdf->writeHTML($matches[1]);
488 * Format the TCPDF footer contents
491 * current TCPDF object
493 * contents of the body of the HTML from the original node
495 * array with the font definition (font name, styles and size)
496 * @see theme_print_pdf_tcpdf_footer()
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]));
504 $pdf->setFooterFont($font);
506 $pdf->SetFooterMargin(10);
508 $pdf->SetFooterData($footer);
514 * Format the TCPDF footer layout
517 * current TCPDF object
518 * @see theme_print_pdf_tcpdf_footer2()
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
, '');
524 $ormargins = $pdf->getOriginalMargins();
525 $pagenumtxt = t('Page !n of !total', array('!n' => $pdf->PageNo(), '!total' => $pdf->getAliasNbPages()));
527 if ($pdf->getRTL()) {
528 $pdf->SetX($ormargins['right']);
529 $pdf->Cell(0, 10, $pagenumtxt, 'T', 0, 'L');
532 $pdf->SetX($ormargins['left']);
533 $pdf->Cell(0, 10, $pagenumtxt, 'T', 0, 'R');