print_pdf.pages.inc 17.7 KB
Newer Older
jcnventura's avatar
jcnventura committed
1
<?php
jcnventura's avatar
jcnventura committed
2 3 4 5 6 7 8 9 10
// $Id$

/**
 * @file
 * Generates the PDF versions of the pages
 *
 * This file is included by the print_pdf module and includes the
 * functions that interface with the PDF generation packages.
 */
jcnventura's avatar
jcnventura committed
11

jcnventura's avatar
jcnventura committed
12
require_once(DRUPAL_ROOT . '/' . drupal_get_path('module', 'print') . '/print.pages.inc');
jcnventura's avatar
jcnventura committed
13

14 15 16 17 18 19 20 21
/**
 * Generate a PDF version of the printer-friendly page
 *
 * @see print_controller()
 * @see _print_get_template()
 * @see _print_pdf_dompdf()
 * @see _print_pdf_tcpdf()
 */
jcnventura's avatar
jcnventura committed
22
function print_pdf_controller() {
23
  global $base_url, $language;
24

25 26 27
  // Disable caching for generated PDFs, as Drupal doesn't ouput the proper headers from the cache
  $GLOBALS['conf']['cache'] = FALSE;

28
  $args = func_get_args();
jcnventura's avatar
jcnventura committed
29
  // Remove the printpdf/ prefix
30
  $path = implode('/', $args);
31
  $cid = isset($_GET['comment']) ? (int)$_GET['comment'] : NULL;
jcnventura's avatar
jcnventura committed
32

33
  $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
jcnventura's avatar
jcnventura committed
34

jcnventura's avatar
jcnventura committed
35
  $print = print_controller($path, $cid, PRINT_PDF_FORMAT);
36 37 38 39
  if ($print === FALSE) {
    return;
  }

jcnventura's avatar
jcnventura committed
40
  // Img elements must be set to absolute
41 42 43 44
  $pattern = '!<(img\s[^>]*?)>!is';
  $print['content'] = preg_replace_callback($pattern, '_print_rewrite_urls', $print['content']);
  $print['logo'] = preg_replace_callback($pattern, '_print_rewrite_urls', $print['logo']);
  $print['footer_message'] = preg_replace_callback($pattern, '_print_rewrite_urls', $print['footer_message']);
45 46 47
  // And converted from private to public paths
  $file_downloads = variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC);
  if ($file_downloads == FILE_DOWNLOADS_PRIVATE) {
48 49 50 51 52 53 54 55 56 57
    switch (variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE)) {
      case LANGUAGE_NEGOTIATION_PATH_DEFAULT:
      case LANGUAGE_NEGOTIATION_PATH:
        $lang = $language->language;
        break;
      default:
        $lang = '';
        break;
    }
    $pattern = "!(<img\s[^>]*?src\s*?=\s*?['\"]?)${base_url}/(?:(?:index.php)?\?q=)?(?:${lang}/)?system/files(/[^>]*?>)!is";
jcnventura's avatar
jcnventura committed
58
    $replacement = '$1file://' . realpath(file_directory_path()) . '$2';
59 60 61 62
    $print['content'] = preg_replace($pattern, $replacement, $print['content']);
    $print['logo'] = preg_replace($pattern, $replacement, $print['logo']);
    $print['footer_message'] = preg_replace($pattern, $replacement, $print['footer_message']);
  }
jcnventura's avatar
jcnventura committed
63

64
  $node = $print['node'];
65
  ob_start();
jcnventura's avatar
jcnventura committed
66
  include_once(DRUPAL_ROOT . '/' . _print_get_template(PRINT_PDF_FORMAT, $print['type']));
67 68 69
  $html = ob_get_contents();
  ob_end_clean();

70 71 72 73
  // Convert the a href elements
  $pattern = '!<(a\s[^>]*?)>!is';
  $html = preg_replace_callback($pattern, '_print_rewrite_urls', $html);

74 75
  $pdf_filename = variable_get('print_pdf_filename', PRINT_PDF_FILENAME_DEFAULT);
  if (function_exists('token_replace') && !empty($pdf_filename)) {
jcnventura's avatar
jcnventura committed
76
    $pdf_filename = token_replace($pdf_filename, 'node', $node) . '.pdf';
77 78
  }
  else {
jcnventura's avatar
jcnventura committed
79
    $pdf_filename = str_replace('/', '_', $path) . '.pdf';
80
  }
