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() {
25 $args = func_get_args();
26 // Remove the printpdf/ prefix
27 $path = implode('/', $args);
28 $cid = isset($_GET['comment']) ?
(int)$_GET['comment'] : NULL
;
30 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT
);
32 $print = print_controller($path, $cid);
33 if ($print === FALSE
) {
37 // Img elements must be set to absolute
38 $pattern = '!<(img\s[^>]*?)>!is';
39 $print['content'] = preg_replace_callback($pattern, '_print_rewrite_urls', $print['content']);
40 $print['logo'] = preg_replace_callback($pattern, '_print_rewrite_urls', $print['logo']);
41 $print['footer_message'] = preg_replace_callback($pattern, '_print_rewrite_urls', $print['footer_message']);
42 // And converted from private to public paths
43 $file_downloads = variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC
);
44 if ($file_downloads == FILE_DOWNLOADS_PRIVATE
) {
45 $pattern = "!(<img\s[^>]*?src\s*?=\s*?['\"]?${base_url}/)system/files(/[^>]*?>)!is";
46 $replacement = '$1'.
file_directory_path() .
'$2';
47 $print['content'] = preg_replace($pattern, $replacement, $print['content']);
48 $print['logo'] = preg_replace($pattern, $replacement, $print['logo']);
49 $print['footer_message'] = preg_replace($pattern, $replacement, $print['footer_message']);
52 $node = $print['node'];
54 include_once(DRUPAL_ROOT .
'/'.
_print_get_template('pdf', $print['type']));
55 $html = ob_get_contents();
58 if (basename($print_pdf_pdf_tool) == 'dompdf_config.inc.php') {
59 _print_pdf_dompdf($print, $html, $path .
'.pdf');
61 elseif (basename($print_pdf_pdf_tool) == 'tcpdf.php') {
62 _print_pdf_tcpdf($print, $html, $path .
'.pdf');
65 return drupal_not_found();
70 * Generate the PDF file using the dompdf library
73 * array containing the configured data
75 * contents of the post-processed template already with the node data
77 * name of the PDF file to be generated
78 * @see print_pdf_controller()
80 function _print_pdf_dompdf($print, $html, $filename) {
81 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT
);
82 $print_pdf_paper_size = variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT
);
83 $print_pdf_page_orientation = variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT
);
84 $print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT
);
85 require_once(DRUPAL_ROOT .
'/'.
$print_pdf_pdf_tool);
87 // dompdf seems to have problems with something in system.css so let's not use it
88 $html = preg_replace('!<link.*?modules/system/system.css.*?/>!', '', $html);
90 $url_array = parse_url($print['url']);
92 $protocol = $url_array['scheme'] .
'://';
93 $host = $url_array['host'];
94 $path = dirname($url_array['path']) .
'/';
96 $dompdf = new
DOMPDF();
97 $dompdf->set_base_path($path);
98 $dompdf->set_host($host);
99 $dompdf->set_paper(drupal_strtolower($print_pdf_paper_size), $print_pdf_page_orientation);
100 $dompdf->set_protocol($protocol);
102 $html = theme('print_pdf_dompdf_footer', $html);
104 $dompdf->load_html(utf8_decode($html));
107 $dompdf->stream($filename, array('Attachment' => ($print_pdf_content_disposition == 2)));
111 * Generate the PDF file using the TCPDF 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_tcpdf($print, $html, $filename) {
123 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT
);
124 $print_pdf_paper_size = variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT
);
125 $print_pdf_page_orientation = variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT
);
126 $print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT
);
128 $pdf_tool_path = realpath(dirname($print_pdf_pdf_tool));
130 define('K_TCPDF_EXTERNAL_CONFIG', TRUE
);
131 define('K_PATH_MAIN', dirname($_SERVER['SCRIPT_FILENAME']));
132 define('K_PATH_URL', $base_url);
133 define('K_PATH_FONTS', $pdf_tool_path .
'/fonts/');
134 define('K_PATH_CACHE', $pdf_tool_path .
'/cache/');
135 define('K_PATH_IMAGES', '');
136 define('K_BLANK_IMAGE', $pdf_tool_path .
'/images/_blank.png');
137 define('K_CELL_HEIGHT_RATIO', 1.25);
138 define('K_SMALL_RATIO', 2/3);
140 require_once(DRUPAL_ROOT .
'/'.
$print_pdf_pdf_tool);
141 if (strpos(PDF_PRODUCER
, 'PHP4') === FALSE
) {
142 require_once(DRUPAL_ROOT .
'/'.
drupal_get_path('module', 'print_pdf') .
'/print_pdf.class.inc');
145 drupal_set_message(t("The PHP4 version of TCPDF is not supported. Please upgrade it."), 'error', FALSE
);
146 drupal_goto($_GET['q']);
150 variable_get('print_pdf_font_family', PRINT_PDF_FONT_FAMILY_DEFAULT
),
152 variable_get('print_pdf_font_size', PRINT_PDF_FONT_SIZE_DEFAULT
),
154 $orientation = drupal_strtoupper($print_pdf_page_orientation[0]);
156 // create new PDF document
157 $pdf = new
PrintTCPDF($orientation , 'mm', $print_pdf_paper_size, TRUE
);
159 // set document information
160 $pdf->SetAuthor(strip_tags($print['submitted']));
161 $pdf->SetCreator(variable_get('site_name', 'Drupal'));
162 $pdf->SetTitle($print['title']);
163 $keys = implode(' ', explode("\n", trim(strip_tags($print['taxonomy']))));
164 $pdf->SetKeywords($keys);
165 $pdf->setPDFVersion('1.6');
167 $pdf = theme('print_pdf_tcpdf_header', $pdf, $html, $font);
168 $pdf = theme('print_pdf_tcpdf_footer', $pdf, $html, $font);
169 $pdf = theme('print_pdf_tcpdf_page', $pdf);
171 //initialize document
172 $pdf->AliasNbPages();
177 $pdf = theme('print_pdf_tcpdf_content', $pdf, $html, $font);
179 // reset pointer to the last page
182 //Close and output PDF document
183 $output_dest = ($print_pdf_content_disposition == 2) ?
'D' : 'I';
184 $pdf->Output($filename, $output_dest);
188 * Format the dompdf footer contents
191 * contents of the body of the HTML from the original node
192 * @see theme_print_pdf_tcpdf_footer()
194 function theme_print_pdf_dompdf_footer(&$html) {
195 preg_match('!<div class="print-footer">(.*?)</div>!si', $html, $tpl_footer);
196 $html = str_replace($tpl_footer[0], '', $html);
198 $text = '<script type="text/php">
200 $font = Font_Metrics::get_font("verdana");;
202 $color = array(0,0,0);
203 $text_height = Font_Metrics::get_font_height($font, $size);
205 $w = $pdf->get_width();
206 $h = $pdf->get_height();
208 $footer = $pdf->open_object();
210 // Draw a line along the bottom
212 $pdf->line(15, $y, $w - 15, $y, $color, 1);
214 $y += $text_height / 2;
215 $pdf->page_text(15, $y, \''.
addslashes(strip_tags($tpl_footer[1])) .
'\', $font, $size, $color);
217 $pdf->close_object();
218 $pdf->add_object($footer, "all");
221 $width = Font_Metrics::get_text_width("Page 1 of 2", $font, $size);
222 $pagenumtxt = t("Page !n of !total", array("!n" => "{PAGE_NUM}", "!total" => "{PAGE_COUNT}"));
223 $pdf->page_text($w - 15 - $width, $y, $pagenumtxt, $font, $size, $color);
227 return str_replace("<body>", "<body>" .
$text, $html);
231 * Format the TCPDF header
234 * current TCPDF object
236 * contents of the body of the HTML from the original node
238 * array with the font definition (font name, styles and size)
239 * @see theme_print_pdf_tcpdf_header()
241 function theme_print_pdf_tcpdf_header(&$pdf, &$html, $font) {
242 preg_match('!<div class="print-logo">(.*?)</div>!si', $html, $tpl_logo);
243 preg_match('!<h1 class="print-title">(.*?)</h1>!si', $html, $tpl_title);
244 preg_match('!<div class="print-site_name">(.*?)</div>!si', $html, $tpl_site_name);
248 $logo_ret = preg_match('!src\s*=\s*(\'.*?\'|".*?"|[^\s]*)!i', $tpl_logo[1], $matches);
250 $logo = trim($matches[1], '\'"');
251 $size = getimagesize($logo);
252 $ratio = $size[0] / $size[1];
256 $pdf->setHeaderFont($font);
258 $pdf->SetHeaderMargin(5);
260 $pdf->SetHeaderData($logo, 10 * $ratio, $tpl_title[1], strip_tags($tpl_site_name[1]));
266 * Format the TCPDF page settings (margins, etc)
269 * current TCPDF object
270 * @see theme_print_pdf_tcpdf_page()
272 function theme_print_pdf_tcpdf_page(&$pdf) {
274 $pdf->SetMargins(15, 20, 15);
275 // set auto page breaks
276 $pdf->SetAutoPageBreak(TRUE
, 15);
277 // set image scale factor
278 $pdf->setImageScale(4);
279 // set image compression quality
280 $pdf->setJPEGQuality(100);
286 * Format the TCPDF page content
289 * current TCPDF object
291 * contents of the body of the HTML from the original node
293 * array with the font definition (font name, styles and size)
294 * @see theme_print_pdf_tcpdf_content()
296 function theme_print_pdf_tcpdf_content(&$pdf, &$html, $font) {
298 $pdf->setFont($font[0], $font[1], $font[2]);
300 preg_match('!<body.*?>(.*)</body>!sim', $html, $matches);
301 $pattern = '!(?:<div class="print-(?:logo|site_name|breadcrumb|footer)">.*?</div>|<hr class="print-hr" />)!si';
302 $matches[1] = preg_replace($pattern, '', $matches[1]);
305 // Since TCPDF's writeHTML is so bad with <p>, do everything possible to make it look nice
306 $matches[1] = preg_replace('!(<p\s*/>|</p>)!i', '<br />', $matches[1]);
307 $matches[1] = str_replace(array('<div', 'div>'), array('<span', 'span><br />'), $matches[1]);
309 $pdf->writeHTML($matches[1]);
315 * Format the TCPDF footer contents
318 * current TCPDF object
320 * contents of the body of the HTML from the original node
322 * array with the font definition (font name, styles and size)
323 * @see theme_print_pdf_tcpdf_footer()
325 function theme_print_pdf_tcpdf_footer(&$pdf, &$html, $font) {
326 preg_match('!<div class="print-footer">(.*?)</div>!si', $html, $tpl_footer);
327 $footer = trim(preg_replace('!</?div[^>]*?>!i', '', $tpl_footer[1]));
331 $pdf->setFooterFont($font);
333 $pdf->SetFooterMargin(10);
335 $pdf->SetFooterData($footer);
341 * Format the TCPDF footer layout
344 * current TCPDF object
345 * @see theme_print_pdf_tcpdf_footer2()
347 function theme_print_pdf_tcpdf_footer2(&$pdf) {
348 //Position at 1.5 cm from bottom
349 $pdf->writeHTMLCell(0, 15, 15, 0, $pdf->footer
, 0, 0, 0, TRUE
, '');
351 $ormargins = $pdf->getOriginalMargins();
352 $pagenumtxt = t('Page !n of !total', array('!n' => $pdf->PageNo(), '!total' => $pdf->getAliasNbPages()));
354 if ($pdf->getRTL()) {
355 $pdf->SetX($ormargins['right']);
356 $pdf->Cell(0, 10, $pagenumtxt, 'T', 0, 'L');
359 $pdf->SetX($ormargins['left']);
360 $pdf->Cell(0, 10, $pagenumtxt, 'T', 0, 'R');