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_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(_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($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($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($print_pdf_pdf_tool);
141 if (strpos(PDF_PRODUCER
, 'PHP4') === FALSE
) {
142 require_once(drupal_get_path('module', 'print_pdf') .
'/print_pdf.class.inc');
145 // Use only the PHP5 version of TCPDF
149 variable_get('print_pdf_font_family', PRINT_PDF_FONT_FAMILY_DEFAULT
),
151 variable_get('print_pdf_font_size', PRINT_PDF_FONT_SIZE_DEFAULT
),
153 $orientation = drupal_strtoupper($print_pdf_page_orientation[0]);
155 // create new PDF document
156 $pdf = new
PrintTCPDF($orientation , 'mm', $print_pdf_paper_size, TRUE
);
158 // set document information
159 $pdf->SetAuthor(strip_tags($print['submitted']));
160 $pdf->SetCreator(variable_get('site_name', 'Drupal'));
161 $pdf->SetTitle($print['title']);
162 $keys = implode(' ', explode("\n", trim(strip_tags($print['taxonomy']))));
163 $pdf->SetKeywords($keys);
164 $pdf->setPDFVersion('1.6');
166 $pdf = theme('print_pdf_tcpdf_header', $pdf, $html, $font);
167 $pdf = theme('print_pdf_tcpdf_footer', $pdf, $html, $font);
168 $pdf = theme('print_pdf_tcpdf_page', $pdf);
170 //initialize document
171 $pdf->AliasNbPages();
176 $pdf = theme('print_pdf_tcpdf_content', $pdf, $html, $font);
178 // reset pointer to the last page
181 //Close and output PDF document
182 $output_dest = ($print_pdf_content_disposition == 2) ?
'D' : 'I';
183 $pdf->Output($filename, $output_dest);
187 * Format the dompdf footer contents
190 * contents of the body of the HTML from the original node
191 * @see theme_print_pdf_tcpdf_footer()
193 function theme_print_pdf_dompdf_footer(&$html) {
194 preg_match('!<div class="print-footer">(.*?)</div>!si', $html, $tpl_footer);
195 $html = str_replace($tpl_footer[0], '', $html);
197 $text = '<script type="text/php">
199 $font = Font_Metrics::get_font("verdana");;
201 $color = array(0,0,0);
202 $text_height = Font_Metrics::get_font_height($font, $size);
204 $w = $pdf->get_width();
205 $h = $pdf->get_height();
207 $footer = $pdf->open_object();
209 // Draw a line along the bottom
211 $pdf->line(15, $y, $w - 15, $y, $color, 1);
213 $y += $text_height / 2;
214 $pdf->page_text(15, $y, \''.
addslashes(strip_tags($tpl_footer[1])) .
'\', $font, $size, $color);
216 $pdf->close_object();
217 $pdf->add_object($footer, "all");
220 $width = Font_Metrics::get_text_width("Page 1 of 2", $font, $size);
221 $pagenumtxt = t("Page !n of !total", array("!n" => "{PAGE_NUM}", "!total" => "{PAGE_COUNT}"));
222 $pdf->page_text($w - 15 - $width, $y, $pagenumtxt, $font, $size, $color);
226 return str_replace("<body>", "<body>" .
$text, $html);
230 * Format the TCPDF header
233 * current TCPDF object
235 * contents of the body of the HTML from the original node
237 * array with the font definition (font name, styles and size)
238 * @see theme_print_pdf_tcpdf_header()
240 function theme_print_pdf_tcpdf_header(&$pdf, &$html, $font) {
241 preg_match('!<div class="print-logo">(.*?)</div>!si', $html, $tpl_logo);
242 preg_match('!<h1 class="print-title">(.*?)</h1>!si', $html, $tpl_title);
243 preg_match('!<div class="print-site_name">(.*?)</div>!si', $html, $tpl_site_name);
247 $logo_ret = preg_match('!src\s*=\s*(\'.*?\'|".*?"|[^\s]*)!i', $tpl_logo[1], $matches);
249 $logo = trim($matches[1], '\'"');
250 $size = getimagesize($logo);
251 $ratio = $size[0] / $size[1];
255 $pdf->setHeaderFont($font);
257 $pdf->SetHeaderMargin(5);
259 $pdf->SetHeaderData($logo, 10 * $ratio, $tpl_title[1], strip_tags($tpl_site_name[1]));
265 * Format the TCPDF page settings (margins, etc)
268 * current TCPDF object
269 * @see theme_print_pdf_tcpdf_page()
271 function theme_print_pdf_tcpdf_page(&$pdf) {
273 $pdf->SetMargins(15, 20, 15);
274 // set auto page breaks
275 $pdf->SetAutoPageBreak(TRUE
, 15);
276 // set image scale factor
277 $pdf->setImageScale(4);
278 // set image compression quality
279 $pdf->setJPEGQuality(100);
285 * Format the TCPDF page content
288 * current TCPDF object
290 * contents of the body of the HTML from the original node
292 * array with the font definition (font name, styles and size)
293 * @see theme_print_pdf_tcpdf_content()
295 function theme_print_pdf_tcpdf_content(&$pdf, &$html, $font) {
297 $pdf->setFont($font[0], $font[1], $font[2]);
299 preg_match('!<body.*?>(.*)</body>!sim', $html, $matches);
300 $pattern = '!(?:<div class="print-(?:logo|site_name|breadcrumb|footer)">.*?</div>|<hr class="print-hr" />)!si';
301 $matches[1] = preg_replace($pattern, '', $matches[1]);
304 // Since TCPDF's writeHTML is so bad with <p>, do everything possible to make it look nice
305 $matches[1] = preg_replace('!(<p\s*/>|</p>)!i', '<br />', $matches[1]);
306 $matches[1] = str_replace(array('<div', 'div>'), array('<span', 'span><br />'), $matches[1]);
308 $pdf->writeHTML($matches[1]);
314 * Format the TCPDF footer contents
317 * current TCPDF object
319 * contents of the body of the HTML from the original node
321 * array with the font definition (font name, styles and size)
322 * @see theme_print_pdf_tcpdf_footer()
324 function theme_print_pdf_tcpdf_footer(&$pdf, &$html, $font) {
325 preg_match('!<div class="print-footer">(.*?)</div>!si', $html, $tpl_footer);
326 $footer = trim(preg_replace('!</?div[^>]*?>!i', '', $tpl_footer[1]));
330 $pdf->setFooterFont($font);
332 $pdf->SetFooterMargin(10);
334 $pdf->SetFooterData($footer);
340 * Format the TCPDF footer layout
343 * current TCPDF object
344 * @see theme_print_pdf_tcpdf_footer2()
346 function theme_print_pdf_tcpdf_footer2(&$pdf) {
347 //Position at 1.5 cm from bottom
348 $pdf->writeHTMLCell(0, 15, 15, 0, $pdf->footer
, 0, 0, 0, TRUE
, '');
350 $ormargins = $pdf->getOriginalMargins();
351 $pagenumtxt = t('Page !n of !total', array('!n' => $pdf->PageNo(), '!total' => $pdf->getAliasNbPages()));
353 if ($pdf->getRTL()) {
354 $pdf->SetX($ormargins['right']);
355 $pdf->Cell(0, 10, $pagenumtxt, 'T', 0, 'L');
358 $pdf->SetX($ormargins['left']);
359 $pdf->Cell(0, 10, $pagenumtxt, 'T', 0, 'R');