81
  if (basename($print_pdf_pdf_tool) == 'dompdf_config.inc.php') {
82
    _print_pdf_dompdf($print, $html, $pdf_filename);
jcnventura's avatar
jcnventura committed
83
  }
84
  elseif (basename($print_pdf_pdf_tool) == 'tcpdf.php') {
85
    _print_pdf_tcpdf($print, $html, $pdf_filename);
jcnventura's avatar
jcnventura committed
86
  }
87 88 89
  elseif (basename($print_pdf_pdf_tool) == 'wkhtmltopdf') {
    _print_pdf_wkhtmltopdf($print, $html, $pdf_filename);
  }
jcnventura's avatar
jcnventura committed
90 91 92
  else {
    return drupal_not_found();
  }
jcnventura's avatar
jcnventura committed
93 94 95 96 97 98 99 100 101 102

  $nodepath = drupal_get_normal_path($node->path);
  db_merge('print_pdf_page_counter')
    ->key(array('path' => $nodepath))
    ->fields(array(
        'totalcount' => 1,
        'timestamp' => REQUEST_TIME,
    ))
    ->expression('totalcount', 'totalcount + :inc', array(':inc' => 1))
    ->execute();
jcnventura's avatar
jcnventura committed
103 104
}

105 106 107 108 109 110 111 112 113 114 115
/**
 * Generate the PDF file using the dompdf library
 *
 * @param $print
 *   array containing the configured data
 * @param $html
 *   contents of the post-processed template already with the node data
 * @param $filename
 *   name of the PDF file to be generated
 * @see print_pdf_controller()
 */
116
function _print_pdf_dompdf($print, $html, $filename) {
117 118 119 120
  $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
  $print_pdf_paper_size = variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT);
  $print_pdf_page_orientation = variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT);
  $print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT);
jcnventura's avatar
jcnventura committed
121
  require_once(DRUPAL_ROOT . '/' . $print_pdf_pdf_tool);
jcnventura's avatar
jcnventura committed
122

123 124 125
  // dompdf seems to have problems with something in system.css so let's not use it
  $html = preg_replace('!<link.*?modules/system/system.css.*?/>!', '', $html);

126 127
  $url_array  = parse_url($print['url']);

jcnventura's avatar
jcnventura committed
128
  $protocol = $url_array['scheme'] . '://';
129
  $host = $url_array['host'];
jcnventura's avatar
jcnventura committed
130
  $path = dirname($url_array['path']) . '/';
jcnventura's avatar
jcnventura committed
131 132

  $dompdf = new DOMPDF();
133 134
  $dompdf->set_base_path($path);
  $dompdf->set_host($host);
135
  $dompdf->set_paper(drupal_strtolower($print_pdf_paper_size), $print_pdf_page_orientation);
136 137
  $dompdf->set_protocol($protocol);

jcnventura's avatar
jcnventura committed
138 139
// dompdf can't handle footers cleanly, so disable the following
//  $html = theme('print_pdf_dompdf_footer', $html);
140

141 142 143 144 145 146 147 148
  // Convert from UTF-8 to ISO 8859-1 and then to HTML entities
  if (function_exists('utf8_decode')) {
    $html = utf8_decode($html);
  }
// iconv fails silently when it encounters something that it doesn't know, so don't use it
//  else if (function_exists('iconv')) {
//    $html = iconv('UTF-8', 'ISO-8859-1', $html);
//  }
jcnventura's avatar
jcnventura committed
149
  elseif (function_exists('mb_convert_encoding')) {
150 151
    $html = mb_convert_encoding($html, 'ISO-8859-1', 'UTF-8');
  }
jcnventura's avatar
jcnventura committed
152
  elseif (function_exists('recode_string')) {
153 154 155
    $html = recode_string('UTF-8..ISO_8859-1', $html);
  }
  $html = htmlspecialchars_decode(htmlentities($html, ENT_NOQUOTES, 'ISO-8859-1'), ENT_NOQUOTES);
156

157
  //must get rid of tbody (dompdf goes into recursion)
158
  $html = preg_replace('!<tbody[^>]*?>|</tbody>!i', '', $html);
