Make basic printer-friendly version work again in drupal 7
[project/print.git] / print_pdf / print_pdf.pages.inc
CommitLineData
b09f08f7 1<?php
3483245d
JV
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.
d08475b8
JV
10 *
11 * @ingroup print
3483245d 12 */
b09f08f7 13
fe25f54b 14require_once(DRUPAL_ROOT . '/' . drupal_get_path('module', 'print') . '/print.pages.inc');
b09f08f7 15
6a4c89f9
JV
16/**
17 * Generate a PDF version of the printer-friendly page
18 *
19 * @see print_controller()
6a4c89f9
JV
20 * @see _print_pdf_dompdf()
21 * @see _print_pdf_tcpdf()
22 */
b09f08f7 23function print_pdf_controller() {
dd0fda21 24 global $base_url, $language;
e60934b7 25
606acc3b
JV
26 // Disable caching for generated PDFs, as Drupal doesn't ouput the proper headers from the cache
27 $GLOBALS['conf']['cache'] = FALSE;
28
5b1b9562 29 $args = func_get_args();
5b1b9562 30 $path = implode('/', $args);
20ab24e6 31 $cid = isset($_GET['comment']) ? (int)$_GET['comment'] : NULL;
3c90dace 32
7d62e7c4 33 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
b09f08f7 34
855b243a 35 $print = print_controller($path, $cid, PRINT_PDF_FORMAT);
5c9469df
JV
36 if ($print === FALSE) {
37 return;
38 }
39
b09f08f7 40 // Img elements must be set to absolute
7d62e7c4
JV
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']);
e60934b7
JV
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) {
dd0fda21
JV
48 switch (variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE)) {
49 case LANGUAGE_NEGOTIATION_PATH_DEFAULT:
50 case LANGUAGE_NEGOTIATION_PATH:
51 $lang = $language->language;
52 break;
53 default:
54 $lang = '';
55 break;
56 }
57 $pattern = "!(<img\s[^>]*?src\s*?=\s*?['\"]?)${base_url}/(?:(?:index.php)?\?q=)?(?:${lang}/)?system/files(/[^>]*?>)!is";
fe25f54b 58 $replacement = '$1file://' . realpath(file_directory_path()) . '$2';
e60934b7
JV
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']);
62 }
46b7c98d
JV
63 // Send to printer option causes problems with PDF
64 $print['sendtoprinter'] = '';
b09f08f7 65
7d62e7c4 66 $node = $print['node'];
e281ff0b 67 $html = theme('print_page', $print, PRINT_PDF_FORMAT, $node);
1caf9f9d 68
361ee8f2 69 // Convert the a href elements, to make sure no relative links remain
f4236c76
JV
70 $pattern = '!<(a\s[^>]*?)>!is';
71 $html = preg_replace_callback($pattern, '_print_rewrite_urls', $html);
361ee8f2
JV
72 // And make anchor links relative again, to permit in-PDF navigation
73 $html = preg_replace("!${base_url}/" . PRINTPDF_PATH . "/.*?%2523!", '#', $html);
f4236c76 74
40db8370
JV
75 $pdf_filename = variable_get('print_pdf_filename', PRINT_PDF_FILENAME_DEFAULT);
76 if (function_exists('token_replace') && !empty($pdf_filename)) {
fe25f54b 77 $pdf_filename = token_replace($pdf_filename, 'node', $node) . '.pdf';
40db8370
JV
78 }
79 else {
fe25f54b 80 $pdf_filename = str_replace('/', '_', $path) . '.pdf';
40db8370 81 }
93971ee7
JV
82 if (function_exists('transliteration_clean_filename')) {
83 $pdf_filename = transliteration_clean_filename($pdf_filename, language_default('language'));
84 }
85
7d62e7c4 86 if (basename($print_pdf_pdf_tool) == 'dompdf_config.inc.php') {
40db8370 87 _print_pdf_dompdf($print, $html, $pdf_filename);
b09f08f7 88 }
7d62e7c4 89 elseif (basename($print_pdf_pdf_tool) == 'tcpdf.php') {
40db8370 90 _print_pdf_tcpdf($print, $html, $pdf_filename);
b09f08f7 91 }
ca41ee23 92 elseif (substr(basename($print_pdf_pdf_tool, '.exe'), 0, 11) == 'wkhtmltopdf') {
0956bdfb
JV
93 _print_pdf_wkhtmltopdf($print, $html, $pdf_filename);
94 }
b09f08f7
JV
95 else {
96 return drupal_not_found();
97 }
855b243a 98
c8aee091 99 $nodepath = (isset($node->path)) ? drupal_get_normal_path($node->path) : 'node/' . $path;
855b243a
JV
100 db_merge('print_pdf_page_counter')
101 ->key(array('path' => $nodepath))
102 ->fields(array(
103 'totalcount' => 1,
104 'timestamp' => REQUEST_TIME,
105 ))
106 ->expression('totalcount', 'totalcount + :inc', array(':inc' => 1))
107 ->execute();
b09f08f7
JV
108}
109
6a4c89f9
JV
110/**
111 * Generate the PDF file using the dompdf 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 */
1caf9f9d 121function _print_pdf_dompdf($print, $html, $filename) {
7d62e7c4
JV
122 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
123 $print_pdf_paper_size = variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT);
124 $print_pdf_page_orientation = variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT);
125 $print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT);
925d7af0
JV
126
127 if (variable_get('print_pdf_autoconfig', PRINT_PDF_AUTOCONFIG_DEFAULT)) {
128 define("DOMPDF_ENABLE_PHP", FALSE);
129 define("DOMPDF_ENABLE_REMOTE", TRUE);
130 define("DOMPDF_TEMP_DIR", file_directory_temp());
45626194 131 define("DOMPDF_UNICODE_ENABLED", variable_get('print_pdf_dompdf_unicode', PRINT_PDF_DOMPDF_UNICODE_DEFAULT));
925d7af0
JV
132 }
133
fe25f54b 134 require_once(DRUPAL_ROOT . '/' . $print_pdf_pdf_tool);
8f55ca8d 135 spl_autoload_register('DOMPDF_autoload');
b09f08f7 136
893ee70f
JV
137 // dompdf seems to have problems with something in system.css so let's not use it
138 $html = preg_replace('!<link.*?modules/system/system.css.*?/>!', '', $html);
139
56bdd124
JV
140 $url_array = parse_url($print['url']);
141
fe25f54b 142 $protocol = $url_array['scheme'] . '://';
56bdd124 143 $host = $url_array['host'];
fe25f54b 144 $path = dirname($url_array['path']) . '/';
b09f08f7
JV
145
146 $dompdf = new DOMPDF();
56bdd124
JV
147 $dompdf->set_base_path($path);
148 $dompdf->set_host($host);
6a4c89f9 149 $dompdf->set_paper(drupal_strtolower($print_pdf_paper_size), $print_pdf_page_orientation);
56bdd124
JV
150 $dompdf->set_protocol($protocol);
151
cc511103
JV
152// dompdf can't handle footers cleanly, so disable the following
153// $html = theme('print_pdf_dompdf_footer', $html);
b97f7b85 154
45626194
JV
155 // If dompdf Unicode support is disabled, try to convert to ISO-8859-1 and then to HTML entities
156 if (!variable_get('print_pdf_dompdf_unicode', PRINT_PDF_DOMPDF_UNICODE_DEFAULT)) {
62de0d27
JV
157 // Convert the euro sign to an HTML entity
158 $html = str_replace('€', '&#0128;', $html);
159
f76a10f2
JV
160 // Convert from UTF-8 to ISO 8859-1 and then to HTML entities
161 if (function_exists('utf8_decode')) {
162 $html = utf8_decode($html);
163 }
164// iconv fails silently when it encounters something that it doesn't know, so don't use it
165// else if (function_exists('iconv')) {
166// $html = iconv('UTF-8', 'ISO-8859-1', $html);
167// }
ff2fdaa2 168 elseif (function_exists('mb_convert_encoding')) {
f76a10f2
JV
169 $html = mb_convert_encoding($html, 'ISO-8859-1', 'UTF-8');
170 }
ff2fdaa2 171 elseif (function_exists('recode_string')) {
f76a10f2
JV
172 $html = recode_string('UTF-8..ISO_8859-1', $html);
173 }
174 $html = htmlspecialchars_decode(htmlentities($html, ENT_NOQUOTES, 'ISO-8859-1'), ENT_NOQUOTES);
45626194 175 }
d356106f 176
dfb35930 177 //must get rid of tbody (dompdf goes into recursion)
d54079c6 178 $html = preg_replace('!<tbody[^>]*?>|</tbody>!i', '', $html);
dfb35930 179
d356106f 180 $dompdf->load_html($html);
3483245d 181
b09f08f7 182 $dompdf->render();
7d62e7c4 183 $dompdf->stream($filename, array('Attachment' => ($print_pdf_content_disposition == 2)));
b09f08f7
JV
184}
185
6a4c89f9
JV
186/**
187 * Generate the PDF file using the TCPDF library
188 *
189 * @param $print
190 * array containing the configured data
191 * @param $html
192 * contents of the post-processed template already with the node data
193 * @param $filename
194 * name of the PDF file to be generated
195 * @see print_pdf_controller()
196 */
1caf9f9d 197function _print_pdf_tcpdf($print, $html, $filename) {
85cce36d
JV
198 global $base_url, $language;
199
7d62e7c4
JV
200 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
201 $print_pdf_paper_size = variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT);
202 $print_pdf_page_orientation = variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT);
203 $print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT);
204
4817021f
JV
205 $pdf_tool_path = realpath(dirname($print_pdf_pdf_tool));
206
b658d760
JV
207 if (variable_get('print_pdf_autoconfig', PRINT_PDF_AUTOCONFIG_DEFAULT)) {
208 define('K_TCPDF_EXTERNAL_CONFIG', TRUE);
209 define('K_PATH_MAIN', dirname($_SERVER['SCRIPT_FILENAME']));
210 define('K_PATH_URL', $base_url);
211 define('K_PATH_FONTS', $pdf_tool_path . '/fonts/');
212 define('K_PATH_CACHE', $pdf_tool_path . '/cache/');
213 define('K_PATH_IMAGES', '');
214 define('K_BLANK_IMAGE', $pdf_tool_path . '/images/_blank.png');
215 define('K_CELL_HEIGHT_RATIO', 1.25);
216 define('K_SMALL_RATIO', 2/3);
217 }
7d62e7c4 218
903b9638 219 // Decode HTML entities in image filenames
a23e5fdd 220 $pattern = "!<img\s[^>]*?src\s*?=\s*?['\"]?{$base_url}[^>]*?>!is";
ff2fdaa2 221 $html = preg_replace_callback($pattern, create_function('$matches', 'return html_entity_decode($matches[0], ENT_QUOTES);'), $html);
5c4a2f7f 222 // Remove queries from the image URL
a23e5fdd 223 $pattern = "!(<img\s[^>]*?src\s*?=\s*?['\"]?{$base_url}[^>]*?)(?:%3F|\?)[\w=&]+([^>]*?>)!is";
903b9638 224 $html = preg_replace($pattern, '$1$2', $html);
cdb13c56 225
fe25f54b 226 require_once(DRUPAL_ROOT . '/' . $print_pdf_pdf_tool);
e03112ed 227 if (strpos(PDF_PRODUCER, 'PHP4') === FALSE) {
fe25f54b 228 require_once(DRUPAL_ROOT . '/' . drupal_get_path('module', 'print_pdf') . '/print_pdf.class.inc');
90263e69
JV
229 }
230 else {
6936e5f6
JV
231 drupal_set_message(t("The PHP4 version of TCPDF is not supported. Please upgrade it."), 'error', FALSE);
232 drupal_goto($_GET['q']);
90263e69 233 }
7d62e7c4 234
455e82fd 235 $font = Array(
937c5ab6 236 check_plain(variable_get('print_pdf_font_family', PRINT_PDF_FONT_FAMILY_DEFAULT)),
b2a85b2b 237 '',
937c5ab6 238 check_plain(variable_get('print_pdf_font_size', PRINT_PDF_FONT_SIZE_DEFAULT)),
455e82fd 239 );
6a4c89f9 240 $orientation = drupal_strtoupper($print_pdf_page_orientation[0]);
56bdd124
JV
241
242 // create new PDF document
3da84173 243 $pdf = new PrintTCPDF($orientation , 'mm', $print_pdf_paper_size, TRUE);
b09f08f7 244
56bdd124 245 // set document information
7d62e7c4
JV
246 $pdf->SetAuthor(strip_tags($print['submitted']));
247 $pdf->SetCreator(variable_get('site_name', 'Drupal'));
e522c3f2 248 $pdf->SetTitle(html_entity_decode($print['title'], ENT_QUOTES, 'UTF-8'));
7d62e7c4 249 $keys = implode(' ', explode("\n", trim(strip_tags($print['taxonomy']))));
56bdd124 250 $pdf->SetKeywords($keys);
7d62e7c4 251 $pdf->setPDFVersion('1.6');
56bdd124 252
360140fb 253 if ($language->direction == LANGUAGE_RTL) {
85cce36d
JV
254 $pdf->setRTL(TRUE);
255 }
256
1024f54b
JV
257 $pdf = theme('print_pdf_tcpdf_header', $pdf, $html, $font);
258 $pdf = theme('print_pdf_tcpdf_footer', $pdf, $html, $font);
259 $pdf = theme('print_pdf_tcpdf_page', $pdf);
4817021f
JV
260
261 //initialize document
262 $pdf->AliasNbPages();
263
264 // add a page
265 $pdf->AddPage();
266
1024f54b 267 $pdf = theme('print_pdf_tcpdf_content', $pdf, $html, $font);
4817021f
JV
268
269 // reset pointer to the last page
270 $pdf->lastPage();
271
29c45af0
JV
272 // try to recover from any warning/error
273 ob_clean();
274
4817021f
JV
275 //Close and output PDF document
276 $output_dest = ($print_pdf_content_disposition == 2) ? 'D' : 'I';
277 $pdf->Output($filename, $output_dest);
278}
279
6a4c89f9 280/**
0956bdfb
JV
281 * Generate the PDF file using wkhtmltopdf
282 *
283 * @param $print
284 * array containing the configured data
285 * @param $html
286 * contents of the post-processed template already with the node data
287 * @param $filename
288 * name of the PDF file to be generated
289 * @see print_pdf_controller()
290 */
291function _print_pdf_wkhtmltopdf($print, $html, $filename) {
292 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
730917d3 293 $print_pdf_paper_size = drupal_strtolower(variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT));
0956bdfb
JV
294 $print_pdf_page_orientation = variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT);
295 $print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT);
d9a43552 296 $print_pdf_wkhtmltopdf_options = variable_get('print_pdf_wkhtmltopdf_options', PRINT_PDF_WKHTMLTOPDF_OPTIONS);
0956bdfb 297
d9a43552 298 $dpi = 96;
0956bdfb 299
a11de1f7
JV
300 if (function_exists('token_replace') && !empty($print_pdf_wkhtmltopdf_options)) {
301 $print_pdf_wkhtmltopdf_options = token_replace($print_pdf_wkhtmltopdf_options, 'node', $print['node']);
302 }
303
730917d3 304 $descriptor = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
26081159 305 $cmd = realpath($print_pdf_pdf_tool) . " --page-size $print_pdf_paper_size --orientation $print_pdf_page_orientation --dpi $dpi $print_pdf_wkhtmltopdf_options - -";
0956bdfb 306
1e7951a3 307 $process = proc_open($cmd, $descriptor, $pipes, NULL, NULL);
0956bdfb
JV
308
309 if (is_resource($process)) {
310 fwrite($pipes[0], $html);
311 fclose($pipes[0]);
312
26081159 313 $pdf = stream_get_contents($pipes[1]);
0956bdfb
JV
314 fclose($pipes[1]);
315
39ed73e1 316 stream_set_blocking($pipes[2], 0);
04742a90
JV
317 $error = stream_get_contents($pipes[2]);
318 if (!empty($error)) {
319 watchdog('print_pdf', 'wkhtmltopdf: ' . $error);
730917d3 320 }
04742a90 321 fclose($pipes[2]);
730917d3 322
04742a90 323 $retval = proc_terminate($process);
0956bdfb 324 }
0956bdfb 325
a8ffbc1f 326 if (!empty($pdf)) {
0956bdfb
JV
327 if (headers_sent()) {
328 die("Unable to stream pdf: headers already sent");
329 }
330 header("Cache-Control: private");
331 header("Content-Type: application/pdf");
332
333 $attachment = ($print_pdf_content_disposition == 2) ? "attachment" : "inline";
334
335 header("Content-Disposition: $attachment; filename=\"$filename\"");
336
337 echo $pdf;
338 flush();
339 }
730917d3
JV
340 else {
341 drupal_goto($print['url']);
342 exit;
343 }
0956bdfb
JV
344}
345
346/**
b97f7b85
JV
347 * Format the dompdf footer contents
348 *
349 * @param $html
350 * contents of the body of the HTML from the original node
351 * @see theme_print_pdf_tcpdf_footer()
352 */
3ebbb392 353function theme_print_pdf_dompdf_footer($html) {
b97f7b85
JV
354 preg_match('!<div class="print-footer">(.*?)</div>!si', $html, $tpl_footer);
355 $html = str_replace($tpl_footer[0], '', $html);
356
357 $text = '<script type="text/php">
358 if (isset($pdf)) {
359 $font = Font_Metrics::get_font("verdana");;
360 $size = 10;
361 $color = array(0,0,0);
362 $text_height = Font_Metrics::get_font_height($font, $size);
363
364 $w = $pdf->get_width();
365 $h = $pdf->get_height();
366
367 $footer = $pdf->open_object();
368
369 // Draw a line along the bottom
370 $y = $h - 25;
371 $pdf->line(15, $y, $w - 15, $y, $color, 1);
372
373 $y += $text_height / 2;
fe25f54b 374 $pdf->page_text(15, $y, \'' . addslashes(strip_tags($tpl_footer[1])) . '\', $font, $size, $color);
b97f7b85
JV
375
376 $pdf->close_object();
377 $pdf->add_object($footer, "all");
378
379 // Center the text
380 $width = Font_Metrics::get_text_width("Page 1 of 2", $font, $size);
381 $pagenumtxt = t("Page !n of !total", array("!n" => "{PAGE_NUM}", "!total" => "{PAGE_COUNT}"));
382 $pdf->page_text($w - 15 - $width, $y, $pagenumtxt, $font, $size, $color);
383 }
384 </script>';
385
386 return str_replace("<body>", "<body>" . $text, $html);
387}
388
389/**
6a4c89f9
JV
390 * Format the TCPDF header
391 *
392 * @param $pdf
393 * current TCPDF object
394 * @param $html
395 * contents of the body of the HTML from the original node
396 * @param $font
397 * array with the font definition (font name, styles and size)
398 * @see theme_print_pdf_tcpdf_header()
399 */
3ebbb392 400function theme_print_pdf_tcpdf_header($pdf, $html, $font) {
4817021f
JV
401 preg_match('!<div class="print-logo">(.*?)</div>!si', $html, $tpl_logo);
402 preg_match('!<h1 class="print-title">(.*?)</h1>!si', $html, $tpl_title);
403 preg_match('!<div class="print-site_name">(.*?)</div>!si', $html, $tpl_site_name);
404
74df721a 405 $ratio = 0;
4817021f
JV
406 $logo = '';
407 $logo_ret = preg_match('!src\s*=\s*(\'.*?\'|".*?"|[^\s]*)!i', $tpl_logo[1], $matches);
3da84173
JV
408 if ($logo_ret) {
409 $logo = trim($matches[1], '\'"');
4817021f 410 $size = getimagesize($logo);
c64555f4 411 $ratio = $size ? ($size[0] / $size[1]) : 0;
3da84173
JV
412 }
413
4817021f
JV
414 // set header font
415 $pdf->setHeaderFont($font);
416 // set header margin
417 $pdf->SetHeaderMargin(5);
56bdd124 418 // set header data
bd02b7ac 419 $pdf->SetHeaderData($logo, 10 * $ratio, html_entity_decode($tpl_title[1], ENT_QUOTES, 'UTF-8'), strip_tags($tpl_site_name[1]));
1024f54b
JV
420
421 return $pdf;
4817021f 422}
3da84173 423
6a4c89f9
JV
424/**
425 * Format the TCPDF page settings (margins, etc)
426 *
427 * @param $pdf
428 * current TCPDF object
429 * @see theme_print_pdf_tcpdf_page()
430 */
3ebbb392 431function theme_print_pdf_tcpdf_page($pdf) {
b09f08f7 432 // set margins
4817021f 433 $pdf->SetMargins(15, 20, 15);
c67b9ed4 434 // set auto page breaks
4817021f 435 $pdf->SetAutoPageBreak(TRUE, 15);
b09f08f7 436 // set image scale factor
3a24b895
JV
437 sscanf(PDF_PRODUCER, "TCPDF %d.%d.%d", $major, $minor, $build);
438 $imagescale = (($major >= 4) && ($minor >= 6) && ($build >= 2)) ? 1 : 4;
439 $pdf->setImageScale($imagescale);
973f5b72
JV
440 // set image compression quality
441 $pdf->setJPEGQuality(100);
1024f54b
JV
442
443 return $pdf;
4817021f 444}
973f5b72 445
6a4c89f9
JV
446/**
447 * Format the TCPDF page content
448 *
449 * @param $pdf
450 * current TCPDF object
451 * @param $html
452 * contents of the body of the HTML from the original node
453 * @param $font
454 * array with the font definition (font name, styles and size)
455 * @see theme_print_pdf_tcpdf_content()
456 */
3ebbb392 457function theme_print_pdf_tcpdf_content($pdf, $html, $font) {
4817021f
JV
458 // set content font
459 $pdf->setFont($font[0], $font[1], $font[2]);
b09f08f7 460
26673803 461 preg_match('!<body.*?>(.*)</body>!sim', $html, $matches);
4817021f 462 $pattern = '!(?:<div class="print-(?:logo|site_name|breadcrumb|footer)">.*?</div>|<hr class="print-hr" />)!si';
3da84173
JV
463 $matches[1] = preg_replace($pattern, '', $matches[1]);
464
f76a10f2
JV
465 // Make CCK fields look better
466 $matches[1] = preg_replace('!(<div class="field.*?>)\s*!sm', '$1', $matches[1]);
467 $matches[1] = preg_replace('!(<div class="field.*?>.*?</div>)\s*!sm', '$1', $matches[1]);
468 $matches[1] = preg_replace('!<div( class="field-label.*?>.*?)</div>!sm', '<strong$1</strong>', $matches[1]);
469
4817021f 470 // Since TCPDF's writeHTML is so bad with <p>, do everything possible to make it look nice
57b1d44f 471 $matches[1] = preg_replace('!<(?:p(|\s+.*?)/?|/p)>!i', '<br$1 />', $matches[1]);
e83e4f36 472 $matches[1] = str_replace(array('<div', 'div>'), array('<span', 'span><br />'), $matches[1]);
f76a10f2
JV
473 do {
474 $prev = $matches[1];
475 $matches[1] = preg_replace('!(</span>)<br />(\s*?</span><br />)!s', '$1$2', $matches[1]);
476 } while ($prev != $matches[1]);
e83e4f36 477
a01ab7bd 478 @$pdf->writeHTML($matches[1]);
1024f54b
JV
479
480 return $pdf;
4817021f 481}
b09f08f7 482
6a4c89f9
JV
483/**
484 * Format the TCPDF footer contents
485 *
486 * @param $pdf
487 * current TCPDF object
488 * @param $html
489 * contents of the body of the HTML from the original node
490 * @param $font
491 * array with the font definition (font name, styles and size)
492 * @see theme_print_pdf_tcpdf_footer()
493 */
3ebbb392 494function theme_print_pdf_tcpdf_footer($pdf, $html, $font) {
b97f7b85
JV
495 preg_match('!<div class="print-footer">(.*?)</div>!si', $html, $tpl_footer);
496 $footer = trim(preg_replace('!</?div[^>]*?>!i', '', $tpl_footer[1]));
2c779f90 497
4817021f
JV
498 // set footer font
499 $font[2] *= 0.8;
500 $pdf->setFooterFont($font);
501 // set footer margin
502 $pdf->SetFooterMargin(10);
503 // set footer data
b97f7b85 504 $pdf->SetFooterData($footer);
1024f54b
JV
505
506 return $pdf;
b09f08f7 507}
6a4c89f9
JV
508
509/**
510 * Format the TCPDF footer layout
511 *
512 * @param $pdf
513 * current TCPDF object
514 * @see theme_print_pdf_tcpdf_footer2()
515 */
3ebbb392 516function theme_print_pdf_tcpdf_footer2($pdf) {
6a4c89f9 517 //Position at 1.5 cm from bottom
4eb16937 518 $pdf->writeHTMLCell(0, 15, 15, -10, $pdf->footer, 0, 0, 0, TRUE, '');
6a4c89f9
JV
519
520 $ormargins = $pdf->getOriginalMargins();
b97f7b85 521 $pagenumtxt = t('Page !n of !total', array('!n' => $pdf->PageNo(), '!total' => $pdf->getAliasNbPages()));
6a4c89f9
JV
522 //Print page number
523 if ($pdf->getRTL()) {
524 $pdf->SetX($ormargins['right']);
525 $pdf->Cell(0, 10, $pagenumtxt, 'T', 0, 'L');
526 }
527 else {
528 $pdf->SetX($ormargins['left']);
529 $pdf->Cell(0, 10, $pagenumtxt, 'T', 0, 'R');
530 }
1024f54b
JV
531
532 return $pdf;
6a4c89f9 533}