You are here

function print_pdf_generate_path in Printer, email and PDF versions 7.2

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

Gennerate a PDF for a given Drupal path.

Parameters

string $path: path of the page to convert to PDF.

array $query: (Optional) array of key/value pairs as used in the url() function for the query.

int $cid: (Optional) comment ID of the comment to render.

string $pdf_filename: (Optional) filename of the generated PDF.

string $view_mode: (Optional) view mode to be used when rendering the content.

Return value

string|null generated PDF page, or NULL in case of error

See also

print_pdf_controller()

1 call to print_pdf_generate_path()
print_pdf_controller in print_pdf/print_pdf.pages.inc
Generate a PDF version of the printer-friendly page.

File

print_pdf/print_pdf.pages.inc, line 108

Code

function print_pdf_generate_path($path, $query = NULL, $cid = NULL, $pdf_filename = NULL, $view_mode = PRINT_VIEW_MODE) {
  global $base_url;
  $link = print_pdf_print_link();
  $node = print_controller($path, $link['format'], $cid, $view_mode);
  if ($node) {

    // Call the tool's hook_pdf_tool_info(), to see if CSS must be expanded.
    $pdf_tool = explode('|', variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT));
    $cache_enabled = variable_get('print_pdf_cache_enabled', PRINT_PDF_CACHE_ENABLED_DEFAULT);
    $function = $pdf_tool[0] . '_pdf_tool_info';
    $info = function_exists($function) ? $function() : array();
    $expand = isset($info['expand_css']) ? $info['expand_css'] : FALSE;
    $html = theme('print', array(
      'node' => $node,
      'query' => $query,
      'expand_css' => $expand,
      'format' => $link['format'],
    ));

    // Img elements must be set to absolute.
    $pattern = '!<(img\\s[^>]*?)>!is';
    $html = preg_replace_callback($pattern, '_print_rewrite_urls', $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}/" . $link['path'] . '/.*?#!', '#', $html);
    $meta = array(
      'node' => $node,
      'url' => url(drupal_get_path_alias(empty($node->nid) ? $node->path : "node/{$node->nid}"), array(
        'absolute' => TRUE,
      )),
    );
    if (isset($node->name)) {
      $meta['name'] = $node->name;
    }
    if (isset($node->title)) {
      $meta['title'] = $node->title;
    }
    $paper_size = isset($node->print_pdf_size) ? $node->print_pdf_size : NULL;
    $page_orientation = isset($node->print_pdf_orientation) ? $node->print_pdf_orientation : NULL;
    $pdf = '';
    $cachemiss = FALSE;
    $cachefile = '';
    if ($cache_enabled && isset($node->nid)) {

      // See if the file exists in the cache.
      $cachefile = drupal_realpath(print_pdf_cache_dir()) . '/' . $node->nid . '.pdf';
      if (is_readable($cachefile)) {

        // Get the PDF content from the cached file.
        $pdf = file_get_contents($cachefile);
        if ($pdf === FALSE) {
          watchdog('print_pdf', 'Failed to read from cached file %file', array(
            '%file' => $cachefile,
          ), WATCHDOG_ERROR);
        }
      }
      else {
        $cachemiss = TRUE;
      }
    }

    // If cache is off or file is not cached, generate one from scratch.
    if (empty($pdf)) {
      $pdf = print_pdf_generate_html($html, $meta, NULL, $paper_size, $page_orientation);
    }
    if (!empty($pdf)) {

      // A PDF was created, save it to cache if configured.
      if ($cachemiss) {
        if (file_unmanaged_save_data($pdf, $cachefile, FILE_EXISTS_REPLACE) == FALSE) {
          watchdog('print_pdf', 'Failed to write to "%f".', array(
            '%f' => $cachefile,
          ), WATCHDOG_ERROR);
        }
      }
      return $pdf_filename ? print_pdf_dispose_content($pdf, $pdf_filename) : $pdf;
    }
  }
  return NULL;
}