159

160
  $dompdf->load_html($html);
jcnventura's avatar
jcnventura committed
161

jcnventura's avatar
jcnventura committed
162
  $dompdf->render();
163
  $dompdf->stream($filename, array('Attachment' => ($print_pdf_content_disposition == 2)));
jcnventura's avatar
jcnventura committed
164 165
}

166 167 168 169 170 171 172 173 174 175 176
/**
 * Generate the PDF file using the TCPDF library
 *
 * @param $print
 *   array containing the configured data
 * @param $html
 *   contents of the post-processed template already with the node data
 * @param $filename
 *   name of the PDF file to be generated
 * @see print_pdf_controller()
 */
177
function _print_pdf_tcpdf($print, $html, $filename) {
178 179
  global $base_url, $language;

180 181 182 183 184
  $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
  $print_pdf_paper_size = variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT);
  $print_pdf_page_orientation = variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT);
  $print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT);

185 186
  $pdf_tool_path = realpath(dirname($print_pdf_pdf_tool));

187
  define('K_TCPDF_EXTERNAL_CONFIG', TRUE);
188 189
  define('K_PATH_MAIN', dirname($_SERVER['SCRIPT_FILENAME']));
  define('K_PATH_URL', $base_url);
jcnventura's avatar
jcnventura committed
190 191
  define('K_PATH_FONTS', $pdf_tool_path . '/fonts/');
  define('K_PATH_CACHE', $pdf_tool_path . '/cache/');
192
  define('K_PATH_IMAGES', '');
jcnventura's avatar
jcnventura committed
193
  define('K_BLANK_IMAGE', $pdf_tool_path . '/images/_blank.png');
194 195 196
  define('K_CELL_HEIGHT_RATIO', 1.25);
  define('K_SMALL_RATIO', 2/3);

jcnventura's avatar
jcnventura committed
197 198
  $pattern = "!<img[^>]*?>!is";
  $html = preg_replace_callback($pattern, create_function('$matches', 'return html_entity_decode($matches[0], ENT_QUOTES);'), $html);
199

jcnventura's avatar
jcnventura committed
200
  require_once(DRUPAL_ROOT . '/' . $print_pdf_pdf_tool);
201
  if (strpos(PDF_PRODUCER, 'PHP4') === FALSE) {
jcnventura's avatar
jcnventura committed
202
    require_once(DRUPAL_ROOT . '/' . drupal_get_path('module', 'print_pdf') . '/print_pdf.class.inc');
203 204
  }
  else {
205 206
    drupal_set_message(t("The PHP4 version of TCPDF is not supported. Please upgrade it."), 'error', FALSE);
    drupal_goto($_GET['q']);
207
  }
208

209 210
  $font = Array(
    variable_get('print_pdf_font_family', PRINT_PDF_FONT_FAMILY_DEFAULT),
211
    '',
212 213
    variable_get('print_pdf_font_size', PRINT_PDF_FONT_SIZE_DEFAULT),
  );
214
  $orientation = drupal_strtoupper($print_pdf_page_orientation[0]);
215 216

  // create new PDF document
217
  $pdf = new PrintTCPDF($orientation , 'mm', $print_pdf_paper_size, TRUE);
jcnventura's avatar
jcnventura committed
218

219
  // set document information
220 221 222 223
  $pdf->SetAuthor(strip_tags($print['submitted']));
  $pdf->SetCreator(variable_get('site_name', 'Drupal'));
  $pdf->SetTitle($print['title']);
  $keys = implode(' ', explode("\n", trim(strip_tags($print['taxonomy']))));
224
  $pdf->SetKeywords($keys);
225
  $pdf->setPDFVersion('1.6');
226

227 228 229 230
  if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
    $pdf->setRTL(TRUE);
  }

231 232 233
  $pdf = theme('print_pdf_tcpdf_header', $pdf, $html, $font);
  $pdf = theme('print_pdf_tcpdf_footer', $pdf, $html, $font);
  $pdf = theme('print_pdf_tcpdf_page', $pdf);
234 235 236 237 238 239 240

  //initialize document
  $pdf->AliasNbPages();

  // add a page
  $pdf->AddPage();

241
  $pdf = theme('print_pdf_tcpdf_content', $pdf, $html, $font);
