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 // Remove the printpdf/ prefix
30 $path = implode('/', $args);
31 $cid = isset($_GET['comment']) ?
(int)$_GET['comment'] : NULL
;
33 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT
);
35 $print = print_controller($path, $cid, PRINT_PDF_FORMAT
);
36 if ($print === FALSE
) {
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
;
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']);
64 $node = $print['node'];
66 include_once(DRUPAL_ROOT .
'/' .
_print_get_template(PRINT_PDF_FORMAT
, $print['type']));
67 $html = ob_get_contents();
70 // Convert the a href elements
71 $pattern = '!<(a\s[^>]*?)>!is';
72 $html = preg_replace_callback($pattern, '_print_rewrite_urls', $html);
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';
79 $pdf_filename = str_replace('/', '_', $path) .
'.pdf';
81 if (basename($print_pdf_pdf_tool) == 'dompdf_config.inc.php') {
82 _print_pdf_dompdf($print, $html, $pdf_filename);
84 elseif (basename($print_pdf_pdf_tool) == 'tcpdf.php') {
85 _print_pdf_tcpdf($print, $html, $pdf_filename);
87 elseif (basename($print_pdf_pdf_tool) == 'wkhtmltopdf') {
88 _print_pdf_wkhtmltopdf($print, $html, $pdf_filename);
91 return drupal_not_found();
94 $nodepath = drupal_get_normal_path($node->path
);
95 db_merge('print_pdf_page_counter')
96 ->key(array('path' => $nodepath))
99 'timestamp' => REQUEST_TIME
,
101 ->expression('totalcount', 'totalcount + :inc', array(':inc' => 1))
106 * Generate the PDF file using the dompdf library
109 * array containing the configured data
111 * contents of the post-processed template already with the node data
113 * name of the PDF file to be generated
114 * @see print_pdf_controller()
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);
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);
126 $url_array = parse_url($print['url']);
128 $protocol = $url_array['scheme'] .
'://';
129 $host = $url_array['host'];
130 $path = dirname($url_array['path']) .
'/';
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);
138 // dompdf can't handle footers cleanly, so disable the following
139 // $html = theme('print_pdf_dompdf_footer', $html);
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);
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);
149 elseif (function_exists('mb_convert_encoding')) {
150 $html = mb_convert_encoding($html, 'ISO-8859-1', 'UTF-8');
152 elseif (function_exists('recode_string')) {
153 $html = recode_string('UTF-8..ISO_8859-1', $html);
155 $html = htmlspecialchars_decode(htmlentities($html, ENT_NOQUOTES
, 'ISO-8859-1'), ENT_NOQUOTES
);
157 //must get rid of tbody (dompdf goes into recursion)
158 $html = preg_replace('!<tbody[^>]*?>|</tbody>!i', '', $html);
160 $dompdf->load_html($html);
163 $dompdf->stream($filename, array('Attachment' => ($print_pdf_content_disposition == 2)));
167 * Generate the PDF file using the TCPDF library
170 * array containing the configured data
172 * contents of the post-processed template already with the node data
174 * name of the PDF file to be generated
175 * @see print_pdf_controller()
177 function _print_pdf_tcpdf($print, $html, $filename) {
178 global $base_url, $language;
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
);
185 $pdf_tool_path = realpath(dirname($print_pdf_pdf_tool));
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);
197 $pattern = "!<img[^>]*?>!is";
198 $html = preg_replace_callback($pattern, create_function('$matches', 'return html_entity_decode($matches[0], ENT_QUOTES);'), $html);
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');
205 drupal_set_message(t("The PHP4 version of TCPDF is not supported. Please upgrade it."), 'error', FALSE
);
206 drupal_goto($_GET['q']);
210 variable_get('print_pdf_font_family', PRINT_PDF_FONT_FAMILY_DEFAULT
),
212 variable_get('print_pdf_font_size', PRINT_PDF_FONT_SIZE_DEFAULT
),
214 $orientation = drupal_strtoupper($print_pdf_page_orientation[0]);
216 // create new PDF document
217 $pdf = new
PrintTCPDF($orientation , 'mm', $print_pdf_paper_size, TRUE
);
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');
227 if (defined('LANGUAGE_RTL') && $language->direction
== LANGUAGE_RTL
) {
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);
235 //initialize document
236 $pdf->AliasNbPages();
241 $pdf = theme('print_pdf_tcpdf_content', $pdf, $html, $font);
243 // reset pointer to the last page
246 // try to recover from any warning/error
249 //Close and output PDF document
250 $output_dest = ($print_pdf_content_disposition == 2) ?
'D' : 'I';
251 $pdf->Output($filename, $output_dest);
255 * Generate the PDF file using wkhtmltopdf
258 * array containing the configured data
260 * contents of the post-processed template already with the node data
262 * name of the PDF file to be generated
263 * @see print_pdf_controller()
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
);
271 $xvfb_binary = key(file_scan_directory(drupal_get_path('module', 'print'), '!^Xvfb$!'));
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');
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);
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
: '');
288 $xdescriptor = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
289 $xprocess = proc_open($xcmd, $xdescriptor, $xpipes, NULL
, NULL
);
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
: '') .
' - -';
296 $process = proc_open($cmd, $descriptor, $pipes, NULL
, isset($xvfb_binary) ?
array('DISPLAY' => ':' .
$xdisplay) : NULL
);
298 if (is_resource($process)) {
299 fwrite($pipes[0], $html);
302 $pdf = stream_get_contents($pipes[1]);
305 $status = proc_get_status($process);
306 if (!$status['running']) {
307 watchdog('print_pdf', 'wkhtmltopdf: ' .
stream_get_contents($pipes[2]));
310 $retval = proc_close($process);
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]));
317 proc_close($xprocess);
321 if (headers_sent()) {
322 die("Unable to stream pdf: headers already sent");
324 header("Cache-Control: private");
325 header("Content-Type: application/pdf");
327 $attachment = ($print_pdf_content_disposition == 2) ?
"attachment" : "inline";
329 header("Content-Disposition: $attachment; filename=\"$filename\"");
335 drupal_goto($print['url']);
341 * Format the dompdf footer contents
344 * contents of the body of the HTML from the original node
345 * @see theme_print_pdf_tcpdf_footer()
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);
351 $text = '<script type="text/php">
353 $font = Font_Metrics::get_font("verdana");;
355 $color = array(0,0,0);
356 $text_height = Font_Metrics::get_font_height($font, $size);
358 $w = $pdf->get_width();
359 $h = $pdf->get_height();
361 $footer = $pdf->open_object();
363 // Draw a line along the bottom
365 $pdf->line(15, $y, $w - 15, $y, $color, 1);
367 $y += $text_height / 2;
368 $pdf->page_text(15, $y, \'' .
addslashes(strip_tags($tpl_footer[1])) .
'\', $font, $size, $color);
370 $pdf->close_object();
371 $pdf->add_object($footer, "all");
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);
380 return str_replace("<body>", "<body>" .
$text, $html);
384 * Format the TCPDF header
387 * current TCPDF object
389 * contents of the body of the HTML from the original node
391 * array with the font definition (font name, styles and size)
392 * @see theme_print_pdf_tcpdf_header()
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);
401 $logo_ret = preg_match('!src\s*=\s*(\'.*?\'|".*?"|[^\s]*)!i', $tpl_logo[1], $matches);
403 $logo = trim($matches[1], '\'"');
404 $size = getimagesize($logo);
405 $ratio = $size ?
($size[0] / $size[1]) : 0;
409 $pdf->setHeaderFont($font);
411 $pdf->SetHeaderMargin(5);
413 $pdf->SetHeaderData($logo, 10 * $ratio, $tpl_title[1], strip_tags($tpl_site_name[1]));
419 * Format the TCPDF page settings (margins, etc)
422 * current TCPDF object
423 * @see theme_print_pdf_tcpdf_page()
425 function theme_print_pdf_tcpdf_page(&$pdf) {
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);
439 * Format the TCPDF page content
442 * current TCPDF object
444 * contents of the body of the HTML from the original node
446 * array with the font definition (font name, styles and size)
447 * @see theme_print_pdf_tcpdf_content()
449 function theme_print_pdf_tcpdf_content(&$pdf, &$html, $font) {
451 $pdf->setFont($font[0], $font[1], $font[2]);
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]);
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]);
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]);
467 $matches[1] = preg_replace('!(</span>)<br />(\s*?</span><br />)!s', '$1$2', $matches[1]);
468 } while ($prev != $matches[1]);
470 @
$pdf->writeHTML($matches[1]);
476 * Format the TCPDF footer contents
479 * current TCPDF object
481 * contents of the body of the HTML from the original node
483 * array with the font definition (font name, styles and size)
484 * @see theme_print_pdf_tcpdf_footer()
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]));
492 $pdf->setFooterFont($font);
494 $pdf->SetFooterMargin(10);
496 $pdf->SetFooterData($footer);
502 * Format the TCPDF footer layout
505 * current TCPDF object
506 * @see theme_print_pdf_tcpdf_footer2()
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
, '');
512 $ormargins = $pdf->getOriginalMargins();
513 $pagenumtxt = t('Page !n of !total', array('!n' => $pdf->PageNo(), '!total' => $pdf->getAliasNbPages()));
515 if ($pdf->getRTL()) {
516 $pdf->SetX($ormargins['right']);
517 $pdf->Cell(0, 10, $pagenumtxt, 'T', 0, 'L');
520 $pdf->SetX($ormargins['left']);
521 $pdf->Cell(0, 10, $pagenumtxt, 'T', 0, 'R');