Fix #258095: Add block with links to PF versions
[project/print.git] / print_pdf / print_pdf.pages.inc
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Generates the PDF versions of the pages
7 *
8 * This file is included by the print_pdf module and includes the
9 * functions that interface with the PDF generation packages.
10 */
11
12 require_once(drupal_get_path('module', 'print') .'/print.pages.inc');
13
14 /**
15 * Generate a PDF version of the printer-friendly page
16 *
17 * @see print_controller()
18 * @see _print_get_template()
19 * @see _print_pdf_dompdf()
20 * @see _print_pdf_tcpdf()
21 */
22 function print_pdf_controller() {
23 global $base_url;
24
25 $args = func_get_args();
26 // Remove the printpdf/ prefix
27 $path = implode('/', $args);
28 $cid = isset($_GET['comment']) ? (int)$_GET['comment'] : NULL;
29
30 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
31
32 $print = print_controller($path, $cid);
33 if ($print === FALSE) {
34 return;
35 }
36
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']);
50 }
51
52 $node = $print['node'];
53 ob_start();
54 include_once(_print_get_template('pdf', $print['type']));
55 $html = ob_get_contents();
56 ob_end_clean();
57
58 if (basename($print_pdf_pdf_tool) == 'dompdf_config.inc.php') {
59 _print_pdf_dompdf($print, $html, $path .'.pdf');
60 }
61 elseif (basename($print_pdf_pdf_tool) == 'tcpdf.php') {
62 _print_pdf_tcpdf($print, $html, $path .'.pdf');
63 }
64 else {
65 return drupal_not_found();
66 }
67 }
68
69 /**
70 * Generate the PDF file using the dompdf library
71 *
72 * @param $print
73 * array containing the configured data
74 * @param $html
75 * contents of the post-processed template already with the node data
76 * @param $filename
77 * name of the PDF file to be generated
78 * @see print_pdf_controller()
79 */
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);
86
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);
89
90 $url_array = parse_url($print['url']);
91
92 $protocol = $url_array['scheme'] .'://';
93 $host = $url_array['host'];
94 $path = dirname($url_array['path']) .'/';
95
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);
101
102 $html = theme('print_pdf_dompdf_footer', $html);
103
104 $dompdf->load_html($html);
105
106 $dompdf->render();
107 $dompdf->stream($filename, array('Attachment' => ($print_pdf_content_disposition == 2)));
108 }
109
110 /**
111 * Generate the PDF file using the TCPDF library
112 *
113 * @param $print
114 * array containing the configured data
115 * @param $html
116 * contents of the post-processed template already with the node data
117 * @param $filename
118 * name of the PDF file to be generated
119 * @see print_pdf_controller()
120 */
121 function _print_pdf_tcpdf($print, $html, $filename) {
122 global $base_url;
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);
127
128 $pdf_tool_path = realpath(dirname($print_pdf_pdf_tool));
129
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);
139
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');
143 }
144 else {
145 // Use only the PHP5 version of TCPDF
146 }
147
148 $font = Array(
149 variable_get('print_pdf_font_family', PRINT_PDF_FONT_FAMILY_DEFAULT),
150 '',
151 variable_get('print_pdf_font_size', PRINT_PDF_FONT_SIZE_DEFAULT),
152 );
153 $orientation = drupal_strtoupper($print_pdf_page_orientation[0]);
154
155 // create new PDF document
156 $pdf = new PrintTCPDF($orientation , 'mm', $print_pdf_paper_size, TRUE);
157
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');
165
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);
169
170 //initialize document
171 $pdf->AliasNbPages();
172
173 // add a page
174 $pdf->AddPage();
175
176 $pdf = theme('print_pdf_tcpdf_content', $pdf, $html, $font);
177
178 // reset pointer to the last page
179 $pdf->lastPage();
180
181 //Close and output PDF document
182 $output_dest = ($print_pdf_content_disposition == 2) ? 'D' : 'I';
183 $pdf->Output($filename, $output_dest);
184 }
185
186 /**
187 * Format the dompdf footer contents
188 *
189 * @param $html
190 * contents of the body of the HTML from the original node
191 * @see theme_print_pdf_tcpdf_footer()
192 */
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);
196
197 $text = '<script type="text/php">
198 if (isset($pdf)) {
199 $font = Font_Metrics::get_font("verdana");;
200 $size = 10;
201 $color = array(0,0,0);
202 $text_height = Font_Metrics::get_font_height($font, $size);
203
204 $w = $pdf->get_width();
205 $h = $pdf->get_height();
206
207 $footer = $pdf->open_object();
208
209 // Draw a line along the bottom
210 $y = $h - 25;
211 $pdf->line(15, $y, $w - 15, $y, $color, 1);
212
213 $y += $text_height / 2;
214 $pdf->page_text(15, $y, \''. addslashes(strip_tags($tpl_footer[1])) .'\', $font, $size, $color);
215
216 $pdf->close_object();
217 $pdf->add_object($footer, "all");
218
219 // Center the text
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);
223 }
224 </script>';
225
226 return str_replace("<body>", "<body>" . $text, $html);
227 }
228
229 /**
230 * Format the TCPDF header
231 *
232 * @param $pdf
233 * current TCPDF object
234 * @param $html
235 * contents of the body of the HTML from the original node
236 * @param $font
237 * array with the font definition (font name, styles and size)
238 * @see theme_print_pdf_tcpdf_header()
239 */
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);
244
245 $ratio = 0;
246 $logo = '';
247 $logo_ret = preg_match('!src\s*=\s*(\'.*?\'|".*?"|[^\s]*)!i', $tpl_logo[1], $matches);
248 if ($logo_ret) {
249 $logo = trim($matches[1], '\'"');
250 $size = getimagesize($logo);
251 $ratio = $size[0] / $size[1];
252 }
253
254 // set header font
255 $pdf->setHeaderFont($font);
256 // set header margin
257 $pdf->SetHeaderMargin(5);
258 // set header data
259 $pdf->SetHeaderData($logo, 10 * $ratio, $tpl_title[1], strip_tags($tpl_site_name[1]));
260
261 return $pdf;
262 }
263
264 /**
265 * Format the TCPDF page settings (margins, etc)
266 *
267 * @param $pdf
268 * current TCPDF object
269 * @see theme_print_pdf_tcpdf_page()
270 */
271 function theme_print_pdf_tcpdf_page(&$pdf) {
272 // set margins
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);
280
281 return $pdf;
282 }
283
284 /**
285 * Format the TCPDF page content
286 *
287 * @param $pdf
288 * current TCPDF object
289 * @param $html
290 * contents of the body of the HTML from the original node
291 * @param $font
292 * array with the font definition (font name, styles and size)
293 * @see theme_print_pdf_tcpdf_content()
294 */
295 function theme_print_pdf_tcpdf_content(&$pdf, &$html, $font) {
296 // set content font
297 $pdf->setFont($font[0], $font[1], $font[2]);
298
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]);
302
303
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]);
307
308 $pdf->writeHTML($matches[1]);
309
310 return $pdf;
311 }
312
313 /**
314 * Format the TCPDF footer contents
315 *
316 * @param $pdf
317 * current TCPDF object
318 * @param $html
319 * contents of the body of the HTML from the original node
320 * @param $font
321 * array with the font definition (font name, styles and size)
322 * @see theme_print_pdf_tcpdf_footer()
323 */
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]));
327
328 // set footer font
329 $font[2] *= 0.8;
330 $pdf->setFooterFont($font);
331 // set footer margin
332 $pdf->SetFooterMargin(10);
333 // set footer data
334 $pdf->SetFooterData($footer);
335
336 return $pdf;
337 }
338
339 /**
340 * Format the TCPDF footer layout
341 *
342 * @param $pdf
343 * current TCPDF object
344 * @see theme_print_pdf_tcpdf_footer2()
345 */
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, '');
349
350 $ormargins = $pdf->getOriginalMargins();
351 $pagenumtxt = t('Page !n of !total', array('!n' => $pdf->PageNo(), '!total' => $pdf->getAliasNbPages()));
352 //Print page number
353 if ($pdf->getRTL()) {
354 $pdf->SetX($ormargins['right']);
355 $pdf->Cell(0, 10, $pagenumtxt, 'T', 0, 'L');
356 }
357 else {
358 $pdf->SetX($ormargins['left']);
359 $pdf->Cell(0, 10, $pagenumtxt, 'T', 0, 'R');
360 }
361
362 return $pdf;
363 }