242 243 244 245

  // reset pointer to the last page
  $pdf->lastPage();

246 247 248
  // try to recover from any warning/error
  ob_clean();

249 250 251 252 253
  //Close and output PDF document
  $output_dest = ($print_pdf_content_disposition == 2) ? 'D' : 'I';
  $pdf->Output($filename, $output_dest);
}

254 255 256 257 258 259 260 261 262 263 264 265 266
/**
 * Generate the PDF file using wkhtmltopdf
 *
 * @param $print
 *   array containing the configured data
 * @param $html
 *   contents of the post-processed template already with the node data
 * @param $filename
 *   name of the PDF file to be generated
 * @see print_pdf_controller()
 */
function _print_pdf_wkhtmltopdf($print, $html, $filename) {
  $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
267
  $print_pdf_paper_size = drupal_strtolower(variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT));
268 269 270 271 272 273 274 275 276 277 278 279 280
  $print_pdf_page_orientation = variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT);
  $print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT);

  $xvfb_binary = key(file_scan_directory(drupal_get_path('module', 'print'), '!^Xvfb$!'));

  define('WKHTMLTOPDF_DPI', '96');
  define('WKHTMLTOPDF_OPTIONS', " --footer-font-size 7 --footer-right '[page]'");
//  define('WKHTMLTOPDF_XVFB_FONT_PATH', '/usr/share/fonts/X11/misc/,/usr/share/fonts/X11/Type1/,/usr/share/fonts/X11/75dpi/,/usr/share/fonts/X11/100dpi/');
  define('WKHTMLTOPDF_TEMP_DIR', '/tmp');

  // If configured to do so, launch a temporary X server with a random display number.
  if (isset($xvfb_binary)) {
    $xdisplay = rand(10, 500);
jcnventura's avatar
jcnventura committed
281 282
    $xcmd = realpath($xvfb_binary) . " :$xdisplay -screen 0 320x200x24 -dpi " . WKHTMLTOPDF_DPI .
            ' -terminate -nolisten tcp -tst' .
283 284 285 286
            (defined('WKHTMLTOPDF_XVFB_FONT_PATH') ? ' -fp ' . WKHTMLTOPDF_XVFB_FONT_PATH : '');

    $xdescriptor = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
    $xprocess = proc_open($xcmd, $xdescriptor, $xpipes, NULL, NULL);
287 288
  }

289 290
  $descriptor = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
  $cmd = realpath($print_pdf_pdf_tool) . " --page-size $print_pdf_paper_size --orientation $print_pdf_page_orientation" .
jcnventura's avatar
jcnventura committed
291
         ' --dpi ' . WKHTMLTOPDF_DPI . (defined('WKHTMLTOPDF_OPTIONS') ? WKHTMLTOPDF_OPTIONS : '') . ' - -';
292

jcnventura's avatar
jcnventura committed
293
  $process = proc_open($cmd, $descriptor, $pipes, NULL, isset($xvfb_binary) ? array('DISPLAY' => ':' . $xdisplay) : NULL);
294 295 296 297 298 299 300 301

  if (is_resource($process)) {
    fwrite($pipes[0], $html);
    fclose($pipes[0]);

    $pdf = stream_get_contents($pipes[1]);
    fclose($pipes[1]);

302 303
    $status = proc_get_status($process);
    if (!$status['running']) {
jcnventura's avatar
jcnventura committed
304
      watchdog('print_pdf', 'wkhtmltopdf: ' . stream_get_contents($pipes[2]));
305 306
    }

307 308 309
    $retval = proc_close($process);
  }
  if (isset($xvfb_binary)) {
310 311
    $xstatus = proc_get_status($xprocess);
    if ($xstatus['exitcode']) {
jcnventura's avatar
jcnventura committed
312
      watchdog('print_pdf', 'wkhtmltopdf Xvfb: ' . stream_get_contents($xpipes[2]));
313 314
    }
    proc_close($xprocess);
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
  }

  if (isset($pdf)) {
    if (headers_sent()) {
      die("Unable to stream pdf: headers already sent");
    }
    header("Cache-Control: private");
    header("Content-Type: application/pdf");

    $attachment =  ($print_pdf_content_disposition == 2) ?  "attachment" :  "inline";

    header("Content-Disposition: $attachment; filename=\"$filename\"");

    echo $pdf;
    flush();
  }
