1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Generates the PDF versions of the pages.
|
6
|
*
|
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
|
* Generate a PDF version of the printer-friendly page.
|
17
|
*
|
18
|
* @see print_controller()
|
19
|
* @see _print_pdf_dompdf()
|
20
|
* @see _print_pdf_tcpdf()
|
21
|
*/
|
22
|
function print_pdf_controller() {
|
23
|
// Disable caching for generated PDFs, as Drupal doesn't output the proper
|
24
|
// headers from the cache.
|
25
|
$GLOBALS['conf']['cache'] = FALSE;
|
26
|
|
27
|
$args = func_get_args();
|
28
|
$path = filter_xss(implode('/', $args));
|
29
|
$cid = isset($_GET['comment']) ? (int) $_GET['comment'] : NULL;
|
30
|
|
31
|
// Handle the query.
|
32
|
$query = $_GET;
|
33
|
unset($query['q']);
|
34
|
|
35
|
$node = NULL;
|
36
|
if (!empty($path)) {
|
37
|
if ($alias = drupal_lookup_path('source', $path)) {
|
38
|
// Alias.
|
39
|
$path_arr = explode('/', $alias);
|
40
|
$node = node_load($path_arr[1]);
|
41
|
}
|
42
|
elseif (ctype_digit($args[0])) {
|
43
|
// Normal nid.
|
44
|
$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
|
$pdf_filename = token_replace($pdf_filename, array('node' => $node), array('clear' => TRUE));
|
50
|
}
|
51
|
else {
|
52
|
$pdf_filename = token_replace($pdf_filename, array('site'), array('clear' => TRUE));
|
53
|
if (empty($pdf_filename)) {
|
54
|
// If empty, use a fallback solution.
|
55
|
$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
|
$nodepath = (isset($node->nid)) ? 'node/' . $node->nid : drupal_get_normal_path($path);
|
76
|
db_merge('print_pdf_page_counter')
|
77
|
->key(array('path' => $nodepath))
|
78
|
->fields(array(
|
79
|
'totalcount' => 1,
|
80
|
'timestamp' => REQUEST_TIME,
|
81
|
))
|
82
|
->expression('totalcount', 'totalcount + 1')
|
83
|
->execute();
|
84
|
|
85
|
drupal_exit();
|
86
|
}
|
87
|
|
88
|
/**
|
89
|
* Gennerate a PDF for a given Drupal path.
|
90
|
*
|
91
|
* @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
|
*
|
103
|
* @return string|null
|
104
|
* generated PDF page, or NULL in case of error
|
105
|
*
|
106
|
* @see print_pdf_controller()
|
107
|
*/
|
108
|
function print_pdf_generate_path($path, $query = NULL, $cid = NULL, $pdf_filename = NULL, $view_mode = PRINT_VIEW_MODE) {
|
109
|
global $base_url;
|
110
|
|
111
|
$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
|
|
118
|
$function = $pdf_tool[0] . '_pdf_tool_info';
|
119
|
if (function_exists($function)) {
|
120
|
$info = $function();
|
121
|
}
|
122
|
$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
|
}
|
150
|
if (isset($node->title)) {
|
151
|
$meta['title'] = $node->title;
|
152
|
}
|
153
|
|
154
|
$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
|
}
|
173
|
}
|
174
|
|
175
|
// 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
|
|
180
|
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
|
|
188
|
return $pdf_filename ? print_pdf_dispose_content($pdf, $pdf_filename) : $pdf;
|
189
|
}
|
190
|
}
|
191
|
return NULL;
|
192
|
}
|