1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
76bdcd04
|
Assos Assos
|
* Generates the PDF versions of the pages.
|
6 |
85ad3d82
|
Assos Assos
|
*
|
7 |
|
|
* This file is included by the print_pdf module and includes the
|
8 |
|
|
* functions that interface with the PDF generation packages.
|
9 |
|
|
*
|
10 |
|
|
* @ingroup print
|
11 |
|
|
*/
|
12 |
|
|
|
13 |
|
|
module_load_include('inc', 'print', 'print.pages');
|
14 |
|
|
|
15 |
|
|
/**
|
16 |
76bdcd04
|
Assos Assos
|
* Generate a PDF version of the printer-friendly page.
|
17 |
85ad3d82
|
Assos Assos
|
*
|
18 |
|
|
* @see print_controller()
|
19 |
|
|
* @see _print_pdf_dompdf()
|
20 |
|
|
* @see _print_pdf_tcpdf()
|
21 |
|
|
*/
|
22 |
|
|
function print_pdf_controller() {
|
23 |
76bdcd04
|
Assos Assos
|
// Disable caching for generated PDFs, as Drupal doesn't output the proper
|
24 |
|
|
// headers from the cache.
|
25 |
85ad3d82
|
Assos Assos
|
$GLOBALS['conf']['cache'] = FALSE;
|
26 |
|
|
|
27 |
|
|
$args = func_get_args();
|
28 |
|
|
$path = filter_xss(implode('/', $args));
|
29 |
76bdcd04
|
Assos Assos
|
$cid = isset($_GET['comment']) ? (int) $_GET['comment'] : NULL;
|
30 |
85ad3d82
|
Assos Assos
|
|
31 |
76bdcd04
|
Assos Assos
|
// Handle the query.
|
32 |
85ad3d82
|
Assos Assos
|
$query = $_GET;
|
33 |
|
|
unset($query['q']);
|
34 |
|
|
|
35 |
76bdcd04
|
Assos Assos
|
$node = NULL;
|
36 |
85ad3d82
|
Assos Assos
|
if (!empty($path)) {
|
37 |
|
|
if ($alias = drupal_lookup_path('source', $path)) {
|
38 |
76bdcd04
|
Assos Assos
|
// Alias.
|
39 |
85ad3d82
|
Assos Assos
|
$path_arr = explode('/', $alias);
|
40 |
|
|
$node = node_load($path_arr[1]);
|
41 |
|
|
}
|
42 |
|
|
elseif (ctype_digit($args[0])) {
|
43 |
76bdcd04
|
Assos Assos
|
// Normal nid.
|
44 |
85ad3d82
|
Assos Assos
|
$node = node_load($args[0]);
|
45 |
|
|
}
|
46 |
|
|
|
47 |
|
|
$pdf_filename = variable_get('print_pdf_filename', PRINT_PDF_FILENAME_DEFAULT);
|
48 |
|
|
if (!empty($pdf_filename) && !empty($node)) {
|
49 |
a2baadd1
|
Assos Assos
|
$pdf_filename = token_replace($pdf_filename, array('node' => $node), array('clear' => TRUE));
|
50 |
85ad3d82
|
Assos Assos
|
}
|
51 |
|
|
else {
|
52 |
a2baadd1
|
Assos Assos
|
$pdf_filename = token_replace($pdf_filename, array('site'), array('clear' => TRUE));
|
53 |
|
|
if (empty($pdf_filename)) {
|
54 |
76bdcd04
|
Assos Assos
|
// If empty, use a fallback solution.
|
55 |
85ad3d82
|
Assos Assos
|
$pdf_filename = str_replace('/', '_', $path);
|
56 |
|
|
}
|
57 |
|
|
}
|
58 |
|
|
}
|
59 |
|
|
else {
|
60 |
|
|
$pdf_filename = 'page';
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
if (function_exists('transliteration_clean_filename')) {
|
64 |
|
|
$pdf_filename = transliteration_clean_filename($pdf_filename, language_default('language'));
|
65 |
|
|
}
|
66 |
|
|
|
67 |
|
|
drupal_alter('print_pdf_filename', $pdf_filename, $path);
|
68 |
|
|
|
69 |
|
|
$pdf = print_pdf_generate_path($path, $query, $cid, $pdf_filename . '.pdf');
|
70 |
|
|
if ($pdf == NULL) {
|
71 |
|
|
drupal_goto($path);
|
72 |
|
|
exit;
|
73 |
|
|
}
|
74 |
|
|
|
75 |
76bdcd04
|
Assos Assos
|
$nodepath = (isset($node->nid)) ? 'node/' . $node->nid : drupal_get_normal_path($path);
|
76 |
85ad3d82
|
Assos Assos
|
db_merge('print_pdf_page_counter')
|
77 |
|
|
->key(array('path' => $nodepath))
|
78 |
|
|
->fields(array(
|
79 |
76bdcd04
|
Assos Assos
|
'totalcount' => 1,
|
80 |
|
|
'timestamp' => REQUEST_TIME,
|
81 |
85ad3d82
|
Assos Assos
|
))
|
82 |
|
|
->expression('totalcount', 'totalcount + 1')
|
83 |
|
|
->execute();
|
84 |
|
|
|
85 |
|
|
drupal_exit();
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
/**
|
89 |
76bdcd04
|
Assos Assos
|
* Gennerate a PDF for a given Drupal path.
|
90 |
85ad3d82
|
Assos Assos
|
*
|
91 |
76bdcd04
|
Assos Assos
|
* @param string $path
|
92 |
|
|
* path of the page to convert to PDF.
|
93 |
|
|
* @param array $query
|
94 |
|
|
* (Optional) array of key/value pairs as used in the url() function for the
|
95 |
|
|
* query.
|
96 |
|
|
* @param int $cid
|
97 |
|
|
* (Optional) comment ID of the comment to render.
|
98 |
|
|
* @param string $pdf_filename
|
99 |
|
|
* (Optional) filename of the generated PDF.
|
100 |
|
|
* @param string $view_mode
|
101 |
|
|
* (Optional) view mode to be used when rendering the content.
|
102 |
85ad3d82
|
Assos Assos
|
*
|
103 |
76bdcd04
|
Assos Assos
|
* @return string|null
|
104 |
|
|
* generated PDF page, or NULL in case of error
|
105 |
85ad3d82
|
Assos Assos
|
*
|
106 |
|
|
* @see print_pdf_controller()
|
107 |
|
|
*/
|
108 |
76bdcd04
|
Assos Assos
|
function print_pdf_generate_path($path, $query = NULL, $cid = NULL, $pdf_filename = NULL, $view_mode = PRINT_VIEW_MODE) {
|
109 |
|
|
global $base_url;
|
110 |
85ad3d82
|
Assos Assos
|
|
111 |
76bdcd04
|
Assos Assos
|
$link = print_pdf_print_link();
|
112 |
|
|
$node = print_controller($path, $link['format'], $cid, $view_mode);
|
113 |
|
|
if ($node) {
|
114 |
|
|
// Call the tool's hook_pdf_tool_info(), to see if CSS must be expanded.
|
115 |
|
|
$pdf_tool = explode('|', variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT));
|
116 |
|
|
$cache_enabled = variable_get('print_pdf_cache_enabled', PRINT_PDF_CACHE_ENABLED_DEFAULT);
|
117 |
85ad3d82
|
Assos Assos
|
|
118 |
76bdcd04
|
Assos Assos
|
$function = $pdf_tool[0] . '_pdf_tool_info';
|
119 |
|
|
if (function_exists($function)) {
|
120 |
|
|
$info = $function();
|
121 |
85ad3d82
|
Assos Assos
|
}
|
122 |
76bdcd04
|
Assos Assos
|
$expand = isset($info['expand_css']) ? $info['expand_css'] : FALSE;
|
123 |
|
|
|
124 |
|
|
$html = theme('print', array(
|
125 |
|
|
'node' => $node,
|
126 |
|
|
'query' => $query,
|
127 |
|
|
'expand_css' => $expand,
|
128 |
|
|
'format' => $link['format'],
|
129 |
|
|
));
|
130 |
|
|
|
131 |
|
|
// Img elements must be set to absolute.
|
132 |
|
|
$pattern = '!<(img\s[^>]*?)>!is';
|
133 |
|
|
$html = preg_replace_callback($pattern, '_print_rewrite_urls', $html);
|
134 |
|
|
|
135 |
|
|
// Convert the a href elements, to make sure no relative links remain.
|
136 |
|
|
$pattern = '!<(a\s[^>]*?)>!is';
|
137 |
|
|
$html = preg_replace_callback($pattern, '_print_rewrite_urls', $html);
|
138 |
|
|
// And make anchor links relative again, to permit in-PDF navigation.
|
139 |
|
|
$html = preg_replace("!${base_url}/" . $link['path'] . '/.*?#!', '#', $html);
|
140 |
|
|
// Remove all scripts due to security concerns.
|
141 |
|
|
$html = preg_replace('!<script(.*?)>(.*?)</script>!is', '', $html);
|
142 |
|
|
|
143 |
|
|
$meta = array(
|
144 |
|
|
'node' => $node,
|
145 |
|
|
'url' => url(drupal_get_path_alias(empty($node->nid) ? $node->path : "node/$node->nid"), array('absolute' => TRUE)),
|
146 |
|
|
);
|
147 |
|
|
if (isset($node->name)) {
|
148 |
|
|
$meta['name'] = $node->name;
|
149 |
85ad3d82
|
Assos Assos
|
}
|
150 |
76bdcd04
|
Assos Assos
|
if (isset($node->title)) {
|
151 |
|
|
$meta['title'] = $node->title;
|
152 |
85ad3d82
|
Assos Assos
|
}
|
153 |
|
|
|
154 |
76bdcd04
|
Assos Assos
|
$paper_size = isset($node->print_pdf_size) ? $node->print_pdf_size : NULL;
|
155 |
|
|
$page_orientation = isset($node->print_pdf_orientation) ? $node->print_pdf_orientation : NULL;
|
156 |
|
|
|
157 |
|
|
$pdf = '';
|
158 |
|
|
$cachemiss = FALSE;
|
159 |
|
|
$cachefile = '';
|
160 |
|
|
if ($cache_enabled && isset($node->nid)) {
|
161 |
|
|
// See if the file exists in the cache.
|
162 |
|
|
$cachefile = drupal_realpath(print_pdf_cache_dir()) . '/' . $node->nid . '.pdf';
|
163 |
|
|
if (is_readable($cachefile)) {
|
164 |
|
|
// Get the PDF content from the cached file.
|
165 |
|
|
$pdf = file_get_contents($cachefile);
|
166 |
|
|
if ($pdf === FALSE) {
|
167 |
|
|
watchdog('print_pdf', 'Failed to read from cached file %file', array('%file' => $cachefile), WATCHDOG_ERROR);
|
168 |
|
|
}
|
169 |
|
|
}
|
170 |
|
|
else {
|
171 |
|
|
$cachemiss = TRUE;
|
172 |
a2baadd1
|
Assos Assos
|
}
|
173 |
85ad3d82
|
Assos Assos
|
}
|
174 |
|
|
|
175 |
76bdcd04
|
Assos Assos
|
// If cache is off or file is not cached, generate one from scratch.
|
176 |
|
|
if (empty($pdf)) {
|
177 |
|
|
$pdf = print_pdf_generate_html($html, $meta, NULL, $paper_size, $page_orientation);
|
178 |
|
|
}
|
179 |
85ad3d82
|
Assos Assos
|
|
180 |
76bdcd04
|
Assos Assos
|
if (!empty($pdf)) {
|
181 |
|
|
// A PDF was created, save it to cache if configured.
|
182 |
|
|
if ($cachemiss) {
|
183 |
|
|
if (file_unmanaged_save_data($pdf, $cachefile, FILE_EXISTS_REPLACE) == FALSE) {
|
184 |
|
|
watchdog('print_pdf', 'Failed to write to "%f".', array('%f' => $cachefile), WATCHDOG_ERROR);
|
185 |
|
|
}
|
186 |
|
|
}
|
187 |
85ad3d82
|
Assos Assos
|
|
188 |
76bdcd04
|
Assos Assos
|
return $pdf_filename ? print_pdf_dispose_content($pdf, $pdf_filename) : $pdf;
|
189 |
|
|
}
|
190 |
85ad3d82
|
Assos Assos
|
}
|
191 |
76bdcd04
|
Assos Assos
|
return NULL;
|
192 |
85ad3d82
|
Assos Assos
|
} |