You are here

function print_pdf_controller in Printer, email and PDF versions 6

Same name and namespace in other branches
  1. 5.4 print_pdf/print_pdf.pages.inc \print_pdf_controller()
  2. 5.3 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_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 22

Code

function print_pdf_controller() {

  // 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 = filter_xss(implode('/', $args));
  $cid = isset($_GET['comment']) ? (int) $_GET['comment'] : NULL;
  if (!empty($path)) {
    if ($alias = drupal_lookup_path('source', $path)) {

      // Alias
      $path_arr = explode('/', $alias);
      $node = node_load($path_arr[1]);
    }
    elseif (ctype_digit($args[0])) {

      // normal nid
      $node = node_load($args[0]);
    }
    $pdf_filename = variable_get('print_pdf_filename', PRINT_PDF_FILENAME_DEFAULT);
    if (module_exists('token')) {
      if (!empty($pdf_filename) && !empty($node)) {
        $pdf_filename = token_replace($pdf_filename, 'node', $node, TOKEN_PREFIX, TOKEN_SUFFIX, array(
          'clear' => TRUE,
        ));
      }
      else {
        $pdf_filename = token_replace($pdf_filename, 'global', NULL, TOKEN_PREFIX, TOKEN_SUFFIX, array(
          'clear' => TRUE,
        ));
        if (empty($pdf_filename)) {

          // If empty, use a fallback solution
          $pdf_filename = str_replace('/', '_', $path);
        }
      }
    }
  }
  else {
    $pdf_filename = 'page';
  }
  if (function_exists('transliteration_clean_filename')) {
    $pdf_filename = transliteration_clean_filename($pdf_filename, language_default('language'));
  }
  drupal_alter('print_pdf_filename', $pdf_filename, $path);
  $pdf = print_pdf_generate_path($path, $cid, $pdf_filename . '.pdf');
  if ($pdf == NULL) {
    drupal_goto($path);
    exit;
  }
  $nodepath = isset($node->path) && is_string($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());
  }
}