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