5 * Displays Printer-friendly versions of Drupal pages.
10 define('PRINTPDF_PATH', 'printpdf');
12 // Defined in print.module
13 //define('PRINT_PDF_FORMAT', 'pdf');
15 define('PRINT_PDF_LIB_PATH', 'sites/all/libraries');
17 define('PRINT_PDF_LINK_POS_DEFAULT', 'link');
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);
28 define('PRINT_PDF_CONTENT_DISPOSITION_DEFAULT', 2);
29 define('PRINT_PDF_PAPER_SIZE_DEFAULT', 'A4');
30 define('PRINT_PDF_PAGE_ORIENTATION_DEFAULT', 'portrait');
31 define('PRINT_PDF_AUTOCONFIG_DEFAULT', 1);
32 define('PRINT_PDF_FONT_FAMILY_DEFAULT', 'dejavusans');
33 define('PRINT_PDF_FONT_SIZE_DEFAULT', 10);
34 define('PRINT_PDF_FONT_SUBSETTING_DEFAULT', 0);
35 define('PRINT_PDF_FILENAME_DEFAULT', '[site:name] - [node:title] - [node:changed:custom:Y-m-d]');
36 define('PRINT_PDF_DOMPDF_UNICODE_DEFAULT', 0);
37 define('PRINT_PDF_WKHTMLTOPDF_OPTIONS', "--footer-font-size 7 --footer-right '[page]'");
40 * Implements hook_permission().
42 function print_pdf_permission() {
44 'access PDF version' => array(
45 'title' => t('Access the PDF version'),
46 'description' => t('View the PDF versions and the links to them in the original pages.'),
52 * Implements hook_theme().
54 function print_pdf_theme() {
56 'print_pdf_format_link' => array(
57 'variables' => array(),
59 'print_pdf_dompdf_footer' => array(
60 'variables' => array('html' => ''),
61 'file' => 'print_pdf.pages.inc',
63 'print_pdf_tcpdf_header' => array(
64 'variables' => array('pdf' => NULL
, 'html' => '', 'font' => array()),
65 'file' => 'print_pdf.pages.inc',
67 'print_pdf_tcpdf_page' => array(
68 'variables' => array('pdf' => NULL
),
69 'file' => 'print_pdf.pages.inc',
71 'print_pdf_tcpdf_content' => array(
72 'variables' => array('pdf' => NULL
, 'html' => '', 'font' => array()),
73 'file' => 'print_pdf.pages.inc',
75 'print_pdf_tcpdf_footer' => array(
76 'variables' => array('pdf' => NULL
, 'html' => '', 'font' => array()),
77 'file' => 'print_pdf.pages.inc',
79 'print_pdf_tcpdf_footer2' => array(
80 'variables' => array('pdf' => NULL
),
81 'file' => 'print_pdf.pages.inc',
87 * Implements hook_menu().
89 function print_pdf_menu() {
92 $items[PRINTPDF_PATH
] = array(
93 'title' => 'Printer-friendly PDF',
94 'page callback' => 'print_pdf_controller',
95 'access arguments' => array('access PDF version'),
96 'type' => MENU_CALLBACK
,
97 'file' => 'print_pdf.pages.inc',
99 $items[PRINTPDF_PATH .
'/' . PRINTPDF_PATH
] = array(
100 'access callback' => FALSE
,
102 $items['admin/config/print/pdf'] = array(
104 'page callback' => 'drupal_get_form',
105 'page arguments' => array('print_pdf_settings'),
106 'access arguments' => array('administer print'),
108 'type' => MENU_LOCAL_TASK
,
109 'file' => 'print_pdf.admin.inc',
111 $items['admin/config/print/pdf/options'] = array(
112 'title' => 'Options',
114 'type' => MENU_DEFAULT_LOCAL_TASK
,
116 $items['admin/config/print/pdf/strings'] = array(
117 'title' => 'Text strings',
118 'page callback' => 'drupal_get_form',
119 'page arguments' => array('print_pdf_strings_settings'),
120 'access arguments' => array('administer print'),
122 'type' => MENU_LOCAL_TASK
,
123 'file' => 'print_pdf.admin.inc',
130 * Implements hook_block_info().
132 function print_pdf_block_info() {
133 $block['print_pdf-top']['info'] = t('Most PDFd');
134 $block['print_pdf-top']['cache'] = DRUPAL_CACHE_GLOBAL
;
139 * Implements hook_block_view().
141 function print_pdf_block_view($delta = 0) {
143 case
'print_pdf-top':
144 $block['subject'] = t('Most PDFd');
145 $result = db_query_range("SELECT path FROM {print_pdf_page_counter} ORDER BY totalcount DESC", 0, 3)
147 if (count($result)) {
148 $block['content'] = '<div class="item-list"><ul>';
149 foreach ($result as
$obj) {
150 $block['content'] .
= '<li>' .
l(_print_get_title($obj->path
), $obj->path
) .
'</li>';
152 $block['content'] .
= '</ul></div>';
160 * Implements hook_requirements().
162 function print_pdf_requirements($phase) {
163 $requirements = array();
166 // At runtime, make sure that a PDF generation tool is selected
168 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT
);
169 if (empty($print_pdf_pdf_tool)) {
170 $requirements['print_pdf_tool'] = array(
171 'title' => $t('PDF generation library'),
172 'value' => $t('No PDF tool selected'),
173 'description' => $t('Please configure it in the <a href="@url">PDF settings page</a>.', array('@url' => url('admin/config/print/pdf'))),
174 'severity' => REQUIREMENT_ERROR
,
178 if (!is_file($print_pdf_pdf_tool) || !is_readable($print_pdf_pdf_tool)) {
179 $requirements['print_pdf_tool'] = array(
180 'title' => $t('PDF generation tool'),
181 'value' => $t('File not found'),
182 'description' => $t('The currently selected PDF generation library (%file) is no longer accessible.', array('%file' => $print_pdf_pdf_tool)),
183 'severity' => REQUIREMENT_ERROR
,
186 elseif (basename($print_pdf_pdf_tool) == 'dompdf_config.inc.php') {
187 $directory = dirname($print_pdf_pdf_tool) .
'/lib/fonts';
188 if (!is_dir($directory) || !is_writable($directory)) {
189 $requirements['print_pdf_tool'] = array(
190 'title' => $t('DOMPDF font cache directory'),
191 'value' => $t('Non-writable permissions'),
192 'description' => $t('You must change the %fontdir permissions to be writable, as dompdf requires write-access to that directory.', array('%fontdir' => $directory)),
193 'severity' => REQUIREMENT_ERROR
,
197 elseif (substr(basename($print_pdf_pdf_tool, '.exe'), 0, 11) == 'wkhtmltopdf') {
198 if (function_exists('is_executable') && !is_executable($print_pdf_pdf_tool)) {
199 $requirements['print_pdf_tool'] = array(
200 'title' => $t('wkhtmltopdf library'),
201 'value' => $t('Non-executable permissions'),
202 'description' => $t('You must modify the permissions of the wkhtmltopdf file (%file) to make it executable.', array('%file' => $print_pdf_pdf_tool)),
203 'severity' => REQUIREMENT_ERROR
,
207 $version = _print_pdf_wkhtmltopdf_version();
208 if (version_compare($version, '0.9.6', '<')) {
209 $requirements['print_pdf_tool'] = array(
210 'title' => $t('wkhtmltopdf library'),
211 'value' => $t('Unsupported version'),
212 'description' => $t('The currently selected version of wkhtmltopdf (@version) is not supported. Please update to a <a href="@url">newer version</a>.', array('@version' => $version, '@url' => url('http://code.google.com/p/wkhtmltopdf/'))),
213 'severity' => REQUIREMENT_ERROR
,
221 return $requirements;
225 * Implements hook_node_view().
227 function print_pdf_node_view($node, $build_mode) {
228 $print_pdf_link_pos = variable_get('print_pdf_link_pos', array(PRINT_PDF_LINK_POS_DEFAULT
=> PRINT_PDF_LINK_POS_DEFAULT
));
229 $print_pdf_link_use_alias = variable_get('print_pdf_link_use_alias', PRINT_PDF_LINK_USE_ALIAS_DEFAULT
);
230 $allowed_type = print_pdf_link_allowed(array('type' => 'node', 'node' => $node, 'teaser' => ($build_mode == 'teaser')));
231 if (($allowed_type) && !empty($print_pdf_link_pos['link'])) {
232 drupal_add_css(drupal_get_path('module', 'print') .
'/css/printlinks.css');
234 $format = theme('print_pdf_format_link');
237 if ($allowed_type === PRINT_ALLOW_BOOK_LINK
) {
238 $links['book_pdf'] = array('href' => PRINTPDF_PATH .
'/book/export/html/' .
$node->nid
,
239 'title' => $format['text'],
240 'attributes' => $format['attributes'],
241 'html' => $format['html'],
244 elseif ($allowed_type === PRINT_ALLOW_NORMAL_LINK
) {
246 if ('node' == 'comment') {
247 $query_arr['comment'] = $node->cid
;
249 $query = print_query_string_encode($query_arr, array('q'));
250 if (empty($query)) $query = NULL
;
252 if ($print_pdf_link_use_alias) {
253 $path = drupal_get_path_alias('node/' .
$node->nid
);
259 $links['print_pdf'] = array('href' => PRINTPDF_PATH .
'/' .
$path,
260 'title' => $format['text'],
261 'attributes' => $format['attributes'],
262 'html' => $format['html'],
266 $node->content
['links']['print_pdf'] = array(
269 '#attributes' => array('class' => array('links', 'inline')),
273 // Insert content corner links
274 if ((!empty($print_pdf_link_pos['corner'])) && ($build_mode == 'full')) {
275 $node->content
['print_links']['#markup'] .
= print_pdf_insert_link(NULL
, $node);
280 * Implements hook_help().
282 function print_pdf_help($path, $arg) {
283 $print_pdf_link_pos = variable_get('print_pdf_link_pos', array(PRINT_PDF_LINK_POS_DEFAULT
=> PRINT_PDF_LINK_POS_DEFAULT
));
284 if (($path !== 'node/%') && !(empty($print_pdf_link_pos['link']) && empty($print_pdf_link_pos['corner']))) {
285 static
$output = FALSE
;
287 if ($output === FALSE
) {
290 $link = print_pdf_insert_link();
292 return "<span class='print-syslink'>$link</span>";
299 * Implements hook_node_load().
301 function print_pdf_node_load($nodes, $types) {
302 foreach ($nodes as
$node) {
303 _print_pdf_set_node_fields($node);
308 * Implements hook_node_insert().
310 function print_pdf_node_insert($node) {
311 if (user_access('administer print') || user_access('node-specific print configuration')) {
312 if ($node->print_pdf_display
=== NULL
) $node->print_pdf_display
= variable_get('print_pdf_display_' .
$node->type
, PRINT_TYPE_SHOW_LINK_DEFAULT
);
313 if ($node->print_pdf_display_comment
=== NULL
) $node->print_pdf_display_comment
= variable_get('print_pdf_display_comment_' .
$node->type
, PRINT_TYPE_COMMENT_LINK_DEFAULT
);
314 if ($node->print_pdf_display_urllist
=== NULL
) $node->print_pdf_display_urllist
= variable_get('print_pdf_display_urllist_' .
$node->type
, PRINT_TYPE_URLLIST_DEFAULT
);
316 _print_pdf_node_conf_modify($node->nid
, $node->print_pdf_display
, $node->print_pdf_display_comment
, $node->print_pdf_display_urllist
);
321 * Implements hook_node_update().
323 function print_pdf_node_update($node) {
324 if (user_access('administer print') || user_access('node-specific print configuration')) {
325 if ($node->print_pdf_display
=== NULL
) $node->print_pdf_display
= variable_get('print_pdf_display_' .
$node->type
, PRINT_TYPE_SHOW_LINK_DEFAULT
);
326 if ($node->print_pdf_display_comment
=== NULL
) $node->print_pdf_display_comment
= variable_get('print_pdf_display_comment_' .
$node->type
, PRINT_TYPE_COMMENT_LINK_DEFAULT
);
327 if ($node->print_pdf_display_urllist
=== NULL
) $node->print_pdf_display_urllist
= variable_get('print_pdf_display_urllist_' .
$node->type
, PRINT_TYPE_URLLIST_DEFAULT
);
329 _print_pdf_node_conf_modify($node->nid
, $node->print_pdf_display
, $node->print_pdf_display_comment
, $node->print_pdf_display_urllist
);
334 * Implements hook_node_delete().
336 function print_pdf_node_delete($node) {
337 db_delete('print_pdf_node_conf')
338 ->condition('nid', $node->nid
)
340 db_delete('print_pdf_page_counter')
341 ->condition('path', 'node/' .
$node->nid
)
346 * Implements hook_form_alter().
348 function print_pdf_form_alter(&$form, &$form_state, $form_id) {
349 // Add the node-type settings option to activate the PDF version link
350 if ((user_access('administer print') || user_access('node-specific print configuration')) &&
351 (($form_id == 'node_type_form') || !empty($form['#node_edit_form']))) {
352 $form['print']['pdf_label'] = array(
354 '#markup' => '<p><strong>' .
t('PDF version') .
'</strong></p>',
357 $form['print']['print_pdf_display'] = array(
358 '#type' => 'checkbox',
359 '#title' => t('Show link'),
361 $form['print']['print_pdf_display_comment'] = array(
362 '#type' => 'checkbox',
363 '#title' => t('Show link in individual comments'),
365 $form['print']['print_pdf_display_urllist'] = array(
366 '#type' => 'checkbox',
367 '#title' => t('Show Printer-friendly URLs list'),
370 if ($form_id == 'node_type_form') {
371 $form['print']['print_pdf_display']['#default_value'] = variable_get('print_pdf_display_' .
$form['#node_type']->type
, PRINT_TYPE_SHOW_LINK_DEFAULT
);
372 $form['print']['print_pdf_display_comment']['#default_value'] = variable_get('print_pdf_display_comment_' .
$form['#node_type']->type
, PRINT_TYPE_COMMENT_LINK_DEFAULT
);
373 $form['print']['print_pdf_display_urllist']['#default_value'] = variable_get('print_pdf_display_urllist_' .
$form['#node_type']->type
, PRINT_TYPE_URLLIST_DEFAULT
);
376 $node = $form['#node'];
377 $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
);
378 $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
);
379 $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
);
385 * Auxiliary function to assign the per-node settings to the node object fields
388 * node to be modified
390 function _print_pdf_set_node_fields(&$node) {
391 if (isset($node->nid
)) {
392 $res = db_query("SELECT link, comments, url_list FROM {print_pdf_node_conf} WHERE nid = :nid", array(':nid' => $node->nid
))
398 $node->print_pdf_display
= $res ?
intval($res->link) : variable_get('print_pdf_display_' .
$node->type
, PRINT_TYPE_SHOW_LINK_DEFAULT
);
399 $node->print_pdf_display_comment
= $res ?
intval($res->comments
) : variable_get('print_pdf_display_comment_' .
$node->type
, PRINT_TYPE_COMMENT_LINK_DEFAULT
);
400 $node->print_pdf_display_urllist
= $res ?
intval($res->url_list
) : variable_get('print_pdf_display_urllist_' .
$node->type
, PRINT_TYPE_URLLIST_DEFAULT
);
404 * Update the print_pdf_node_conf table to reflect the given attributes
405 * If updating to the default values, delete the record.
408 * value of the nid field (primary key)
410 * value of the link field (0 or 1)
412 * value of the comments field (0 or 1)
414 * value of the url_list field (0 or 1)
416 function _print_pdf_node_conf_modify($nid, $link, $comments, $url_list) {
417 db_merge('print_pdf_node_conf')
418 ->key(array('nid' => $nid))
421 'comments' => $comments,
422 'url_list' => $url_list,
428 * Format the PDF version link
431 * array of formatted attributes
434 function theme_print_pdf_format_link() {
435 $print_pdf_link_class = variable_get('print_pdf_link_class', PRINT_PDF_LINK_CLASS_DEFAULT
);
436 $print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT
);
437 $print_pdf_show_link = variable_get('print_pdf_show_link', PRINT_PDF_SHOW_LINK_DEFAULT
);
438 $print_pdf_link_text = filter_xss(variable_get('print_pdf_link_text', t('PDF version')));
440 $img = drupal_get_path('module', 'print') .
'/icons/pdf_icon.gif';
441 $title = t('Display a PDF version of this page.');
442 $class = strip_tags($print_pdf_link_class);
443 $new_window = ($print_pdf_content_disposition == 1);
444 $format = _print_format_link_aux($print_pdf_show_link, $print_pdf_link_text, $img);
446 return array('text' => $format['text'],
447 'html' => $format['html'],
448 'attributes' => print_fill_attributes($title, $class, $new_window),
453 * Auxiliary function to display a formatted PDF version link
455 * Function made available so that developers may call this function from
456 * their defined pages/blocks.
459 * path of the original page (optional). If not specified, the current URL
462 * an optional node object, to be used in defining the path, if used, the
463 * path argument is irrelevant
465 * string with the HTML link to the printer-friendly page
467 function print_pdf_insert_link($path = NULL
, $node = NULL
) {
468 if ($node !== NULL
) {
470 $path = 'node/' .
$nid;
471 $allowed_type = print_pdf_link_allowed(array('node' => $node));
474 if ($path === NULL
) {
475 $nid = preg_replace('!^node/!', '', $_GET['q']);
481 $allowed_type = print_pdf_link_allowed(array('path' => $path));
486 if ($allowed_type === PRINT_ALLOW_BOOK_LINK
) {
487 $path = 'book/export/html/' .
$nid;
490 if (variable_get('print_pdf_link_use_alias', PRINT_PDF_LINK_USE_ALIAS_DEFAULT
)) {
491 $path = drupal_get_path_alias($path);
497 $path = PRINTPDF_PATH .
'/' .
$path;
498 $query = print_query_string_encode($_GET, array('q'));
506 drupal_add_css(drupal_get_path('module', 'print') .
'/css/printlinks.css');
507 $format = theme('print_pdf_format_link');
508 return '<span class="print_pdf">' .
l($format['text'], $path, array('attributes' => $format['attributes'], 'query' => $query, 'absolute' => TRUE
, 'html' => $format['html'])) .
'</span>';
516 * Determine a the link to the PDF version is allowed depending on all possible settings
519 * array containing the possible parameters:
520 * teaser, node, type, path
522 * FALSE if not allowed
523 * PRINT_ALLOW_NORMAL_LINK if a normal link is allowed
524 * PRINT_ALLOW_BOOK_LINK if a link is allowed in a book node
526 function print_pdf_link_allowed($args) {
527 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT
);
528 if ((!empty($args['teaser']) && !variable_get('print_pdf_link_teaser', PRINT_PDF_LINK_TEASER_DEFAULT
))
529 || !user_access('access PDF version') || (empty($print_pdf_pdf_tool))) {
530 // If the teaser link is disabled or the user is not allowed
533 if (!empty($args['path'])) {
534 $nid = preg_replace('!^node/!', '', drupal_get_normal_path($args['path']));
535 if (is_numeric($nid)) {
536 $args['node'] = node_load($nid);
539 if (!empty($args['node'])) {
540 static
$node_type = FALSE
;
542 $node = $args['node'];
543 if (isset($node->type
)) {
544 $node_type = $node->type
;
547 $print_pdf_node_link_visibility = variable_get('print_pdf_node_link_visibility', PRINT_PDF_NODE_LINK_VISIBILITY_DEFAULT
);
548 $print_pdf_node_link_pages = variable_get('print_pdf_node_link_pages', PRINT_PDF_NODE_LINK_PAGES_DEFAULT
);
550 if (!_print_page_match($print_pdf_node_link_visibility, "node/" .
$node->nid
, $print_pdf_node_link_pages)) {
551 // Page not in visibility list
554 elseif (isset($args['type']) && ($args['type'] == 'comment') && isset($node_type)) {
555 // Link is for a comment, return the configured setting
556 $res = db_query("SELECT comments FROM {print_pdf_node_conf} WHERE nid = :nid", array(':nid' => $node->nid
))
558 $print_display_comment = $res ?
intval($res->comments
) : variable_get('print_pdf_display_comment_' .
$node_type, PRINT_TYPE_COMMENT_LINK_DEFAULT
);
559 if ($print_display_comment) {
560 return PRINT_ALLOW_NORMAL_LINK
;
565 if (!$node->print_pdf_display
) {
566 // Link for this node is disabled
569 elseif (isset($node->book
)) {
571 $print_pdf_book_link = variable_get('print_pdf_book_link', PRINT_PDF_BOOK_LINK_DEFAULT
);
572 switch ($print_pdf_book_link) {
574 if (user_access('access printer-friendly version')) {
575 return PRINT_ALLOW_BOOK_LINK
;
579 return PRINT_ALLOW_NORMAL_LINK
;
583 return PRINT_ALLOW_NORMAL_LINK
;
589 $print_pdf_sys_link_visibility = variable_get('print_pdf_sys_link_visibility', PRINT_PDF_SYS_LINK_VISIBILITY_DEFAULT
);
590 $print_pdf_sys_link_pages = variable_get('print_pdf_sys_link_pages', PRINT_PDF_SYS_LINK_PAGES_DEFAULT
);
592 return _print_page_match($print_pdf_sys_link_visibility, $_GET['q'], $print_pdf_sys_link_pages);
597 function _print_pdf_wkhtmltopdf_version() {
598 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT
);
599 $descriptor = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
601 $cmd = realpath($print_pdf_pdf_tool) .
' --version';
602 $process = proc_open($cmd, $descriptor, $pipes, NULL
, NULL
);
603 if (is_resource($process)) {
604 $out = preg_match('!.*?(\d+\.\d+\.\d+).*$!m', stream_get_contents($pipes[1]), $matches);
608 $retval = proc_terminate($process);
611 return ($matches[1]);