Merge branch 'MDL-43332_m26' of https://github.com/markn86/moodle into MOODLE_26_STABLE
[moodle.git] / lib / pdflib.php
blob3b72e6e9e77638aaee1e36e9bb44edb2ba0d6b1d
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * pdflib.php - Moodle PDF library
21 * We currently use the TCPDF library by Nicola Asuni.
23 * The default location for fonts that are included with TCPDF is
24 * lib/tcpdf/fonts/. If PDF_CUSTOM_FONT_PATH exists, this directory
25 * will be used instead of lib/tcpdf/fonts/. If there is only one font
26 * present in PDF_CUSTOM_FONT_PATH, the font is used as the default
27 * font.
29 * See lib/tcpdf/fonts/README for details on how to convert fonts for use
30 * with TCPDF.
32 * Example usage:
33 * <code>
34 * $doc = new pdf;
35 * $doc->setPrintHeader(false);
36 * $doc->setPrintFooter(false);
37 * $doc->AddPage();
38 * $doc->Write(5, 'Hello World!');
39 * $doc->Output();
40 * </code>
42 * @package moodlecore
43 * @copyright Vy-Shane Sin Fat
44 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
47 defined('MOODLE_INTERNAL') || die();
49 /** defines the site-specific location of fonts */
50 define('PDF_CUSTOM_FONT_PATH', $CFG->dataroot.'/fonts/');
52 /** default font to be used if there are more of them available */
53 define('PDF_DEFAULT_FONT', 'FreeSerif');
55 /** tell tcpdf it is configured here instead of in its own config file */
56 define('K_TCPDF_EXTERNAL_CONFIG', 1);
58 // The configuration constants needed by tcpdf follow
60 /** tcpdf installation path */
61 define('K_PATH_MAIN', $CFG->dirroot.'/lib/tcpdf/');
63 /** URL path to tcpdf installation folder */
64 define('K_PATH_URL', $CFG->wwwroot . '/lib/tcpdf/');
66 /** path for PDF fonts */
67 define('K_PATH_FONTS', K_PATH_MAIN . 'fonts/');
69 /** cache directory for temporary files (full path) */
70 define('K_PATH_CACHE', $CFG->cachedir . '/tcpdf/');
72 /** images directory */
73 define('K_PATH_IMAGES', $CFG->dirroot . '/');
75 /** blank image */
76 define('K_BLANK_IMAGE', K_PATH_IMAGES . '/pix/spacer.gif');
78 /** height of cell repect font height */
79 define('K_CELL_HEIGHT_RATIO', 1.25);
81 /** reduction factor for small font */
82 define('K_SMALL_RATIO', 2/3);
84 require_once(dirname(__FILE__).'/tcpdf/tcpdf.php');
86 /**
87 * Wrapper class that extends TCPDF (lib/tcpdf/tcpdf.php).
88 * Moodle customisations are done here.
90 * @package moodlecore
91 * @copyright Vy-Shane Sin Fat
92 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
94 class pdf extends TCPDF {
96 /**
97 * Class constructor
99 * See the parent class documentation for the parameters info.
101 public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8') {
102 make_cache_directory('tcpdf');
104 parent::__construct($orientation, $unit, $format, $unicode, $encoding);
106 if (is_dir(PDF_CUSTOM_FONT_PATH)) {
107 $fontfiles = $this->_getfontfiles(PDF_CUSTOM_FONT_PATH);
109 if (count($fontfiles) == 1) {
110 $autofontname = substr($fontfiles[0], 0, -4);
111 $this->AddFont($autofontname, '', $autofontname.'.php');
112 $this->SetFont($autofontname);
113 } else if (count($fontfiles == 0)) {
114 $this->SetFont(PDF_DEFAULT_FONT);
116 } else {
117 $this->SetFont(PDF_DEFAULT_FONT);
120 // theses replace the tcpdf's config/lang/ definitions
121 $this->l['w_page'] = get_string('page');
122 $this->l['a_meta_language'] = current_language();
123 $this->l['a_meta_charset'] = 'UTF-8';
124 $this->l['a_meta_dir'] = get_string('thisdirection', 'langconfig');
128 * Send the document to a given destination: string, local file or browser.
129 * In the last case, the plug-in may be used (if present) or a download ("Save as" dialog box) may be forced.<br />
130 * The method first calls Close() if necessary to terminate the document.
131 * @param $name (string) The name of the file when saved. Note that special characters are removed and blanks characters are replaced with the underscore character.
132 * @param $dest (string) Destination where to send the document. It can take one of the following values:<ul><li>I: send the file inline to the browser (default). The plug-in is used if available. The name given by name is used when one selects the "Save as" option on the link generating the PDF.</li><li>D: send to the browser and force a file download with the name given by name.</li><li>F: save to a local server file with the name given by name.</li><li>S: return the document as a string (name is ignored).</li><li>FI: equivalent to F + I option</li><li>FD: equivalent to F + D option</li><li>E: return the document as base64 mime multi-part email attachment (RFC 2045)</li></ul>
133 * @public
134 * @since 1.0
135 * @see Close()
137 public function Output($name='doc.pdf', $dest='I') {
138 $olddebug = error_reporting(0);
139 $result = parent::output($name, $dest);
140 error_reporting($olddebug);
141 return $result;
145 * Return fonts path
146 * Overriding TCPDF::_getfontpath()
148 * @global object
150 protected function _getfontpath() {
151 global $CFG;
153 if (is_dir(PDF_CUSTOM_FONT_PATH)
154 && count($this->_getfontfiles(PDF_CUSTOM_FONT_PATH)) > 0) {
155 $fontpath = PDF_CUSTOM_FONT_PATH;
156 } else {
157 $fontpath = K_PATH_FONTS;
159 return $fontpath;
163 * Get the .php files for the fonts
165 protected function _getfontfiles($fontdir) {
166 $dirlist = get_directory_list($fontdir);
167 $fontfiles = array();
169 foreach ($dirlist as $file) {
170 if (substr($file, -4) == '.php') {
171 array_push($fontfiles, $file);
174 return $fontfiles;