Project

General

Profile

Paste
Download (3.26 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / print / print_pdf / lib_handlers / print_pdf_mpdf / print_pdf_mpdf.pages.inc @ 76bdcd04

1
<?php
2

    
3
/**
4
 * @file
5
 * Generates the PDF version using mPDF.
6
 *
7
 * This file is included by the print_pdf_mpdf module and includes the
8
 * functions that interface with the mPDF library.
9
 *
10
 * @ingroup print
11
 */
12

    
13
/**
14
 * Implements hook_print_pdf_generate().
15
 */
16
function print_pdf_mpdf_print_pdf_generate($html, $meta, $paper_size = NULL, $page_orientation = NULL) {
17
  module_load_include('inc', 'print', 'includes/print');
18

    
19
  $pdf_tool = explode('|', variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT));
20
  // Version 7 of the mpdf library uses a composer autoloader.
21
  // Also there no longer is way to truly detect the library version, so this
22
  // seems like the best alternative.
23
  $mpdf_version_7_plus = strpos($pdf_tool[1], 'autoload.php') !== FALSE;
24
  if (empty($paper_size)) {
25
    $paper_size = variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT);
26
  }
27
  if (empty($page_orientation)) {
28
    $page_orientation = variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT);
29
  }
30
  $images_via_file = variable_get('print_pdf_images_via_file', PRINT_PDF_IMAGES_VIA_FILE_DEFAULT);
31

    
32
  $config = array();
33
  if ($mpdf_version_7_plus) {
34
    $config['tempDir'] = drupal_realpath('public://print_pdf/print_pdf_mpdf/');
35
  }
36
  else {
37
    // Deprecated since mpdf v7.x.
38
    if (variable_get('print_pdf_autoconfig', PRINT_PDF_AUTOCONFIG_DEFAULT)) {
39
      if (!defined('_MPDF_TTFONTDATAPATH')) {
40
        define('_MPDF_TTFONTDATAPATH', drupal_realpath('public://print_pdf/print_pdf_mpdf/ttfontdata/'));
41
      }
42
      if (!defined('_MPDF_TEMP_PATH')) {
43
        define('_MPDF_TEMP_PATH', drupal_realpath('public://print_pdf/print_pdf_mpdf/tmp/'));
44
      }
45
    }
46
  }
47

    
48
  $tool_path = DRUPAL_ROOT . '/' . $pdf_tool[1];
49
  if (file_exists($tool_path)) {
50
    require_once $tool_path;
51
  }
52
  else {
53
    watchdog('print_pdf', 'Configured PDF tool does not exist at path: %path', array('%path' => $tool_path), WATCHDOG_ERROR);
54
    throw new Exception("Configured PDF tool does not exist, unable to generate PDF.");
55
  }
56

    
57
  $format = ($page_orientation == "landscape") ? $paper_size . "-L" : $paper_size;
58

    
59
  // Try to use local file access for image files.
60
  $html = _print_access_images_via_file($html, $images_via_file);
61

    
62
  // Set document information.
63
  if ($mpdf_version_7_plus) {
64
    $config['mode'] = 'utf-8';
65
    $config['format'] = $format;
66
    $mpdf = new \Mpdf\Mpdf($config);
67
  }
68
  else {
69
    $mpdf = new mPDF('UTF-8', $format);
70
  }
71

    
72
  if (isset($meta['name'])) {
73
    $mpdf->SetAuthor(strip_tags($meta['name']));
74
  }
75
  $mpdf->SetCreator(variable_get('site_name', 'Drupal'));
76
  /*
77
  // Pulled from the HTML meta data
78
  $mpdf->SetTitle(html_entity_decode($meta['title'], ENT_QUOTES, 'UTF-8'));
79

    
80
  $keys = implode(' ', explode("\n", trim(strip_tags($print['taxonomy']))));
81
  $mpdf->SetKeywords($keys);
82

    
83
  // Encrypt the file and grant permissions to the user to copy and print
84
  // No password is required to open the document
85
  // Owner has full rights using the password "MyPassword"
86
  $mpdf->SetProtection(array('copy', 'print'), '', 'MyPassword');
87
  $mpdf->SetProtection(array('copy', 'print', 'print-highres'), '', '');
88
   */
89
  drupal_alter('print_pdf_mpdf', $mpdf, $html, $meta);
90

    
91
  $mpdf->WriteHTML($html);
92

    
93
  // Try to recover from any warning/error.
94
  ob_clean();
95

    
96
  return $mpdf->Output('', 'S');
97
}