You are here

function print_pdf_controller in Printer, email and PDF versions 5.4

Same name and namespace in other branches
  1. 5.3 print_pdf/print_pdf.pages.inc \print_pdf_controller()
  2. 6 print_pdf/print_pdf.pages.inc \print_pdf_controller()
  3. 7.2 print_pdf/print_pdf.pages.inc \print_pdf_controller()
  4. 7 print_pdf/print_pdf.pages.inc \print_pdf_controller()
  5. 5.x print_pdf/print_pdf.pages.inc \print_pdf_controller()

Generate a PDF version of the printer-friendly page

See also

print_controller()

_print_get_template()

_print_pdf_dompdf()

_print_pdf_tcpdf()

1 string reference to 'print_pdf_controller'
print_pdf_menu in print_pdf/print_pdf.module
Implementation of hook_menu().

File

print_pdf/print_pdf.pages.inc, line 23

Code

function print_pdf_controller() {
  global $base_url;

  // Disable caching for generated PDFs, as Drupal doesn't ouput the proper headers from the cache
  $GLOBALS['conf']['cache'] = FALSE;
  $args = func_get_args();
  $path = implode('/', $args);
  $cid = isset($_GET['comment']) ? (int) $_GET['comment'] : NULL;
  $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
  $print = print_controller($path, $cid, PRINT_PDF_FORMAT);
  if ($print === FALSE) {
    return;
  }

  // Img elements must be set to absolute
  $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']);

  // Send to printer option causes problems with PDF
  $print['sendtoprinter'] = '';
  $node = $print['node'];
  ob_start();
  include_once _print_get_template(PRINT_PDF_FORMAT, $print['type']);
  $html = ob_get_contents();
  ob_end_clean();
  $html = drupal_final_markup($html);

  // Convert the a href elements, to make sure no relative links remain
  $pattern = '!<(a\\s[^>]*?)>!is';
  $html = preg_replace_callback($pattern, '_print_rewrite_urls', $html);

  // And make anchor links relative again, to permit in-PDF navigation
  $html = preg_replace("!{$base_url}/" . PRINTPDF_PATH . "/.*?%2523!", '#', $html);
  $pdf_filename = variable_get('print_pdf_filename', PRINT_PDF_FILENAME_DEFAULT);
  if (function_exists('token_replace') && !empty($pdf_filename)) {
    $pdf_filename = token_replace($pdf_filename, 'node', $node) . '.pdf';
  }
  else {
    $pdf_filename = str_replace('/', '_', $path) . '.pdf';
  }
  if (function_exists('transliteration_clean_filename')) {
    $pdf_filename = transliteration_clean_filename($pdf_filename);
  }
  if (basename($print_pdf_pdf_tool) == 'dompdf_config.inc.php') {
    _print_pdf_dompdf($print, $html, $pdf_filename);
  }
  elseif (basename($print_pdf_pdf_tool) == 'tcpdf.php') {
    _print_pdf_tcpdf($print, $html, $pdf_filename);
  }
  elseif (substr(basename($print_pdf_pdf_tool, '.exe'), 0, 11) == 'wkhtmltopdf') {
    _print_pdf_wkhtmltopdf($print, $html, $pdf_filename);
  }
  else {
    return drupal_not_found();
  }
  $nodepath = isset($node->path) ? drupal_get_normal_path($node->path) : 'node/' . $path;
  db_query("UPDATE {print_pdf_page_counter} SET totalcount = totalcount + 1, timestamp = %d WHERE path = '%s'", time(), $nodepath);

  // If we affected 0 rows, this is the first time viewing the node.
  if (!db_affected_rows()) {

    // We must create a new row to store counters for the new node.
    db_query("INSERT INTO {print_pdf_page_counter} (path, totalcount, timestamp) VALUES ('%s', 1, %d)", $nodepath, time());
  }
}