331 332 333 334
  else {
    drupal_goto($print['url']);
    exit;
  }
335 336
}

337 338 339 340 341 342 343
/**
 * Format the dompdf footer contents
 *
 * @param $html
 *   contents of the body of the HTML from the original node
 * @see theme_print_pdf_tcpdf_footer()
 */
344
function theme_print_pdf_dompdf_footer(&$html) {
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
  preg_match('!<div class="print-footer">(.*?)</div>!si', $html, $tpl_footer);
  $html = str_replace($tpl_footer[0], '', $html);

  $text = '<script type="text/php">
    if (isset($pdf)) {
      $font = Font_Metrics::get_font("verdana");;
      $size = 10;
      $color = array(0,0,0);
      $text_height = Font_Metrics::get_font_height($font, $size);

      $w = $pdf->get_width();
      $h = $pdf->get_height();

      $footer = $pdf->open_object();

      // Draw a line along the bottom
      $y = $h - 25;
      $pdf->line(15, $y, $w - 15, $y, $color, 1);

      $y += $text_height / 2;
jcnventura's avatar
jcnventura committed
365
      $pdf->page_text(15, $y, \'' . addslashes(strip_tags($tpl_footer[1])) . '\', $font, $size, $color);
366 367 368 369 370 371 372 373 374 375 376 377 378 379

      $pdf->close_object();
      $pdf->add_object($footer, "all");

      // Center the text
      $width = Font_Metrics::get_text_width("Page 1 of 2", $font, $size);
      $pagenumtxt = t("Page !n of !total", array("!n" => "{PAGE_NUM}", "!total" => "{PAGE_COUNT}"));
      $pdf->page_text($w - 15 - $width, $y, $pagenumtxt, $font, $size, $color);
    }
  </script>';

  return str_replace("<body>", "<body>" . $text, $html);
}

380 381 382 383 384 385 386 387 388 389 390
/**
 * Format the TCPDF header
 *
 * @param $pdf
 *   current TCPDF object
 * @param $html
 *   contents of the body of the HTML from the original node
 * @param $font
 *   array with the font definition (font name, styles and size)
 * @see theme_print_pdf_tcpdf_header()
 */
391
function theme_print_pdf_tcpdf_header(&$pdf, &$html, $font) {
392 393 394 395
  preg_match('!<div class="print-logo">(.*?)</div>!si', $html, $tpl_logo);
  preg_match('!<h1 class="print-title">(.*?)</h1>!si', $html, $tpl_title);
  preg_match('!<div class="print-site_name">(.*?)</div>!si', $html, $tpl_site_name);

396
  $ratio = 0;
397 398
  $logo = '';
  $logo_ret = preg_match('!src\s*=\s*(\'.*?\'|".*?"|[^\s]*)!i', $tpl_logo[1], $matches);
399 400
  if ($logo_ret) {
    $logo = trim($matches[1], '\'"');
401
    $size = getimagesize($logo);
402
    $ratio = $size ? ($size[0] / $size[1]) : 0;
403 404
  }

405 406 407 408
  // set header font
  $pdf->setHeaderFont($font);
  // set header margin
  $pdf->SetHeaderMargin(5);
409
  // set header data
410
  $pdf->SetHeaderData($logo, 10 * $ratio, $tpl_title[1], strip_tags($tpl_site_name[1]));
411 412

  return $pdf;
413
}
414

415 416 417 418 419 420 421
/**
 * Format the TCPDF page settings (margins, etc)
 *
 * @param $pdf
 *   current TCPDF object
 * @see theme_print_pdf_tcpdf_page()
 */
422
function theme_print_pdf_tcpdf_page(&$pdf) {
jcnventura's avatar
jcnventura committed
423
  // set margins
424
  $pdf->SetMargins(15, 20, 15);
425
  // set auto page breaks
426
  $pdf->SetAutoPageBreak(TRUE, 15);
jcnventura's avatar
jcnventura committed
427
  // set image scale factor
jcnventura's avatar
jcnventura committed
428
  $pdf->setImageScale(4);
429 430
  // set image compression quality
  $pdf->setJPEGQuality(100);
431 432

  return $pdf;
433
}
434

435 436 437 438 439 440 441 442 443 444 445
/**
 * Format the TCPDF page content
 *
 * @param $pdf
 *   current TCPDF object
 * @param $html
 *   contents of the body of the HTML from the original node
 * @param $font
 *   array with the font definition (font name, styles and size)
 * @see theme_print_pdf_tcpdf_content()
 */
446
function theme_print_pdf_tcpdf_content(&$pdf, &$html, $font) {
447 448
  // set content font
  $pdf->setFont($font[0], $font[1], $font[2]);
jcnventura's avatar
jcnventura committed
449

450
  preg_match('!<body.*?>(.*)</body>!sim', $html, $matches);
451
  $pattern = '!(?:<div class="print-(?:logo|site_name|breadcrumb|footer)">.*?</div>|<hr class="print-hr" />)!si';
452 453
  $matches[1] = preg_replace($pattern, '', $matches[1]);

454 455 456 457 458
  // Make CCK fields look better
  $matches[1] = preg_replace('!(<div class="field.*?>)\s*!sm', '$1', $matches[1]);
  $matches[1] = preg_replace('!(<div class="field.*?>.*?</div>)\s*!sm', '$1', $matches[1]);
  $matches[1] = preg_replace('!<div( class="field-label.*?>.*?)</div>!sm', '<strong$1</strong>', $matches[1]);

459
  // Since TCPDF's writeHTML is so bad with <p>, do everything possible to make it look nice
460
  $matches[1] = preg_replace('!<(?:p(|\s+.*?)/?|/p)>!i', '<br$1 />', $matches[1]);
461
  $matches[1] = str_replace(array('<div', 'div>'), array('<span', 'span><br />'), $matches[1]);
462 463 464 465
  do {
    $prev = $matches[1];
    $matches[1] = preg_replace('!(</span>)<br />(\s*?</span><br />)!s', '$1$2', $matches[1]);
  } while ($prev != $matches[1]);
466

467
  @$pdf->writeHTML($matches[1]);
468 469

  return $pdf;
470
}
jcnventura's avatar
jcnventura committed
471

472 473 474 475 476 477 478 479 480 481 482
/**
 * Format the TCPDF footer contents
 *
 * @param $pdf
 *   current TCPDF object
 * @param $html
 *   contents of the body of the HTML from the original node
 * @param $font
 *   array with the font definition (font name, styles and size)
 * @see theme_print_pdf_tcpdf_footer()
 */
483
function theme_print_pdf_tcpdf_footer(&$pdf, &$html, $font) {
484 485
  preg_match('!<div class="print-footer">(.*?)</div>!si', $html, $tpl_footer);
  $footer = trim(preg_replace('!</?div[^>]*?>!i', '', $tpl_footer[1]));
486

487 488 489 490 491 492
  // set footer font
  $font[2] *= 0.8;
  $pdf->setFooterFont($font);
  // set footer margin
  $pdf->SetFooterMargin(10);
  // set footer data
493
  $pdf->SetFooterData($footer);
494 495

  return $pdf;
jcnventura's avatar
jcnventura committed
496
}
497 498 499 500 501 502 503 504

/**
 * Format the TCPDF footer layout
 *
 * @param $pdf
 *   current TCPDF object
 * @see theme_print_pdf_tcpdf_footer2()
 */
505
function theme_print_pdf_tcpdf_footer2(&$pdf) {
506
  //Position at 1.5 cm from bottom
507
  $pdf->writeHTMLCell(0, 15, 15, 0, $pdf->footer, 0, 0, 0, TRUE, '');
508 509

  $ormargins = $pdf->getOriginalMargins();
510
  $pagenumtxt = t('Page !n of !total', array('!n' => $pdf->PageNo(), '!total' => $pdf->getAliasNbPages()));
511 512 513 514 515 516 517 518 519
  //Print page number
  if ($pdf->getRTL()) {
    $pdf->SetX($ormargins['right']);
    $pdf->Cell(0, 10, $pagenumtxt, 'T', 0, 'L');
  }
  else {
    $pdf->SetX($ormargins['left']);
    $pdf->Cell(0, 10, $pagenumtxt, 'T', 0, 'R');
  }
520 521

  return $pdf;
522
}