Revision 76bdcd04
Added by Assos Assos almost 6 years ago
drupal7/sites/all/modules/print/print_pdf/print_pdf.module | ||
---|---|---|
7 | 7 |
* @ingroup print |
8 | 8 |
*/ |
9 | 9 |
|
10 |
define('PRINTPDF_PATH', 'printpdf'); |
|
11 |
|
|
12 |
// Defined in print.module |
|
13 |
// define('PRINT_PDF_FORMAT', 'pdf'); |
|
14 |
|
|
15 |
define('PRINT_PDF_LIB_PATH', 'sites/all/libraries'); |
|
16 |
|
|
17 |
define('PRINT_PDF_LINK_POS_DEFAULT', '{ "link": "link", "block": "block", "help": "help" }'); |
|
18 |
define('PRINT_PDF_LINK_TEASER_DEFAULT', 0); |
|
19 |
define('PRINT_PDF_SHOW_LINK_DEFAULT', 1); |
|
20 |
define('PRINT_PDF_NODE_LINK_VISIBILITY_DEFAULT', 0); |
|
21 |
define('PRINT_PDF_NODE_LINK_PAGES_DEFAULT', ''); |
|
22 |
define('PRINT_PDF_LINK_CLASS_DEFAULT', 'print-pdf'); |
|
23 |
define('PRINT_PDF_SYS_LINK_VISIBILITY_DEFAULT', 1); |
|
24 |
define('PRINT_PDF_SYS_LINK_PAGES_DEFAULT', ''); |
|
25 |
define('PRINT_PDF_LINK_USE_ALIAS_DEFAULT', 0); |
|
26 |
define('PRINT_PDF_BOOK_LINK_DEFAULT', 1); |
|
27 |
define('PRINT_PDF_PDF_TOOL_DEFAULT', 0); |
|
10 |
define('PRINT_PDF_PDF_TOOL_DEFAULT', FALSE); |
|
28 | 11 |
define('PRINT_PDF_CONTENT_DISPOSITION_DEFAULT', 2); |
29 | 12 |
define('PRINT_PDF_PAPER_SIZE_DEFAULT', 'A4'); |
30 | 13 |
define('PRINT_PDF_PAGE_ORIENTATION_DEFAULT', 'portrait'); |
31 | 14 |
define('PRINT_PDF_IMAGES_VIA_FILE_DEFAULT', 0); |
32 | 15 |
define('PRINT_PDF_AUTOCONFIG_DEFAULT', 1); |
33 |
define('PRINT_PDF_FONT_FAMILY_DEFAULT', 'dejavusans'); |
|
34 |
define('PRINT_PDF_FONT_SIZE_DEFAULT', 10); |
|
35 |
define('PRINT_PDF_FONT_SUBSETTING_DEFAULT', FALSE); |
|
36 | 16 |
define('PRINT_PDF_FILENAME_DEFAULT', '[site:name] - [node:title] - [node:changed:custom:Y-m-d]'); |
37 |
define('PRINT_PDF_DOMPDF_UNICODE_DEFAULT', 0); |
|
38 |
define('PRINT_PDF_WKHTMLTOPDF_OPTIONS', "--footer-font-size 7 --footer-right '[page]'"); |
|
39 |
define('PRINT_PDF_DOMPDF_CACHE_DIR_DEFAULT', 'print_pdf/dompdf'); |
|
40 |
define('PRINT_PDF_TCPDF_CACHE_DIR_DEFAULT', 'print_pdf/tcpdf'); |
|
17 |
define('PRINT_PDF_CACHE_ENABLED_DEFAULT', 0); |
|
18 |
// 1 day. |
|
19 |
define('PRINT_PDF_CACHE_LIFETIME_DEFAULT', 86400); |
|
41 | 20 |
|
42 | 21 |
/** |
43 |
* Implements hook_permission().
|
|
22 |
* Implements hook_print_link().
|
|
44 | 23 |
*/ |
45 |
function print_pdf_permission() {
|
|
24 |
function print_pdf_print_link() {
|
|
46 | 25 |
return array( |
47 |
'access PDF version' => array( |
|
48 |
'title' => t('Access the PDF version'), |
|
49 |
'description' => t('View the PDF versions and the links to them in the original pages.'), |
|
50 |
), |
|
26 |
'format' => 'pdf', |
|
27 |
'text' => t('PDF version'), |
|
28 |
'description' => t('Display a PDF version of this page.'), |
|
29 |
'path' => 'printpdf', |
|
30 |
'class' => 'print-pdf', |
|
31 |
'icon' => 'pdf_icon.png', |
|
32 |
'module' => 'print_pdf', |
|
51 | 33 |
); |
52 | 34 |
} |
53 | 35 |
|
54 | 36 |
/** |
55 |
* Implements hook_theme().
|
|
37 |
* Implements hook_print_new_window_alter().
|
|
56 | 38 |
*/ |
57 |
function print_pdf_theme() { |
|
39 |
function print_pdf_print_new_window_alter(&$new_window, $format) { |
|
40 |
$new_window = (variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT) == 1); |
|
41 |
} |
|
42 |
|
|
43 |
/** |
|
44 |
* Implements hook_permission(). |
|
45 |
*/ |
|
46 |
function print_pdf_permission() { |
|
58 | 47 |
return array( |
59 |
'print_pdf_format_link' => array( |
|
60 |
'variables' => array(), |
|
61 |
), |
|
62 |
'print_pdf_dompdf_footer' => array( |
|
63 |
'variables' => array('html' => ''), |
|
64 |
'file' => 'print_pdf.pages.inc', |
|
65 |
), |
|
66 |
'print_pdf_tcpdf_header' => array( |
|
67 |
'variables' => array('pdf' => NULL, 'html' => '', 'font' => array()), |
|
68 |
'file' => 'print_pdf.pages.inc', |
|
69 |
), |
|
70 |
'print_pdf_tcpdf_page' => array( |
|
71 |
'variables' => array('pdf' => NULL), |
|
72 |
'file' => 'print_pdf.pages.inc', |
|
73 |
), |
|
74 |
'print_pdf_tcpdf_content' => array( |
|
75 |
'variables' => array('pdf' => NULL, 'html' => '', 'font' => array()), |
|
76 |
'file' => 'print_pdf.pages.inc', |
|
77 |
), |
|
78 |
'print_pdf_tcpdf_footer' => array( |
|
79 |
'variables' => array('pdf' => NULL, 'html' => '', 'font' => array()), |
|
80 |
'file' => 'print_pdf.pages.inc', |
|
81 |
), |
|
82 |
'print_pdf_tcpdf_footer2' => array( |
|
83 |
'variables' => array('pdf' => NULL), |
|
84 |
'file' => 'print_pdf.pages.inc', |
|
48 |
'access PDF version' => array( |
|
49 |
'title' => t('Access the PDF version'), |
|
50 |
'description' => t('View the PDF versions and the links to them in the original pages.'), |
|
85 | 51 |
), |
86 | 52 |
); |
87 | 53 |
} |
... | ... | |
91 | 57 |
*/ |
92 | 58 |
function print_pdf_init() { |
93 | 59 |
if (variable_get('print_pdf_autoconfig', PRINT_PDF_AUTOCONFIG_DEFAULT)) { |
94 |
$pdf_dirs = array(); |
|
95 | 60 |
$print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT); |
96 |
|
|
97 |
if (basename($print_pdf_pdf_tool) == 'dompdf_config.inc.php') { |
|
98 |
$pdf_dirs[] = PRINT_PDF_DOMPDF_CACHE_DIR_DEFAULT . '/fonts'; |
|
99 |
} |
|
100 |
elseif (basename($print_pdf_pdf_tool) == 'tcpdf.php') { |
|
101 |
foreach (array('cache', 'images') as $dir) { |
|
102 |
$pdf_dirs[] = PRINT_PDF_TCPDF_CACHE_DIR_DEFAULT . '/' . $dir; |
|
61 |
$tool = explode('|', $print_pdf_pdf_tool); |
|
62 |
$function = $tool[0] . '_pdf_tool_info'; |
|
63 |
$info = function_exists($function) ? $function() : array(); |
|
64 |
|
|
65 |
if (isset($info['public_dirs'])) { |
|
66 |
foreach ($info['public_dirs'] as $dir) { |
|
67 |
$directory = 'public://print_pdf/' . $tool[0] . '/' . $dir; |
|
68 |
/** @var DrupalLocalStreamWrapper $wrapper */ |
|
69 |
$wrapper = file_stream_wrapper_get_instance_by_uri($directory); |
|
70 |
$real_directory_path = $wrapper->getDirectoryPath() . "/" . file_uri_target($directory); |
|
71 |
$result = file_prepare_directory($real_directory_path, FILE_CREATE_DIRECTORY); |
|
72 |
if (!$result) { |
|
73 |
watchdog('print_pdf', 'Failed to create directory "%dir" for %tool.', array('%dir' => $directory, '%tool' => $tool[0]), WATCHDOG_CRITICAL); |
|
74 |
} |
|
103 | 75 |
} |
104 | 76 |
} |
105 |
|
|
106 |
if (!empty($pdf_dirs)) { |
|
107 |
foreach ($pdf_dirs as $pdf_dir) { |
|
108 |
$directory = 'public://' . $pdf_dir; |
|
109 |
file_prepare_directory($directory, FILE_CREATE_DIRECTORY); |
|
110 |
} |
|
77 |
} |
|
78 |
if (variable_get('print_pdf_cache_enabled', PRINT_PDF_CACHE_ENABLED_DEFAULT)) { |
|
79 |
$directory = print_pdf_cache_dir(); |
|
80 |
$wrapper = file_stream_wrapper_get_instance_by_uri($directory); |
|
81 |
$real_directory_path = $wrapper->getDirectoryPath() . "/" . file_uri_target($directory); |
|
82 |
$result = file_prepare_directory($real_directory_path, FILE_MODIFY_PERMISSIONS | FILE_CREATE_DIRECTORY); |
|
83 |
if (!$result) { |
|
84 |
watchdog('print_pdf', 'Failed to create directory "%dir" for print_pdf cache.', array('%dir' => $directory), WATCHDOG_CRITICAL); |
|
111 | 85 |
} |
112 | 86 |
} |
113 | 87 |
} |
114 | 88 |
|
89 |
/** |
|
90 |
* Implements hook_flush_caches(). |
|
91 |
*/ |
|
92 |
function print_pdf_flush_caches() { |
|
93 |
print_pdf_cache_delete(); |
|
94 |
|
|
95 |
return array(); |
|
96 |
} |
|
97 |
|
|
115 | 98 |
/** |
116 | 99 |
* Implements hook_menu(). |
117 | 100 |
*/ |
118 | 101 |
function print_pdf_menu() { |
102 |
$link = print_pdf_print_link(); |
|
119 | 103 |
$items = array(); |
120 | 104 |
|
121 |
$items[PRINTPDF_PATH] = array(
|
|
105 |
$items[$link['path']] = array(
|
|
122 | 106 |
'title' => 'Printer-friendly PDF', |
123 | 107 |
'page callback' => 'print_pdf_controller', |
124 | 108 |
'access arguments' => array('access PDF version'), |
125 | 109 |
'type' => MENU_CALLBACK, |
126 | 110 |
'file' => 'print_pdf.pages.inc', |
127 | 111 |
); |
128 |
$items[PRINTPDF_PATH . '/' . PRINTPDF_PATH] = array(
|
|
112 |
$items[$link['path'] . '/' . $link['path']] = array(
|
|
129 | 113 |
'access callback' => FALSE, |
130 | 114 |
); |
131 | 115 |
$items['admin/config/user-interface/print/pdf'] = array( |
... | ... | |
140 | 124 |
); |
141 | 125 |
$items['admin/config/user-interface/print/pdf/options'] = array( |
142 | 126 |
'title' => 'Options', |
143 |
'weight' => 1, |
|
127 |
'weight' => -1,
|
|
144 | 128 |
'type' => MENU_DEFAULT_LOCAL_TASK, |
145 | 129 |
); |
146 |
$items['admin/config/user-interface/print/pdf/strings'] = array( |
|
147 |
'title' => 'Text strings', |
|
148 |
'description' => 'Override the user-facing strings used in the PDF version.', |
|
149 |
'page callback' => 'drupal_get_form', |
|
150 |
'page arguments' => array('print_pdf_strings_settings'), |
|
151 |
'access arguments' => array('administer print'), |
|
152 |
'weight' => 2, |
|
153 |
'type' => MENU_LOCAL_TASK, |
|
154 |
'file' => 'print_pdf.admin.inc', |
|
155 |
); |
|
156 | 130 |
|
157 | 131 |
return $items; |
158 | 132 |
} |
159 | 133 |
|
134 |
/** |
|
135 |
* Implements hook_variable_info(). |
|
136 |
*/ |
|
137 |
function print_pdf_variable_info($options) { |
|
138 |
$link = print_pdf_print_link(); |
|
139 |
|
|
140 |
$variable['print_pdf_link_text'] = array( |
|
141 |
'title' => t('PDF version'), |
|
142 |
'description' => t('Text used in the link to the PDF version.'), |
|
143 |
'type' => 'string', |
|
144 |
'default' => t($link['text']), |
|
145 |
); |
|
146 |
|
|
147 |
return $variable; |
|
148 |
} |
|
149 |
|
|
160 | 150 |
/** |
161 | 151 |
* Implements hook_block_info(). |
162 | 152 |
*/ |
163 | 153 |
function print_pdf_block_info() { |
164 |
$block['print_pdf-top']['info'] = t('Most PDFd');
|
|
165 |
$block['print_pdf-top']['cache'] = DRUPAL_CACHE_GLOBAL;
|
|
166 |
return $block;
|
|
154 |
$block['print_pdf-top']['info'] = t('Most PDFd'); |
|
155 |
$block['print_pdf-top']['cache'] = DRUPAL_CACHE_GLOBAL; |
|
156 |
return $block; |
|
167 | 157 |
} |
168 | 158 |
|
169 | 159 |
/** |
... | ... | |
171 | 161 |
*/ |
172 | 162 |
function print_pdf_block_view($delta = 0) { |
173 | 163 |
$block = array(); |
174 |
|
|
175 |
switch ($delta) { |
|
176 |
case 'print_pdf-top': |
|
177 |
$block['subject'] = t('Most PDFd'); |
|
178 |
$result = db_query_range("SELECT path FROM {print_pdf_page_counter} LEFT JOIN {node} n ON path = CONCAT('node/', n.nid) WHERE status <> 0 OR status IS NULL ORDER BY totalcount DESC", 0, 3) |
|
179 |
->fetchAll(); |
|
180 |
if (count($result)) { |
|
181 |
$block['content'] = '<div class="item-list"><ul>'; |
|
182 |
foreach ($result as $obj) { |
|
183 |
$block['content'] .= '<li>' . l(_print_get_title($obj->path), $obj->path) . '</li>'; |
|
184 |
} |
|
185 |
$block['content'] .= '</ul></div>'; |
|
186 |
} |
|
187 |
break; |
|
188 |
} |
|
189 |
return $block; |
|
190 |
} |
|
191 |
|
|
192 |
/** |
|
193 |
* Implements hook_requirements(). |
|
194 |
*/ |
|
195 |
function print_pdf_requirements($phase) { |
|
196 |
$requirements = array(); |
|
197 |
$t = get_t(); |
|
198 |
switch ($phase) { |
|
199 |
// At runtime, make sure that a PDF generation tool is selected |
|
200 |
case 'runtime': |
|
201 |
$print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT); |
|
202 |
if (empty($print_pdf_pdf_tool)) { |
|
203 |
$requirements['print_pdf_tool'] = array( |
|
204 |
'title' => $t('Printer, email and PDF versions - PDF generation library'), |
|
205 |
'value' => $t('No PDF tool selected'), |
|
206 |
'description' => $t('Please configure it in the !url.', array('!url' => l($t('PDF settings page'), 'admin/config/user-interface/print/pdf'))), |
|
207 |
'severity' => REQUIREMENT_ERROR, |
|
208 |
); |
|
209 |
} |
|
210 |
else { |
|
211 |
if (!is_file($print_pdf_pdf_tool) || !is_readable($print_pdf_pdf_tool)) { |
|
212 |
$requirements['print_pdf_tool'] = array( |
|
213 |
'title' => $t('Printer, email and PDF versions - PDF generation library'), |
|
214 |
'value' => $t('File not found'), |
|
215 |
'description' => $t('The currently selected PDF generation library (%file) is no longer accessible.', array('%file' => $print_pdf_pdf_tool)), |
|
216 |
'severity' => REQUIREMENT_ERROR, |
|
217 |
); |
|
218 |
} |
|
219 |
elseif (basename($print_pdf_pdf_tool) == 'dompdf_config.inc.php') { |
|
220 |
if (variable_get('print_pdf_autoconfig', PRINT_PDF_AUTOCONFIG_DEFAULT)) { |
|
221 |
$directory = 'public://' . PRINT_PDF_DOMPDF_CACHE_DIR_DEFAULT . '/fonts'; |
|
222 |
if (!is_dir($directory) || !is_writable($directory)) { |
|
223 |
$requirements['print_pdf_tool'] = array( |
|
224 |
'title' => $t('DOMPDF font cache directory'), |
|
225 |
'value' => $t('Non-writable permissions'), |
|
226 |
'description' => $t('You must change the %fontdir permissions to be writable, as dompdf requires write-access to that directory.', array('%fontdir' => $directory)), |
|
227 |
'severity' => REQUIREMENT_ERROR, |
|
228 |
); |
|
229 |
} |
|
230 |
} |
|
231 |
} |
|
232 |
elseif (basename($print_pdf_pdf_tool) == 'tcpdf.php') { |
|
233 |
$version = _print_pdf_tcpdf_version(); |
|
234 |
|
|
235 |
if (version_compare($version, '5.9.001', '<')) { |
|
236 |
$requirements['print_pdf_tool'] = array( |
|
237 |
'title' => $t('Printer, email and PDF versions - PDF generation library'), |
|
238 |
'value' => $t('Unsupported TCPDF version'), |
|
239 |
'description' => $t('The currently selected version of TCPDF (@version) is not supported. Please update to a !url.', array('@version' => $version, '!url' => l($t('newer version'), 'http://sourceforge.net/projects/tcpdf/files/latest'))), |
|
240 |
'severity' => REQUIREMENT_ERROR, |
|
241 |
); |
|
242 |
} |
|
243 |
else { |
|
244 |
$requirements['print_pdf_tool'] = array( |
|
245 |
'title' => $t('Printer, email and PDF versions - PDF generation library'), |
|
246 |
'value' => $t('TCPDF') . ' ' . $version, |
|
247 |
); |
|
248 |
} |
|
249 |
|
|
250 |
if (variable_get('print_pdf_autoconfig', PRINT_PDF_AUTOCONFIG_DEFAULT)) { |
|
251 |
foreach (array('cache', 'images') as $dir) { |
|
252 |
$directory = 'public://' . PRINT_PDF_TCPDF_CACHE_DIR_DEFAULT . '/' . $dir; |
|
253 |
if (!is_dir($directory) || !is_writable($directory)) { |
|
254 |
$requirements['print_pdf_tool_' . $dir] = array( |
|
255 |
'title' => $t('TCPDF directory'), |
|
256 |
'value' => $t('Non-writable permissions'), |
|
257 |
'description' => $t('You must change the %fontdir permissions to be writable, as TCPDF requires write-access to that directory.', array('%fontdir' => $directory)), |
|
258 |
'severity' => REQUIREMENT_ERROR, |
|
259 |
); |
|
260 |
} |
|
261 |
} |
|
262 |
} |
|
263 |
} |
|
264 |
elseif (drupal_substr(basename($print_pdf_pdf_tool, '.exe'), 0, 11) == 'wkhtmltopdf') { |
|
265 |
if (function_exists('is_executable') && !is_executable($print_pdf_pdf_tool)) { |
|
266 |
$requirements['print_pdf_tool'] = array( |
|
267 |
'title' => $t('wkhtmltopdf library'), |
|
268 |
'value' => $t('Non-executable permissions'), |
|
269 |
'description' => $t('You must modify the permissions of the wkhtmltopdf file (%file) to make it executable.', array('%file' => $print_pdf_pdf_tool)), |
|
270 |
'severity' => REQUIREMENT_ERROR, |
|
271 |
); |
|
272 |
} |
|
273 |
else { |
|
274 |
$version = _print_pdf_wkhtmltopdf_version(); |
|
275 |
if (version_compare($version, '0.9.6', '<')) { |
|
276 |
$requirements['print_pdf_tool'] = array( |
|
277 |
'title' => $t('Printer, email and PDF versions - PDF generation library'), |
|
278 |
'value' => $t('Unsupported wkhtmltopdf version'), |
|
279 |
'description' => $t('The currently selected version of wkhtmltopdf (@version) is not supported. Please update to a !url.', array('@version' => $version, '!url' => l($t('newer version'), 'http://code.google.com/p/wkhtmltopdf/'))), |
|
280 |
'severity' => REQUIREMENT_ERROR, |
|
281 |
); |
|
282 |
} |
|
283 |
else { |
|
284 |
$requirements['print_pdf_tool'] = array( |
|
285 |
'title' => $t('Printer, email and PDF versions - PDF generation library'), |
|
286 |
'value' => $t('wkhtmltopdf') . ' ' . $version, |
|
287 |
); |
|
288 |
} |
|
289 |
} |
|
164 |
switch ($delta) { |
|
165 |
case 'print_pdf-top': |
|
166 |
$block['subject'] = t('Most PDFd'); |
|
167 |
$result = db_query_range("SELECT path FROM {print_pdf_page_counter} LEFT JOIN {node} n ON path = CONCAT('node/', n.nid) WHERE status <> 0 OR status IS NULL ORDER BY totalcount DESC", 0, 3) |
|
168 |
->fetchAll(); |
|
169 |
if (count($result)) { |
|
170 |
$items = array(); |
|
171 |
foreach ($result as $obj) { |
|
172 |
$items[] = l(_print_get_title($obj->path), $obj->path); |
|
290 | 173 |
} |
174 |
$block['content'] = theme('item_list', array('items' => $items, 'type' => 'ul')); |
|
291 | 175 |
} |
292 | 176 |
break; |
293 | 177 |
} |
294 |
return $requirements; |
|
295 |
} |
|
296 |
|
|
297 |
/** |
|
298 |
* Implements hook_node_view(). |
|
299 |
*/ |
|
300 |
function print_pdf_node_view($node, $view_mode) { |
|
301 |
$print_pdf_link_pos = variable_get('print_pdf_link_pos', drupal_json_decode(PRINT_PDF_LINK_POS_DEFAULT)); |
|
302 |
$print_pdf_link_use_alias = variable_get('print_pdf_link_use_alias', PRINT_PDF_LINK_USE_ALIAS_DEFAULT); |
|
303 |
|
|
304 |
foreach (array('node', 'comment') as $type) { |
|
305 |
$allowed_type = print_pdf_link_allowed(array('type' => $type, 'node' => $node, 'view_mode' => $view_mode)); |
|
306 |
if (($allowed_type) && !empty($print_pdf_link_pos['link'])) { |
|
307 |
drupal_add_css(drupal_get_path('module', 'print') . '/css/printlinks.css'); |
|
308 |
$links = array(); |
|
309 |
$format = theme('print_pdf_format_link'); |
|
310 |
|
|
311 |
// Show book link |
|
312 |
if ($allowed_type === PRINT_ALLOW_BOOK_LINK) { |
|
313 |
$links['book_pdf'] = array( |
|
314 |
'href' => PRINTPDF_PATH . '/book/export/html/' . $node->nid, |
|
315 |
'title' => $format['text'], |
|
316 |
'attributes' => $format['attributes'], |
|
317 |
'html' => $format['html'], |
|
318 |
); |
|
319 |
} |
|
320 |
elseif ($allowed_type === PRINT_ALLOW_NORMAL_LINK) { |
|
321 |
$path = (($print_pdf_link_use_alias) && ($alias = drupal_lookup_path('alias', 'node/' . $node->nid))) ? $alias : $node->nid; |
|
322 |
|
|
323 |
$links['print_pdf'] = array( |
|
324 |
'href' => PRINTPDF_PATH . '/' . $path, |
|
325 |
'title' => $format['text'], |
|
326 |
'attributes' => $format['attributes'], |
|
327 |
'html' => $format['html'], |
|
328 |
'query' => print_query_string_encode($_GET, array('q')), |
|
329 |
); |
|
330 |
} |
|
331 |
|
|
332 |
$link_content = array( |
|
333 |
'#theme' => 'links', |
|
334 |
'#links' => $links, |
|
335 |
'#attributes' => array('class' => array('links', 'inline')), |
|
336 |
); |
|
337 |
|
|
338 |
if ($type == 'node') { |
|
339 |
$node->content['links']['print_pdf'] = $link_content; |
|
340 |
} |
|
341 |
elseif (($type == 'comment') && isset($node->content['comments']['comments'])) { |
|
342 |
foreach ($node->content['comments']['comments'] as $cid => $comment) { |
|
343 |
if (is_numeric($cid)) { |
|
344 |
$link_content['#links']['print_pdf']['query']['comment'] = $cid; |
|
345 |
$node->content['comments']['comments'][$cid]['links']['print_pdf'] = $link_content; |
|
346 |
} |
|
347 |
} |
|
348 |
} |
|
349 |
} |
|
350 |
} |
|
351 |
|
|
352 |
// Insert content corner links |
|
353 |
if ((!empty($print_pdf_link_pos['corner'])) && ($view_mode == 'full')) { |
|
354 |
$node->content['print_links']['#markup'] .= print_pdf_insert_link(NULL, $node); |
|
355 |
} |
|
356 |
} |
|
357 |
|
|
358 |
/** |
|
359 |
* Implements hook_help(). |
|
360 |
*/ |
|
361 |
function print_pdf_help($path, $arg) { |
|
362 |
$print_pdf_link_pos = variable_get('print_pdf_link_pos', drupal_json_decode(PRINT_PDF_LINK_POS_DEFAULT)); |
|
363 |
if (($path !== 'node/%') && !(empty($print_pdf_link_pos['help']))) { |
|
364 |
static $output = FALSE; |
|
365 |
|
|
366 |
if ($output === FALSE) { |
|
367 |
$output = TRUE; |
|
368 |
|
|
369 |
$link = print_pdf_insert_link(); |
|
370 |
if ($link) { |
|
371 |
return "<span class='print-syslink'>$link</span>"; |
|
372 |
} |
|
373 |
} |
|
374 |
} |
|
178 |
return $block; |
|
375 | 179 |
} |
376 | 180 |
|
377 | 181 |
/** |
... | ... | |
383 | 187 |
$ids[] = $node->nid; |
384 | 188 |
} |
385 | 189 |
|
386 |
$result = db_query('SELECT nid, link, comments, url_list FROM {print_pdf_node_conf} WHERE nid IN (:nids)', array(':nids' => $ids))->fetchAllAssoc('nid'); |
|
190 |
$link = print_pdf_print_link(); |
|
191 |
|
|
192 |
$size = 'print_' . $link['format'] . '_size'; |
|
193 |
$orientation = 'print_' . $link['format'] . '_orientation'; |
|
194 |
|
|
195 |
$result = db_query('SELECT nid, size, orientation FROM {print_pdf_node_conf} WHERE nid IN (:nids)', array(':nids' => $ids))->fetchAllAssoc('nid'); |
|
387 | 196 |
|
388 | 197 |
foreach ($nodes as $node) { |
389 |
$node->print_pdf_display = isset($result[$node->nid]) ? intval($result[$node->nid]->link) : variable_get('print_pdf_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT); |
|
390 |
$node->print_pdf_display_comment = isset($result[$node->nid]) ? intval($result[$node->nid]->comments) : variable_get('print_pdf_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT); |
|
391 |
$node->print_pdf_display_urllist = isset($result[$node->nid]) ? intval($result[$node->nid]->url_list) : variable_get('print_pdf_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT); |
|
198 |
$node->{$size} = (isset($result[$node->nid]) && !empty($result[$node->nid]->size)) ? $result[$node->nid]->size : variable_get($size . '_' . $node->type); |
|
199 |
$node->{$orientation} = (isset($result[$node->nid]) && !empty($result[$node->nid]->orientation)) ? $result[$node->nid]->orientation : variable_get($orientation . '_' . $node->type); |
|
392 | 200 |
} |
393 | 201 |
} |
394 | 202 |
|
... | ... | |
396 | 204 |
* Implements hook_node_insert(). |
397 | 205 |
*/ |
398 | 206 |
function print_pdf_node_insert($node) { |
399 |
if (user_access('administer print') || user_access('node-specific print configuration')) { |
|
400 |
if (!isset($node->print_pdf_display)) $node->print_pdf_display = variable_get('print_pdf_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT); |
|
401 |
if (!isset($node->print_pdf_display_comment)) $node->print_pdf_display_comment = variable_get('print_pdf_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT); |
|
402 |
if (!isset($node->print_pdf_display_urllist)) $node->print_pdf_display_urllist = variable_get('print_pdf_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT); |
|
403 |
|
|
404 |
_print_pdf_node_conf_modify($node->nid, $node->print_pdf_display, $node->print_pdf_display_comment, $node->print_pdf_display_urllist); |
|
405 |
} |
|
207 |
print_pdf_node_update($node); |
|
406 | 208 |
} |
407 | 209 |
|
408 | 210 |
/** |
... | ... | |
410 | 212 |
*/ |
411 | 213 |
function print_pdf_node_update($node) { |
412 | 214 |
if (user_access('administer print') || user_access('node-specific print configuration')) { |
413 |
if (!isset($node->print_pdf_display) || $node->print_pdf_display === NULL) { |
|
414 |
$node->print_pdf_display = variable_get('print_pdf_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT); |
|
415 |
} |
|
416 |
if (!isset($node->print_pdf_display_comment) || $node->print_pdf_display_comment === NULL) { |
|
417 |
$node->print_pdf_display_comment = variable_get('print_pdf_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT); |
|
215 |
$link = print_pdf_print_link(); |
|
216 |
|
|
217 |
$size = 'print_' . $link['format'] . '_size'; |
|
218 |
$orientation = 'print_' . $link['format'] . '_orientation'; |
|
219 |
|
|
220 |
if (!isset($node->{$size})) { |
|
221 |
$node->{$size} = variable_get($size . '_' . $node->type); |
|
418 | 222 |
} |
419 |
if (!isset($node->print_pdf_display_urllist) || $node->print_pdf_display_urllist === NULL) {
|
|
420 |
$node->print_pdf_display_urllist = variable_get('print_pdf_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
|
|
223 |
if (!isset($node->{$orientation})) {
|
|
224 |
$node->{$orientation} = variable_get($orientation . '_' . $node->type);
|
|
421 | 225 |
} |
422 | 226 |
|
423 |
_print_pdf_node_conf_modify($node->nid, $node->print_pdf_display, $node->print_pdf_display_comment, $node->print_pdf_display_urllist); |
|
227 |
db_merge('print_pdf_node_conf') |
|
228 |
->key(array('nid' => $node->nid)) |
|
229 |
->fields(array( |
|
230 |
'size' => $node->{$size}, |
|
231 |
'orientation' => $node->{$orientation}, |
|
232 |
)) |
|
233 |
->execute(); |
|
424 | 234 |
} |
235 |
|
|
236 |
print_pdf_cache_delete($node->nid); |
|
425 | 237 |
} |
426 | 238 |
|
427 | 239 |
/** |
428 | 240 |
* Implements hook_node_delete(). |
429 | 241 |
*/ |
430 | 242 |
function print_pdf_node_delete($node) { |
431 |
db_delete('print_pdf_node_conf') |
|
432 |
->condition('nid', $node->nid) |
|
433 |
->execute(); |
|
434 | 243 |
db_delete('print_pdf_page_counter') |
435 | 244 |
->condition('path', 'node/' . $node->nid) |
436 | 245 |
->execute(); |
246 |
|
|
247 |
print_pdf_cache_delete($node->nid); |
|
437 | 248 |
} |
438 | 249 |
|
439 | 250 |
/** |
440 | 251 |
* Implements hook_form_alter(). |
441 | 252 |
*/ |
442 | 253 |
function print_pdf_form_alter(&$form, &$form_state, $form_id) { |
443 |
// Add the node-type settings option to activate the PDF version link
|
|
254 |
// Add the node-type settings to activate the printer-friendly version link.
|
|
444 | 255 |
if ((user_access('administer print') || user_access('node-specific print configuration')) && |
445 | 256 |
(($form_id == 'node_type_form') || !empty($form['#node_edit_form']))) { |
446 |
$form['print']['pdf_label'] = array( |
|
447 |
'#type' => 'markup', |
|
448 |
'#markup' => '<p><strong>' . t('PDF version') . '</strong></p>', |
|
449 |
); |
|
257 |
$link = print_pdf_print_link(); |
|
450 | 258 |
|
451 |
$form['print']['print_pdf_display'] = array( |
|
452 |
'#type' => 'checkbox', |
|
453 |
'#title' => t('Show link'), |
|
454 |
); |
|
455 |
$form['print']['print_pdf_display_comment'] = array( |
|
456 |
'#type' => 'checkbox', |
|
457 |
'#title' => t('Show link in individual comments'), |
|
259 |
$size = 'print_' . $link['format'] . '_size'; |
|
260 |
$orientation = 'print_' . $link['format'] . '_orientation'; |
|
261 |
|
|
262 |
$form['print']['print_' . $link['format']][$size] = array( |
|
263 |
'#type' => 'select', |
|
264 |
'#title' => t('Paper size'), |
|
265 |
'#options' => _print_pdf_paper_sizes(TRUE), |
|
266 |
'#description' => t('Choose the paper size of the generated PDF.'), |
|
458 | 267 |
); |
459 |
$form['print']['print_pdf_display_urllist'] = array( |
|
460 |
'#type' => 'checkbox', |
|
461 |
'#title' => t('Show Printer-friendly URLs list'), |
|
268 |
|
|
269 |
$form['print']['print_' . $link['format']][$orientation] = array( |
|
270 |
'#type' => 'select', |
|
271 |
'#title' => t('Page orientation'), |
|
272 |
'#options' => array( |
|
273 |
'' => 'Unchanged', |
|
274 |
'portrait' => t('Portrait'), |
|
275 |
'landscape' => t('Landscape'), |
|
276 |
), |
|
277 |
'#description' => t('Choose the page orientation of the generated PDF.'), |
|
462 | 278 |
); |
463 | 279 |
|
464 | 280 |
if ($form_id == 'node_type_form') { |
465 |
$form['print']['print_pdf_display']['#default_value'] = variable_get('print_pdf_display_' . $form['#node_type']->type, PRINT_TYPE_SHOW_LINK_DEFAULT); |
|
466 |
$form['print']['print_pdf_display_comment']['#default_value'] = variable_get('print_pdf_display_comment_' . $form['#node_type']->type, PRINT_TYPE_COMMENT_LINK_DEFAULT); |
|
467 |
$form['print']['print_pdf_display_urllist']['#default_value'] = variable_get('print_pdf_display_urllist_' . $form['#node_type']->type, PRINT_TYPE_URLLIST_DEFAULT); |
|
281 |
$form['print']['print_' . $link['format']][$size]['#default_value'] = variable_get($size . '_' . $form['#node_type']->type); |
|
282 |
$form['print']['print_' . $link['format']][$orientation]['#default_value'] = variable_get($orientation . '_' . $form['#node_type']->type); |
|
468 | 283 |
} |
469 | 284 |
else { |
470 | 285 |
$node = $form['#node']; |
471 |
$form['print']['print_pdf_display']['#default_value'] = isset($node->print_pdf_display) ? $node->print_pdf_display : variable_get('print_pdf_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT); |
|
472 |
$form['print']['print_pdf_display_comment']['#default_value'] = isset($node->print_pdf_display_comment) ? $node->print_pdf_display_comment : variable_get('print_pdf_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT); |
|
473 |
$form['print']['print_pdf_display_urllist']['#default_value'] = isset($node->print_pdf_display_urllist) ? $node->print_pdf_display_urllist : variable_get('print_pdf_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT); |
|
286 |
$form['print']['print_' . $link['format']][$size]['#default_value'] = isset($node->{$size}) ? $node->{$size} : variable_get($size . '_' . $node->type); |
|
287 |
$form['print']['print_' . $link['format']][$orientation]['#default_value'] = isset($node->{$orientation}) ? $node->{$orientation} : variable_get($orientation . '_' . $node->type); |
|
474 | 288 |
} |
475 | 289 |
} |
476 | 290 |
} |
477 | 291 |
|
478 | 292 |
/** |
479 |
* Update the print_pdf_node_conf table to reflect the given attributes
|
|
293 |
* Auxiliary function to display a formatted PDF version link.
|
|
480 | 294 |
* |
481 |
* If updating to the default values, delete the record. |
|
295 |
* Function made available so that developers may call this function from |
|
296 |
* their defined pages/blocks. |
|
297 |
* |
|
298 |
* @param string $path |
|
299 |
* Path to be used in the link. If not specified, the current URL is used. |
|
300 |
* @param object $node |
|
301 |
* Node object, to be used in checking node access. If the path argument is |
|
302 |
* not provided, the path used will be node/nid. |
|
303 |
* @param string $location |
|
304 |
* Where in the page where the link is being inserted ('link', 'corner', |
|
305 |
* 'block', 'help'). |
|
482 | 306 |
* |
483 |
* @param $nid |
|
484 |
* value of the nid field (primary key) |
|
485 |
* @param $link |
|
486 |
* value of the link field (0 or 1) |
|
487 |
* @param $comments |
|
488 |
* value of the comments field (0 or 1) |
|
489 |
* @param $url_list |
|
490 |
* value of the url_list field (0 or 1) |
|
307 |
* @return bool |
|
308 |
* string with the HTML link to the printer-friendly page |
|
309 |
* |
|
310 |
* @ingroup print_api |
|
491 | 311 |
*/ |
492 |
function _print_pdf_node_conf_modify($nid, $link, $comments, $url_list) { |
|
493 |
db_merge('print_pdf_node_conf') |
|
494 |
->key(array('nid' => $nid)) |
|
495 |
->fields(array( |
|
496 |
'link' => $link, |
|
497 |
'comments' => $comments, |
|
498 |
'url_list' => $url_list, |
|
499 |
)) |
|
500 |
->execute(); |
|
312 |
function print_pdf_insert_link($path = NULL, $node = NULL, $location = '') { |
|
313 |
if (function_exists('print_ui_insert_link')) { |
|
314 |
return print_ui_insert_link(print_pdf_print_link(), array( |
|
315 |
'path' => $path, |
|
316 |
'node' => $node, |
|
317 |
'location' => $location, |
|
318 |
)); |
|
319 |
} |
|
320 |
else { |
|
321 |
return FALSE; |
|
322 |
} |
|
501 | 323 |
} |
502 | 324 |
|
503 | 325 |
/** |
504 |
* Format the PDF version link |
|
326 |
* Check if the link to the PDF version is allowed depending on the settings. |
|
327 |
* |
|
328 |
* @param array $args |
|
329 |
* Array containing the possible parameters: |
|
330 |
* view_mode, node, type, path. |
|
505 | 331 |
* |
506 |
* @return |
|
507 |
* array of formatted attributes |
|
508 |
* @ingroup themeable |
|
332 |
* @return bool |
|
333 |
* FALSE if not allowed, TRUE otherwise |
|
509 | 334 |
*/ |
510 |
function theme_print_pdf_format_link() { |
|
511 |
$print_pdf_link_class = variable_get('print_pdf_link_class', PRINT_PDF_LINK_CLASS_DEFAULT); |
|
512 |
$print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT); |
|
513 |
$print_pdf_show_link = variable_get('print_pdf_show_link', PRINT_PDF_SHOW_LINK_DEFAULT); |
|
514 |
$print_pdf_link_text = filter_xss(variable_get('print_pdf_link_text', t('PDF version'))); |
|
515 |
|
|
516 |
$img = drupal_get_path('module', 'print') . '/icons/pdf_icon.gif'; |
|
517 |
$title = t('Display a PDF version of this page.'); |
|
518 |
$class = strip_tags($print_pdf_link_class); |
|
519 |
$new_window = ($print_pdf_content_disposition == 1); |
|
520 |
$format = _print_format_link_aux($print_pdf_show_link, $print_pdf_link_text, $img); |
|
521 |
|
|
522 |
return array('text' => $format['text'], |
|
523 |
'html' => $format['html'], |
|
524 |
'attributes' => print_fill_attributes($title, $class, $new_window), |
|
525 |
); |
|
335 |
function print_pdf_link_allowed($args) { |
|
336 |
$print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT); |
|
337 |
|
|
338 |
return (user_access('access PDF version') && (!empty($print_pdf_pdf_tool))); |
|
526 | 339 |
} |
527 | 340 |
|
528 | 341 |
/** |
529 |
* Auxiliary function to display a formatted PDF version link |
|
530 |
* |
|
531 |
* Function made available so that developers may call this function from |
|
532 |
* their defined pages/blocks. |
|
533 |
* |
|
534 |
* @param $path |
|
535 |
* path of the original page (optional). If not specified, the current URL |
|
536 |
* is used |
|
537 |
* @param $node |
|
538 |
* an optional node object, to be used in defining the path, if used, the |
|
539 |
* path argument is irrelevant |
|
540 |
* @return |
|
541 |
* string with the HTML link to the printer-friendly page |
|
342 |
* Implements hook_cron(). |
|
542 | 343 |
*/ |
543 |
function print_pdf_insert_link($path = NULL, $node = NULL) { |
|
544 |
if ($node !== NULL) { |
|
545 |
$nid = $node->nid; |
|
546 |
$path = 'node/' . $nid; |
|
547 |
$allowed_type = print_pdf_link_allowed(array('node' => $node)); |
|
548 |
} |
|
549 |
else { |
|
550 |
if ($path === NULL) { |
|
551 |
$nid = preg_replace('!^node/([\d]+)!', '$1', $_GET['q']); |
|
552 |
$path = $_GET['q']; |
|
553 |
} |
|
554 |
else { |
|
555 |
$nid = NULL; |
|
556 |
} |
|
557 |
$allowed_type = print_pdf_link_allowed(array('path' => $path)); |
|
558 |
} |
|
344 |
function print_pdf_cron() { |
|
345 |
print_pdf_cache_clean(); |
|
346 |
} |
|
559 | 347 |
|
560 |
if ($allowed_type) { |
|
561 |
if ($nid !== NULL) { |
|
562 |
if ($allowed_type === PRINT_ALLOW_BOOK_LINK) { |
|
563 |
$path = 'book/export/html/' . $nid; |
|
564 |
} |
|
565 |
else { |
|
566 |
if (variable_get('print_pdf_link_use_alias', PRINT_PDF_LINK_USE_ALIAS_DEFAULT) && ($alias = drupal_lookup_path('alias', $path))) { |
|
567 |
$path = $alias; |
|
568 |
} |
|
569 |
else { |
|
570 |
$path = $nid; |
|
571 |
} |
|
348 |
/** |
|
349 |
* Removes pdf files for nodes/paths if they are older than the lifetime. |
|
350 |
*/ |
|
351 |
function print_pdf_cache_clean() { |
|
352 |
$lifetime = variable_get('print_pdf_cache_lifetime', PRINT_PDF_CACHE_LIFETIME_DEFAULT); |
|
353 |
|
|
354 |
if ($lifetime > 0) { |
|
355 |
$files = file_scan_directory(print_pdf_cache_dir(), '!\d+\.pdf$!'); |
|
356 |
foreach ($files as $file) { |
|
357 |
// For all files in the cache directory, see when they were last accessed. |
|
358 |
$result = db_query("SELECT timestamp FROM {print_pdf_page_counter} WHERE path = :path", array(':path' => 'node/' . $file->name)) |
|
359 |
->fetchField(); |
|
360 |
|
|
361 |
// Keep the file only if last access was within the cache max life value. |
|
362 |
if (($result === FALSE) || ($result + $lifetime < REQUEST_TIME)) { |
|
363 |
print_pdf_cache_delete($file->name); |
|
572 | 364 |
} |
573 |
$path = PRINTPDF_PATH . '/' . $path; |
|
574 |
$query = print_query_string_encode($_GET, array('q')); |
|
575 | 365 |
} |
576 |
else { |
|
577 |
$query = NULL; |
|
578 |
} |
|
579 |
drupal_add_css(drupal_get_path('module', 'print') . '/css/printlinks.css'); |
|
580 |
$format = theme('print_pdf_format_link'); |
|
581 |
return '<span class="print_pdf">' . l($format['text'], $path, array('attributes' => $format['attributes'], 'query' => $query, 'absolute' => TRUE, 'html' => $format['html'])) . '</span>'; |
|
582 |
} |
|
583 |
else { |
|
584 |
return FALSE; |
|
585 | 366 |
} |
586 | 367 |
} |
587 | 368 |
|
588 | 369 |
/** |
589 |
* Check if the link to the PDF version is allowed depending on the settings
|
|
370 |
* Returns the cache directory.
|
|
590 | 371 |
* |
591 |
* @param $args |
|
592 |
* array containing the possible parameters: |
|
593 |
* teaser, node, type, path |
|
594 |
* @return |
|
595 |
* FALSE if not allowed |
|
596 |
* PRINT_ALLOW_NORMAL_LINK if a normal link is allowed |
|
597 |
* PRINT_ALLOW_BOOK_LINK if a link is allowed in a book node |
|
372 |
* @return string |
|
373 |
* The scheme://path of the cache directory |
|
598 | 374 |
*/ |
599 |
function print_pdf_link_allowed($args) { |
|
600 |
$view_mode = isset($args['view_mode']) ? $args['view_mode'] : ''; |
|
601 |
$print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT); |
|
602 |
if ((($view_mode == 'teaser') && !variable_get('print_pdf_link_teaser', PRINT_PDF_LINK_TEASER_DEFAULT)) |
|
603 |
|| !in_array($view_mode, array('full', 'teaser', '')) || !user_access('access PDF version') || (empty($print_pdf_pdf_tool))) { |
|
604 |
// If the teaser link is disabled or the user is not allowed |
|
605 |
return FALSE; |
|
375 |
function print_pdf_cache_dir() { |
|
376 |
$scheme = 'private'; |
|
377 |
if (!file_stream_wrapper_valid_scheme($scheme)) { |
|
378 |
$scheme = 'temporary'; |
|
606 | 379 |
} |
607 |
if (!empty($args['path'])) { |
|
608 |
$nid = preg_replace('!^node/!', '', drupal_get_normal_path($args['path'])); |
|
609 |
if (ctype_digit($nid)) { |
|
610 |
$args['node'] = node_load($nid); |
|
611 |
} |
|
612 |
} |
|
613 |
if (!empty($args['node'])) { |
|
614 |
static $node_type = FALSE; |
|
380 |
return $scheme . '://print_pdf/cache'; |
|
381 |
} |
|
615 | 382 |
|
616 |
$node = $args['node']; |
|
617 |
if (isset($node->type)) { |
|
618 |
$node_type = $node->type; |
|
619 |
} |
|
620 |
// Node |
|
621 |
$print_pdf_node_link_visibility = variable_get('print_pdf_node_link_visibility', PRINT_PDF_NODE_LINK_VISIBILITY_DEFAULT); |
|
622 |
$print_pdf_node_link_pages = variable_get('print_pdf_node_link_pages', PRINT_PDF_NODE_LINK_PAGES_DEFAULT); |
|
383 |
/** |
|
384 |
* Deletes one or more files from the PDF cache directory. |
|
385 |
* |
|
386 |
* @param int $nid |
|
387 |
* The node ID of the page for which the cached PDF should be deleted. |
|
388 |
* If not provided, the entire cache directory will be deleted. |
|
389 |
*/ |
|
390 |
function print_pdf_cache_delete($nid = NULL) { |
|
391 |
$directory = print_pdf_cache_dir(); |
|
623 | 392 |
|
624 |
if (!_print_page_match($print_pdf_node_link_visibility, "node/" . $node->nid, $print_pdf_node_link_pages)) { |
|
625 |
// Page not in visibility list |
|
626 |
return FALSE; |
|
627 |
} |
|
628 |
elseif (isset($args['type']) && ($args['type'] == 'comment') && isset($node_type)) { |
|
629 |
// Link is for a comment, return the configured setting |
|
630 |
// Cache this statically to avoid duplicate queries for every comment. |
|
631 |
static $res = array(); |
|
632 |
if (!isset($res[$node->nid])) { |
|
633 |
$res[$node->nid] = db_query("SELECT comments FROM {print_pdf_node_conf} WHERE nid = :nid", array(':nid' => $node->nid))->fetchField(); |
|
634 |
} |
|
635 |
$print_display_comment = ($res && ($res[$node->nid] !== FALSE)) ? $res[$node->nid] : variable_get('print_pdf_display_comment_' . $node_type, PRINT_TYPE_COMMENT_LINK_DEFAULT); |
|
636 |
if ($print_display_comment) { |
|
637 |
return PRINT_ALLOW_NORMAL_LINK; |
|
638 |
} |
|
639 |
} |
|
640 |
else { |
|
641 |
// Node link |
|
642 |
if (isset($node->print_pdf_display) && !$node->print_pdf_display) { |
|
643 |
// Link for this node is disabled |
|
644 |
return FALSE; |
|
645 |
} |
|
646 |
elseif (isset($node->book)) { |
|
647 |
// Node is a book; |
|
648 |
$print_pdf_book_link = variable_get('print_pdf_book_link', PRINT_PDF_BOOK_LINK_DEFAULT); |
|
649 |
switch ($print_pdf_book_link) { |
|
650 |
case 1: |
|
651 |
if (user_access('access printer-friendly version')) { |
|
652 |
return PRINT_ALLOW_BOOK_LINK; |
|
653 |
} |
|
654 |
break; |
|
655 |
case 2: |
|
656 |
return PRINT_ALLOW_NORMAL_LINK; |
|
657 |
} |
|
658 |
} |
|
659 |
else { |
|
660 |
return PRINT_ALLOW_NORMAL_LINK; |
|
661 |
} |
|
393 |
if ($nid) { |
|
394 |
$filename = $directory . '/' . $nid . '.pdf'; |
|
395 |
if (is_file($filename)) { |
|
396 |
file_unmanaged_delete($filename); |
|
662 | 397 |
} |
663 | 398 |
} |
664 | 399 |
else { |
665 |
// 'System' page |
|
666 |
$print_pdf_sys_link_visibility = variable_get('print_pdf_sys_link_visibility', PRINT_PDF_SYS_LINK_VISIBILITY_DEFAULT); |
|
667 |
$print_pdf_sys_link_pages = variable_get('print_pdf_sys_link_pages', PRINT_PDF_SYS_LINK_PAGES_DEFAULT); |
|
668 |
|
|
669 |
return _print_page_match($print_pdf_sys_link_visibility, $_GET['q'], $print_pdf_sys_link_pages); |
|
400 |
// If no nid is provided, flush the entire cache. |
|
401 |
if (is_dir($directory)) { |
|
402 |
file_unmanaged_delete_recursive($directory); |
|
403 |
} |
|
670 | 404 |
} |
671 |
return FALSE; |
|
672 | 405 |
} |
673 | 406 |
|
674 | 407 |
/** |
675 |
* Find out the version of the TCPDF library |
|
408 |
* Displays the PDF as inline or a downloadable file. |
|
409 |
* |
|
410 |
* @param string $pdf |
|
411 |
* PDF content string. |
|
412 |
* @param string $filename |
|
413 |
* Filename of the generated PDF. |
|
414 |
* |
|
415 |
* @return string |
|
416 |
* The disposed PDF file |
|
676 | 417 |
*/ |
677 |
function _print_pdf_tcpdf_version() { |
|
678 |
$print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT); |
|
679 |
if (variable_get('print_pdf_autoconfig', PRINT_PDF_AUTOCONFIG_DEFAULT)) { |
|
680 |
// prevent TCPDF default configs |
|
681 |
define('K_TCPDF_EXTERNAL_CONFIG', TRUE); |
|
418 |
function print_pdf_dispose_content($pdf, $filename) { |
|
419 |
if (headers_sent()) { |
|
420 |
exit('Unable to stream pdf: headers already sent'); |
|
682 | 421 |
} |
683 |
require_once(DRUPAL_ROOT . '/' . $print_pdf_pdf_tool); |
|
422 |
header('Cache-Control: private'); |
|
423 |
header('Content-Type: application/pdf'); |
|
684 | 424 |
|
685 |
// Hide warnings, as some TCPDF constants may still be undefined |
|
686 |
if (class_exists('TCPDF')) { |
|
687 |
@$pdf = new TCPDF(); |
|
425 |
$content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT); |
|
426 |
$attachment = ($content_disposition == 2) ? 'attachment' : 'inline'; |
|
688 | 427 |
|
689 |
if (class_exists('TCPDF_STATIC')) { |
|
690 |
return TCPDF_STATIC::getTCPDFVersion(); |
|
691 |
} |
|
692 |
elseif (method_exists($pdf, 'getTCPDFVersion')) { |
|
693 |
return $pdf->getTCPDFVersion(); |
|
694 |
} |
|
695 |
elseif (defined('PDF_PRODUCER')) { |
|
696 |
sscanf(PDF_PRODUCER, "TCPDF %s", $version); |
|
428 |
header("Content-Disposition: $attachment; filename=\"$filename\""); |
|
697 | 429 |
|
698 |
return $version;
|
|
699 |
}
|
|
700 |
} |
|
701 |
return 'unknown';
|
|
430 |
echo $pdf;
|
|
431 |
flush();
|
|
432 |
|
|
433 |
return TRUE;
|
|
702 | 434 |
} |
703 | 435 |
|
704 | 436 |
/** |
705 |
* Find out the version of the wkhtmltopdf library |
|
437 |
* Generate a PDF version of the provided HTML. |
|
438 |
* |
|
439 |
* @param string $html |
|
440 |
* HTML content of the PDF. |
|
441 |
* @param array $meta |
|
442 |
* Meta information to be used in the PDF |
|
443 |
* - url: original URL |
|
444 |
* - name: author's name |
|
445 |
* - title: Page title |
|
446 |
* - node: node object. |
|
447 |
* @param string $filename |
|
448 |
* (optional) Filename of the generated PDF. |
|
449 |
* @param string $paper_size |
|
450 |
* (optional) Paper size of the generated PDF. |
|
451 |
* @param string $page_orientation |
|
452 |
* (optional) Page orientation of the generated PDF. |
|
453 |
* |
|
454 |
* @return string|null |
|
455 |
* generated PDF page, or NULL in case of error |
|
456 |
* |
|
457 |
* @see print_pdf_controller() |
|
458 |
* |
|
459 |
* @ingroup print_api |
|
706 | 460 |
*/ |
707 |
function _print_pdf_wkhtmltopdf_version() { |
|
708 |
$print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT); |
|
709 |
$descriptor = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')); |
|
710 |
|
|
711 |
$cmd = '"' . realpath($print_pdf_pdf_tool) . '" --version'; |
|
712 |
$process = proc_open($cmd, $descriptor, $pipes, NULL, NULL); |
|
713 |
if (is_resource($process)) { |
|
714 |
$content = stream_get_contents($pipes[1]); |
|
715 |
$out = preg_match('!.*?(\d+\.\d+\.\d+).*$!m', $content, $matches); |
|
716 |
fclose($pipes[0]); |
|
717 |
fclose($pipes[1]); |
|
718 |
fclose($pipes[2]); |
|
719 |
$retval = proc_close($process); |
|
461 |
function print_pdf_generate_html($html, $meta, $filename = NULL, $paper_size = NULL, $page_orientation = NULL) { |
|
462 |
$pdf_tool = explode('|', variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT)); |
|
463 |
|
|
464 |
module_load_include('inc', $pdf_tool[0], $pdf_tool[0] . '.pages'); |
|
465 |
|
|
466 |
$function = $pdf_tool[0] . '_print_pdf_generate'; |
|
467 |
$pdf = function_exists($function) ? $function($html, $meta, $paper_size, $page_orientation) : NULL; |
|
468 |
if ($filename) { |
|
469 |
return print_pdf_dispose_content($pdf, $filename); |
|
720 | 470 |
} |
721 | 471 |
|
722 |
return ($matches[1]);
|
|
472 |
return $pdf;
|
|
723 | 473 |
} |
724 | 474 |
|
725 | 475 |
/** |
... | ... | |
731 | 481 |
'path' => drupal_get_path('module', 'print_pdf'), |
732 | 482 |
); |
733 | 483 |
} |
484 |
|
|
485 |
/** |
|
486 |
* Lists all possible paper sizes. |
|
487 |
* |
|
488 |
* @param bool $include_default |
|
489 |
* Flag indicating whether to include the tool's default value. |
|
490 |
* |
|
491 |
* @return array |
|
492 |
* array of strings with the available paper sizes |
|
493 |
*/ |
|
494 |
function _print_pdf_paper_sizes($include_default = FALSE) { |
|
495 |
$ret = ($include_default) ? array('' => 'Unchanged') : array(); |
|
496 |
|
|
497 |
$ret += array( |
|
498 |
'4A0' => '4A0', |
|
499 |
'2A0' => '2A0', |
|
500 |
'A0' => 'A0', |
|
501 |
'A1' => 'A1', |
|
502 |
'A2' => 'A2', |
|
503 |
'A3' => 'A3', |
|
504 |
'A4' => 'A4', |
|
505 |
'A5' => 'A5', |
|
506 |
'A6' => 'A6', |
|
507 |
'A7' => 'A7', |
|
508 |
'A8' => 'A8', |
|
509 |
'A9' => 'A9', |
|
510 |
'A10' => 'A10', |
|
511 |
'B0' => 'B0', |
|
512 |
'B1' => 'B1', |
|
513 |
'B2' => 'B2', |
|
514 |
'B3' => 'B3', |
|
515 |
'B4' => 'B4', |
|
516 |
'B5' => 'B5', |
|
517 |
'B6' => 'B6', |
|
518 |
'B7' => 'B7', |
|
519 |
'B8' => 'B8', |
|
520 |
'B9' => 'B9', |
|
521 |
'B10' => 'B10', |
|
522 |
'C0' => 'C0', |
|
523 |
'C1' => 'C1', |
|
524 |
'C2' => 'C2', |
|
525 |
'C3' => 'C3', |
|
526 |
'C4' => 'C4', |
|
527 |
'C5' => 'C5', |
|
528 |
'C6' => 'C6', |
|
529 |
'C7' => 'C7', |
|
530 |
'C8' => 'C8', |
|
531 |
'C9' => 'C9', |
|
532 |
'C10' => 'C10', |
|
533 |
'RA0' => 'RA0', |
|
534 |
'RA1' => 'RA1', |
|
535 |
'RA2' => 'RA2', |
|
536 |
'RA3' => 'RA3', |
|
537 |
'RA4' => 'RA4', |
|
538 |
'SRA0' => 'SRA0', |
|
539 |
'SRA1' => 'SRA1', |
|
540 |
'SRA2' => 'SRA2', |
|
541 |
'SRA3' => 'SRA3', |
|
542 |
'SRA4' => 'SRA4', |
|
543 |
'LETTER' => 'Letter', |
|
544 |
'LEGAL' => 'Legal', |
|
545 |
'EXECUTIVE' => 'Executive', |
|
546 |
'FOLIO' => 'Folio', |
|
547 |
); |
|
548 |
|
|
549 |
return $ret; |
|
550 |
} |
Also available in: Unified diff
Weekly update of contrib modules