Merge branch 'MDL-35147_22' of git://github.com/timhunt/moodle into MOODLE_22_STABLE
[moodle.git] / lib / pdflib.php
blob169d16f05271039c8da0e6616341ea36349a9b58
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 . '/');
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') {
103 parent::__construct($orientation, $unit, $format, $unicode, $encoding);
105 if (is_dir(PDF_CUSTOM_FONT_PATH)) {
106 $fontfiles = $this->_getfontfiles(PDF_CUSTOM_FONT_PATH);
108 if (count($fontfiles) == 1) {
109 $autofontname = substr($fontfiles[0], 0, -4);
110 $this->AddFont($autofontname, '', $autofontname.'.php');
111 $this->SetFont($autofontname);
112 } else if (count($fontfiles == 0)) {
113 $this->SetFont(PDF_DEFAULT_FONT);
115 } else {
116 $this->SetFont(PDF_DEFAULT_FONT);
119 // theses replace the tcpdf's config/lang/ definitions
120 $this->l['w_page'] = get_string('page');
121 $this->l['a_meta_language'] = current_language();
122 $this->l['a_meta_charset'] = 'UTF-8';
123 $this->l['a_meta_dir'] = get_string('thisdirection', 'langconfig');
127 * Send the document to a given destination: string, local file or browser.
128 * In the last case, the plug-in may be used (if present) or a download ("Save as" dialog box) may be forced.<br />
129 * The method first calls Close() if necessary to terminate the document.
130 * @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.
131 * @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>
132 * @public
133 * @since 1.0
134 * @see Close()
136 public function Output($name='doc.pdf', $dest='I') {
137 $olddebug = error_reporting(0);
138 $result = parent::output($name, $dest);
139 error_reporting($olddebug);
140 return $result;
144 * Return fonts path
145 * Overriding TCPDF::_getfontpath()
147 * @global object
149 protected function _getfontpath() {
150 global $CFG;
152 if (is_dir(PDF_CUSTOM_FONT_PATH)
153 && count($this->_getfontfiles(PDF_CUSTOM_FONT_PATH)) > 0) {
154 $fontpath = PDF_CUSTOM_FONT_PATH;
155 } else {
156 $fontpath = K_PATH_FONTS;
158 return $fontpath;
162 * Get the .php files for the fonts
164 protected function _getfontfiles($fontdir) {
165 $dirlist = get_directory_list($fontdir);
166 $fontfiles = array();
168 foreach ($dirlist as $file) {
169 if (substr($file, -4) == '.php') {
170 array_push($fontfiles, $file);
173 return $fontfiles;