5 * Generates the PDF versions of the pages
7 * This file is included by the print_pdf module and includes the
8 * functions that interface with the PDF generation packages.
13 require_once(drupal_get_path('module', 'print') .
'/print.pages.inc');
16 * Generate a PDF version of the printer-friendly page
18 * @see print_controller()
19 * @see _print_pdf_dompdf()
20 * @see _print_pdf_tcpdf()
22 function print_pdf_controller() {
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']);
45 // Send to printer option causes problems with PDF
46 $print['sendtoprinter'] = '';
48 $node = $print['node'];
49 $html = theme('print_page', $print, PRINT_PDF_FORMAT
, $node);
50 $html = drupal_final_markup($html);
52 // Convert the a href elements, to make sure no relative links remain
53 $pattern = '!<(a\s[^>]*?)>!is';
54 $html = preg_replace_callback($pattern, '_print_rewrite_urls', $html);
55 // And make anchor links relative again, to permit in-PDF navigation
56 $html = preg_replace("!${base_url}/". PRINTPDF_PATH .
"/.*?%2523!", '#', $html);
58 $pdf_filename = variable_get('print_pdf_filename', PRINT_PDF_FILENAME_DEFAULT
);
59 if (function_exists('token_replace') && !empty($pdf_filename)) {
60 $pdf_filename = token_replace($pdf_filename, 'node', $node) .
'.pdf';
63 $pdf_filename = str_replace('/', '_', $path) .
'.pdf';
65 if (function_exists('transliteration_clean_filename')) {
66 $pdf_filename = transliteration_clean_filename($pdf_filename, language_default('language'));
69 if (basename($print_pdf_pdf_tool) == 'dompdf_config.inc.php') {
70 _print_pdf_dompdf($print, $html, $pdf_filename);
72 elseif (basename($print_pdf_pdf_tool) == 'tcpdf.php') {
73 _print_pdf_tcpdf($print, $html, $pdf_filename);
75 elseif (substr(basename($print_pdf_pdf_tool, '.exe'), 0, 11) == 'wkhtmltopdf') {
76 _print_pdf_wkhtmltopdf($print, $html, $pdf_filename);
79 return drupal_not_found();
82 $nodepath = (isset($node->path
)) ?
drupal_get_normal_path($node->path
) : 'node/'.
$path;
83 db_query("UPDATE {print_pdf_page_counter} SET totalcount = totalcount + 1, timestamp = %d WHERE path = '%s'", time(), $nodepath);
84 // If we affected 0 rows, this is the first time viewing the node.
85 if (!db_affected_rows()) {
86 // We must create a new row to store counters for the new node.
87 db_query("INSERT INTO {print_pdf_page_counter} (path, totalcount, timestamp) VALUES ('%s', 1, %d)", $nodepath, time());
92 * Convert image paths to the file:// protocol
94 * In some Drupal setups, the use of the 'private' filesystem or Apache's
95 * configuration prevent access to the images of the page. This function
96 * tries to circumnvent those problems by accessing files in the local
100 * contents of the post-processed template already with the node data
101 * @see print_pdf_controller()
103 function _print_pdf_file_access_images($html) {
104 global $base_url, $language;
106 // And converted from private to public paths
107 switch (variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE
)) {
108 case LANGUAGE_NEGOTIATION_PATH_DEFAULT
:
109 case LANGUAGE_NEGOTIATION_PATH
:
110 $lang = $language->language
;
116 $file_downloads = variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC
);
117 if ($file_downloads == FILE_DOWNLOADS_PRIVATE
) {
118 $pattern = "!(<img\s[^>]*?src\s*?=\s*?['\"]?)${base_url}/(?:(?:index.php)?\?q=)?(?:${lang}/)?system/files/([^>]*?>)!is";
119 $replacement = '$1file://'.
realpath(file_directory_path()) .
'/$2';
120 $html = preg_replace($pattern, $replacement, $html);
122 $pattern = "!(<img\s[^>]*?src\s*?=\s*?['\"]?)${base_url}/(?:(?:index.php)?\?q=)?(?:${lang}/)?([^>]*?>)!is";
123 $replacement = '$1file://'.
dirname($_SERVER['SCRIPT_FILENAME']) .
'/$2';
124 $html = preg_replace($pattern, $replacement, $html);
130 * Generate the PDF file using the dompdf library
133 * array containing the configured data
135 * contents of the post-processed template already with the node data
137 * name of the PDF file to be generated
138 * @see print_pdf_controller()
140 function _print_pdf_dompdf($print, $html, $filename) {
141 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT
);
142 $print_pdf_paper_size = variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT
);
143 $print_pdf_page_orientation = variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT
);
144 $print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT
);
146 if (variable_get('print_pdf_autoconfig', PRINT_PDF_AUTOCONFIG_DEFAULT
)) {
147 define("DOMPDF_ENABLE_PHP", FALSE
);
148 define("DOMPDF_ENABLE_REMOTE", TRUE
);
149 define("DOMPDF_TEMP_DIR", file_directory_temp());
150 define("DOMPDF_UNICODE_ENABLED", variable_get('print_pdf_dompdf_unicode', PRINT_PDF_DOMPDF_UNICODE_DEFAULT
));
153 require_once($print_pdf_pdf_tool);
154 if (function_exists('spl_autoload_register')) {
155 spl_autoload_register('DOMPDF_autoload');
158 // Try to use local file access for image files
159 $html = _print_pdf_file_access_images($html);
161 // dompdf seems to have problems with something in system.css so let's not use it
162 $html = preg_replace('!<link.*?modules/system/system.css.*?/>!', '', $html);
164 $url_array = parse_url($print['url']);
166 $protocol = $url_array['scheme'] .
'://';
167 $host = $url_array['host'];
168 $path = dirname($url_array['path']) .
'/';
170 $dompdf = new
DOMPDF();
171 $dompdf->set_base_path($path);
172 $dompdf->set_host($host);
173 $dompdf->set_paper(drupal_strtolower($print_pdf_paper_size), $print_pdf_page_orientation);
174 $dompdf->set_protocol($protocol);
176 // dompdf can't handle footers cleanly, so disable the following
177 // $html = theme('print_pdf_dompdf_footer', $html);
179 // If dompdf Unicode support is disabled, try to convert to ISO-8859-1 and then to HTML entities
180 if (!variable_get('print_pdf_dompdf_unicode', PRINT_PDF_DOMPDF_UNICODE_DEFAULT
)) {
181 // Convert the euro sign to an HTML entity
182 $html = str_replace('€', '€', $html);
184 // Convert from UTF-8 to ISO 8859-1 and then to HTML entities
185 if (function_exists('utf8_decode')) {
186 $html = utf8_decode($html);
188 // iconv fails silently when it encounters something that it doesn't know, so don't use it
189 // else if (function_exists('iconv')) {
190 // $html = iconv('UTF-8', 'ISO-8859-1', $html);
192 elseif (function_exists('mb_convert_encoding')) {
193 $html = mb_convert_encoding($html, 'ISO-8859-1', 'UTF-8');
195 elseif (function_exists('recode_string')) {
196 $html = recode_string('UTF-8..ISO_8859-1', $html);
198 $html = htmlspecialchars_decode(htmlentities($html, ENT_NOQUOTES
, 'ISO-8859-1'), ENT_NOQUOTES
);
201 //must get rid of tbody (dompdf goes into recursion)
202 $html = preg_replace('!<tbody[^>]*?>|</tbody>!i', '', $html);
204 $dompdf->load_html($html);
207 $dompdf->stream($filename, array('Attachment' => ($print_pdf_content_disposition == 2)));
211 * Generate the PDF file using the TCPDF library
214 * array containing the configured data
216 * contents of the post-processed template already with the node data
218 * name of the PDF file to be generated
219 * @see print_pdf_controller()
221 function _print_pdf_tcpdf($print, $html, $filename) {
222 global $base_url, $language;
224 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT
);
225 $print_pdf_paper_size = variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT
);
226 $print_pdf_page_orientation = variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT
);
227 $print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT
);
229 $pdf_tool_path = realpath(dirname($print_pdf_pdf_tool));
231 if (variable_get('print_pdf_autoconfig', PRINT_PDF_AUTOCONFIG_DEFAULT
)) {
232 define('K_TCPDF_EXTERNAL_CONFIG', TRUE
);
233 define('K_PATH_MAIN', dirname($_SERVER['SCRIPT_FILENAME']));
234 define('K_PATH_URL', $base_url);
235 define('K_PATH_FONTS', $pdf_tool_path .
'/fonts/');
236 define('K_PATH_CACHE', $pdf_tool_path .
'/cache/');
237 define('K_PATH_IMAGES', '');
238 define('K_BLANK_IMAGE', $pdf_tool_path .
'/images/_blank.png');
239 define('K_CELL_HEIGHT_RATIO', 1.25);
240 define('K_SMALL_RATIO', 2/3);
243 // Try to use local file access for image files
244 $html = _print_pdf_file_access_images($html);
246 // Decode HTML entities in image filenames
247 $pattern = "!<img\s[^>]*?src\s*?=\s*?['\"]?{$base_url}[^>]*?>!is";
248 $html = preg_replace_callback($pattern, create_function('$matches', 'return html_entity_decode($matches[0], ENT_QUOTES);'), $html);
249 // Remove queries from the image URL
250 $pattern = "!(<img\s[^>]*?src\s*?=\s*?['\"]?{$base_url}[^>]*?)(?:%3F|\?)[\w=&]+([^>]*?>)!is";
251 $html = preg_replace($pattern, '$1$2', $html);
253 require_once($print_pdf_pdf_tool);
254 if (strpos(PDF_PRODUCER
, 'PHP4') === FALSE
) {
255 require_once(drupal_get_path('module', 'print_pdf') .
'/print_pdf.class.inc');
258 require_once(drupal_get_path('module', 'print_pdf') .
'/print_pdf.class_php4.inc');
262 check_plain(variable_get('print_pdf_font_family', PRINT_PDF_FONT_FAMILY_DEFAULT
)),
264 check_plain(variable_get('print_pdf_font_size', PRINT_PDF_FONT_SIZE_DEFAULT
)),
266 $orientation = drupal_strtoupper($print_pdf_page_orientation[0]);
268 // create new PDF document
269 $pdf = new
PrintTCPDF($orientation , 'mm', $print_pdf_paper_size, TRUE
);
271 // set document information
272 $pdf->SetAuthor(strip_tags($print['submitted']));
273 $pdf->SetCreator(variable_get('site_name', 'Drupal'));
274 $pdf->SetTitle(html_entity_decode($print['title'], ENT_QUOTES
, 'UTF-8'));
275 $keys = implode(' ', explode("\n", trim(strip_tags($print['taxonomy']))));
276 $pdf->SetKeywords($keys);
277 $pdf->setPDFVersion('1.6');
278 $pdf-setFontSubsetting(variable_get('print_pdf_tcpdf_subsetting',FALSE
));
280 if ($language->direction
== LANGUAGE_RTL
) {
284 $pdf = theme('print_pdf_tcpdf_header', $pdf, $html, $font);
285 $pdf = theme('print_pdf_tcpdf_footer', $pdf, $html, $font);
286 $pdf = theme('print_pdf_tcpdf_page', $pdf);
288 //initialize document
289 $pdf->AliasNbPages();
294 $pdf = theme('print_pdf_tcpdf_content', $pdf, $html, $font);
296 // reset pointer to the last page
299 // try to recover from any warning/error
302 //Close and output PDF document
303 $output_dest = ($print_pdf_content_disposition == 2) ?
'D' : 'I';
304 $pdf->Output($filename, $output_dest);
308 * Generate the PDF file using wkhtmltopdf
311 * array containing the configured data
313 * contents of the post-processed template already with the node data
315 * name of the PDF file to be generated
316 * @see print_pdf_controller()
318 function _print_pdf_wkhtmltopdf($print, $html, $filename) {
319 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT
);
320 $print_pdf_paper_size = variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT
);
321 $print_pdf_page_orientation = variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT
);
322 $print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT
);
323 $print_pdf_wkhtmltopdf_options = variable_get('print_pdf_wkhtmltopdf_options', PRINT_PDF_WKHTMLTOPDF_OPTIONS
);
327 if (function_exists('token_replace') && !empty($print_pdf_wkhtmltopdf_options)) {
328 $print_pdf_wkhtmltopdf_options = token_replace($print_pdf_wkhtmltopdf_options, 'node', $print['node']);
331 $version = _print_pdf_wkhtmltopdf_version();
333 // 0.10.0 beta2 identifies itself as 0.9.9
334 if (version_compare($version, '0.9.9', '>=')) {
335 $print_pdf_wkhtmltopdf_options = '--disable-local-file-access '.
$print_pdf_wkhtmltopdf_options;
337 elseif (version_compare($version, '0.9.6', '>=')) {
338 $print_pdf_wkhtmltopdf_options = '--disallow-local-file-access '.
$print_pdf_wkhtmltopdf_options;
341 drupal_goto($print['url']);
345 $descriptor = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
346 $cmd = realpath($print_pdf_pdf_tool) .
" --page-size $print_pdf_paper_size --orientation $print_pdf_page_orientation --dpi $dpi $print_pdf_wkhtmltopdf_options - -";
348 $process = proc_open($cmd, $descriptor, $pipes, NULL
, NULL
);
350 if (is_resource($process)) {
351 fwrite($pipes[0], $html);
354 $pdf = stream_get_contents($pipes[1]);
357 stream_set_blocking($pipes[2], 0);
358 $error = stream_get_contents($pipes[2]);
359 if (!empty($error)) {
360 watchdog('print_pdf', 'wkhtmltopdf: '.
$error);
364 $retval = proc_terminate($process);
368 if (headers_sent()) {
369 die("Unable to stream pdf: headers already sent");
371 header("Cache-Control: private");
372 header("Content-Type: application/pdf");
374 $attachment = ($print_pdf_content_disposition == 2) ?
"attachment" : "inline";
376 header("Content-Disposition: $attachment; filename=\"$filename\"");
382 drupal_goto($print['url']);
388 * Format the dompdf footer contents
391 * contents of the body of the HTML from the original node
392 * @see theme_print_pdf_tcpdf_footer()
394 function theme_print_pdf_dompdf_footer($html) {
395 preg_match('!<div class="print-footer">(.*?)</div>!si', $html, $tpl_footer);
396 $html = str_replace($tpl_footer[0], '', $html);
398 $text = '<script type="text/php">
400 $font = Font_Metrics::get_font("verdana");;
402 $color = array(0,0,0);
403 $text_height = Font_Metrics::get_font_height($font, $size);
405 $w = $pdf->get_width();
406 $h = $pdf->get_height();
408 $footer = $pdf->open_object();
410 // Draw a line along the bottom
412 $pdf->line(15, $y, $w - 15, $y, $color, 1);
414 $y += $text_height / 2;
415 $pdf->page_text(15, $y, \''.
addslashes(strip_tags($tpl_footer[1])) .
'\', $font, $size, $color);
417 $pdf->close_object();
418 $pdf->add_object($footer, "all");
421 $width = Font_Metrics::get_text_width("Page 1 of 2", $font, $size);
422 $pagenumtxt = t("Page !n of !total", array("!n" => "{PAGE_NUM}", "!total" => "{PAGE_COUNT}"));
423 $pdf->page_text($w - 15 - $width, $y, $pagenumtxt, $font, $size, $color);
427 return str_replace("<body>", "<body>" .
$text, $html);
431 * Format the TCPDF header
434 * current TCPDF object
436 * contents of the body of the HTML from the original node
438 * array with the font definition (font name, styles and size)
439 * @see theme_print_pdf_tcpdf_header()
441 function theme_print_pdf_tcpdf_header($pdf, $html, $font) {
442 preg_match('!<div class="print-logo">(.*?)</div>!si', $html, $tpl_logo);
443 preg_match('!<h1 class="print-title">(.*?)</h1>!si', $html, $tpl_title);
444 preg_match('!<div class="print-site_name">(.*?)</div>!si', $html, $tpl_site_name);
448 $logo_ret = preg_match('!src\s*=\s*(\'.*?\'|".*?"|[^\s]*)!i', $tpl_logo[1], $matches);
450 $logo = trim($matches[1], '\'"');
451 $size = getimagesize($logo);
452 $ratio = $size ?
($size[0] / $size[1]) : 0;
456 $pdf->setHeaderFont($font);
458 $pdf->SetHeaderMargin(5);
460 $pdf->SetHeaderData($logo, 10 * $ratio, html_entity_decode($tpl_title[1], ENT_QUOTES
, 'UTF-8'), strip_tags($tpl_site_name[1]));
466 * Format the TCPDF page settings (margins, etc)
469 * current TCPDF object
470 * @see theme_print_pdf_tcpdf_page()
472 function theme_print_pdf_tcpdf_page($pdf) {
474 $pdf->SetMargins(15, 20, 15);
475 // set auto page breaks
476 $pdf->SetAutoPageBreak(TRUE
, 15);
477 // set image scale factor
478 sscanf(PDF_PRODUCER
, "TCPDF %d.%d.%d", $major, $minor, $build);
479 $imagescale = (($major >= 4) && ($minor >= 6) && ($build >= 2)) ?
1 : 4;
480 $pdf->setImageScale($imagescale);
481 // set image compression quality
482 $pdf->setJPEGQuality(100);
488 * Format the TCPDF page content
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_content()
498 function theme_print_pdf_tcpdf_content($pdf, $html, $font) {
500 $pdf->setFont($font[0], $font[1], $font[2]);
502 preg_match('!<body.*?>(.*)</body>!sim', $html, $matches);
503 $pattern = '!(?:<div class="print-(?:logo|site_name|breadcrumb|footer)">.*?</div>|<hr class="print-hr" />)!si';
504 $matches[1] = preg_replace($pattern, '', $matches[1]);
506 // Make CCK fields look better
507 $matches[1] = preg_replace('!(<div class="field.*?>)\s*!sm', '$1', $matches[1]);
508 $matches[1] = preg_replace('!(<div class="field.*?>.*?</div>)\s*!sm', '$1', $matches[1]);
509 $matches[1] = preg_replace('!<div( class="field-label.*?>.*?)</div>!sm', '<strong$1</strong>', $matches[1]);
511 // Since TCPDF's writeHTML is so bad with <p>, do everything possible to make it look nice
512 $matches[1] = preg_replace('!<(?:p(|\s+.*?)/?|/p)>!i', '<br$1 />', $matches[1]);
513 $matches[1] = str_replace(array('<div', 'div>'), array('<span', 'span><br />'), $matches[1]);
516 $matches[1] = preg_replace('!(</span>)<br />(\s*?</span><br />)!s', '$1$2', $matches[1]);
517 } while ($prev != $matches[1]);
519 @
$pdf->writeHTML($matches[1]);
525 * Format the TCPDF footer contents
528 * current TCPDF object
530 * contents of the body of the HTML from the original node
532 * array with the font definition (font name, styles and size)
533 * @see theme_print_pdf_tcpdf_footer()
535 function theme_print_pdf_tcpdf_footer($pdf, $html, $font) {
536 preg_match('!<div class="print-footer">(.*?)</div>!si', $html, $tpl_footer);
537 $footer = trim(preg_replace('!</?div[^>]*?>!i', '', $tpl_footer[1]));
541 $pdf->setFooterFont($font);
543 $pdf->SetFooterMargin(10);
545 $pdf->SetFooterData($footer);
551 * Format the TCPDF footer layout
554 * current TCPDF object
555 * @see theme_print_pdf_tcpdf_footer2()
557 function theme_print_pdf_tcpdf_footer2($pdf) {
558 //Position at 1.5 cm from bottom
559 $pdf->writeHTMLCell(0, 15, 15, -10, $pdf->footer
, 0, 0, 0, TRUE
, '');
561 $ormargins = $pdf->getOriginalMargins();
562 $pagenumtxt = t('Page !n of !total', array('!n' => $pdf->PageNo(), '!total' => $pdf->getAliasNbPages()));
564 if ($pdf->getRTL()) {
565 $pdf->SetX($ormargins['right']);
566 $pdf->Cell(0, 10, $pagenumtxt, 'T', 0, 'L');
569 $pdf->SetX($ormargins['left']);
570 $pdf->Cell(0, 10, $pagenumtxt, 'T', 0, 'R');