print_pdf.pages.inc 5.12 KB
Newer Older
jcnventura's avatar
jcnventura committed
1
<?php
jcnventura's avatar
jcnventura committed
2 3 4 5 6 7 8

/**
 * @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.
9 10
 *
 * @ingroup print
jcnventura's avatar
jcnventura committed
11
 */
jcnventura's avatar
jcnventura committed
12

13
module_load_include('inc', 'print', 'print.pages');
jcnventura's avatar
jcnventura committed
14

15 16 17 18 19 20 21
/**
 * Generate a PDF version of the printer-friendly page
 *
 * @see print_controller()
 * @see _print_pdf_dompdf()
 * @see _print_pdf_tcpdf()
 */
jcnventura's avatar
jcnventura committed
22
function print_pdf_controller() {
23 24 25
  // Disable caching for generated PDFs, as Drupal doesn't ouput the proper headers from the cache
  $GLOBALS['conf']['cache'] = FALSE;

26
  $args = func_get_args();
jcnventura's avatar
jcnventura committed
27
  $path = filter_xss(implode('/', $args));
28
  $cid = isset($_GET['comment']) ? (int)$_GET['comment'] : NULL;
jcnventura's avatar
jcnventura committed
29

30 31 32 33 34 35 36 37 38 39 40 41 42
  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 (!empty($pdf_filename) && !empty($node)) {
43
      $pdf_filename = token_replace($pdf_filename, array('node' => $node));
44 45
    }
    else {
46
      $pdf_filename = token_replace($pdf_filename, array('site'));
47
      if (empty($pdf_filename) || count(token_scan($pdf_filename))) {
48 49 50
        // If there are still tokens, use a fallback solution
        $pdf_filename = str_replace('/', '_', $path);
      }
51
    }
52 53
  }
  else {
54
    $pdf_filename = 'page';
55
  }
56

57 58 59 60
  if (function_exists('transliteration_clean_filename')) {
    $pdf_filename = transliteration_clean_filename($pdf_filename, language_default('language'));
  }

61 62
  drupal_alter('print_pdf_filename', $pdf_filename, $path);

63
  $pdf = print_pdf_generate_path($path, $cid, $pdf_filename . '.pdf');
64
  if ($pdf == NULL) {
65
    drupal_goto($path);
66 67
    exit;
  }
jcnventura's avatar
jcnventura committed
68

69
  $nodepath = (isset($node->path) && is_string($node->path)) ? drupal_get_normal_path($node->path) : 'node/' . $path;
70 71 72 73 74 75
  db_merge('print_pdf_page_counter')
    ->key(array('path' => $nodepath))
    ->fields(array(
        'totalcount' => 1,
        'timestamp' => REQUEST_TIME,
    ))
76
    ->expression('totalcount', 'totalcount + 1')
77
    ->execute();
78 79

  drupal_exit();
80 81
}

jcnventura's avatar
jcnventura committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
/**
 * Gennerate a PDF for a given Drupal path.
 *
 * @param string $path
 *   path of the page to convert to PDF
 * @param int $cid
 *   (optional) comment ID of the comment to render.
 * @param string $pdf_filename
 *   (optional) filename of the generated PDF
 *
 * @return
 *   generated PDF page, or NULL in case of error
 *
 * @see print_pdf_controller()
 */
97
function print_pdf_generate_path($path, $cid = NULL, $pdf_filename = NULL) {
98 99
  global $base_url;

jcnventura's avatar
jcnventura committed
100
  $link = print_pdf_print_link();
101 102
  $node = print_controller($path, $link['format'], $cid);
  if ($node) {
103
    $html = theme('print', array('node' => $node, 'expand_css' => TRUE, 'format' => $link['format']));
104

105 106
    // Img elements must be set to absolute
    $pattern = '!<(img\s[^>]*?)>!is';
107
    $html = preg_replace_callback($pattern, '_print_rewrite_urls', $html);
108 109 110 111 112 113 114

    // 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);

115 116 117
    $meta = array();
    if (isset($node)) {
      $meta['node'] = $node;
118

119 120 121 122 123
      // @todo get these
      if (isset($node->url)) $meta['url'] = $node->url;
      if (isset($node->name)) $meta['name'] = $node->name;
      if (isset($node->title)) $meta['title'] = $node->title;
    }
124

125
    return print_pdf_generate_html($html, $meta, $pdf_filename);
jcnventura's avatar
jcnventura committed
126
  }
127 128
  else {
    return NULL;
129
  }
jcnventura's avatar
jcnventura committed
130 131
}

132 133 134 135 136 137 138 139
/**
 * Convert image paths to the file:// protocol
 *
 * In some Drupal setups, the use of the 'private' filesystem or Apache's
 * configuration prevent access to the images of the page. This function
 * tries to circumnvent those problems by accessing files in the local
 * filesystem.
 *
jcnventura's avatar
jcnventura committed
140
 * @param string $html
141
 *   contents of the post-processed template already with the node data
jcnventura's avatar
jcnventura committed
142 143 144 145
 *
 * @return string
 *   converted file names
 *
146 147 148 149
 * @see print_pdf_controller()
 */
function _print_pdf_file_access_images($html) {
  global $base_url, $language;
150
  $print_pdf_images_via_file = variable_get('print_pdf_images_via_file', PRINT_PDF_IMAGES_VIA_FILE_DEFAULT);
151

152
  $lang = (function_exists('language_negotiation_get_any') && language_negotiation_get_any('locale-url')) ? $language->language : '';
153

154
  // Always convert private to local paths
155 156 157 158
  $pattern = "!(<img\s[^>]*?src\s*?=\s*?['\"]?)${base_url}/(?:(?:index.php)?\?q=)?(?:${lang}/)?system/files/([^>]*?>)!is";
  $replacement = '$1file://' . realpath(variable_get('file_private_path', '')) . '/$2';
  $html = preg_replace($pattern, $replacement, $html);
  if ($print_pdf_images_via_file) {
159 160 161 162
    $pattern = "!(<img\s[^>]*?src\s*?=\s*?['\"]?)${base_url}/(?:(?:index.php)?\?q=)?(?:${lang}/)?([^>]*?>)!is";
    $replacement = '$1file://' . dirname($_SERVER['SCRIPT_FILENAME']) . '/$2';
    $html = preg_replace($pattern, $replacement, $html);
  }
163

164 165
  return $html;
}