MDL-27930 Lesson: the count-down timer does not work
[moodle.git] / lib / pdflib.php
blob0945ff8d4eb334129a175a11e1f52ecb06df5ec5
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/, the default location is
26 * $CFG->dataroot.'/fonts/'.
28 * You should always copy all fonts from lib/tcpdf/fonts/ to your
29 * PDF_CUSTOM_FONT_PATH and then add extra fonts. Alternatively
30 * you may download all TCPDF fonts from http://www.tcpdf.org/download.php
31 * and extract them to PDF_CUSTOM_FONT_PATH directory.
33 * You can specify your own default font family in config.php
34 * by defining PDF_DEFAULT_FONT constant there.
36 * If you want to add True Type fonts such as "Arial Unicode MS",
37 * you need to create a simple script and then execute it, it should add
38 * new file to your fonts directory:
39 * <code>
40 * <?php
41 * require('config.php');
42 * require_once($CFG->libdir . '/pdflib.php');
43 * TCPDF_FONTS::addTTFfont('/full_path_to/ARIALUNI.TTF', 'TrueTypeUnicode');
44 * </code>
45 * This script will convert the TTF file to format compatible with TCPDF.
47 * Please note you need to have appropriate license to use the font files on your server!
49 * Example usage:
50 * <code>
51 * $doc = new pdf;
52 * $doc->setPrintHeader(false);
53 * $doc->setPrintFooter(false);
54 * $doc->AddPage();
55 * $doc->Write(5, 'Hello World!');
56 * $doc->Output();
57 * </code>
59 * @package moodlecore
60 * @copyright Vy-Shane Sin Fat
61 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
64 defined('MOODLE_INTERNAL') || die();
66 if (!defined('PDF_CUSTOM_FONT_PATH')) {
67 /** Defines the site-specific location of fonts. */
68 define('PDF_CUSTOM_FONT_PATH', $CFG->dataroot.'/fonts/');
71 if (!defined('PDF_DEFAULT_FONT')) {
72 /** Default font to be used. */
73 define('PDF_DEFAULT_FONT', 'FreeSerif');
76 /** tell tcpdf it is configured here instead of in its own config file */
77 define('K_TCPDF_EXTERNAL_CONFIG', 1);
79 // The configuration constants needed by tcpdf follow
81 /**
82 * Init K_PATH_FONTS and PDF_FONT_NAME_MAIN constant.
84 * Unfortunately this hack is necessary because the constants need
85 * to be defined before inclusion of the tcpdf.php file.
87 function tcpdf_init_k_font_path() {
88 global $CFG;
90 $defaultfonts = $CFG->dirroot.'/lib/tcpdf/fonts/';
92 if (!defined('K_PATH_FONTS')) {
93 if (is_dir(PDF_CUSTOM_FONT_PATH)) {
94 // NOTE:
95 // There used to be an option to have just one file and having it set as default
96 // but that does not make sense any more because add-ons using standard fonts
97 // would fail very badly, also font families consist of multiple php files for
98 // regular, bold, italic, etc.
100 // Check for some standard font files if present and if not do not use the custom path.
101 $somestandardfiles = array('courier', 'helvetica', 'times', 'symbol', 'zapfdingbats', 'freeserif', 'freesans');
102 $missing = false;
103 foreach ($somestandardfiles as $file) {
104 if (!file_exists(PDF_CUSTOM_FONT_PATH . $file . '.php')) {
105 $missing = true;
106 break;
109 if ($missing) {
110 define('K_PATH_FONTS', $defaultfonts);
111 } else {
112 define('K_PATH_FONTS', PDF_CUSTOM_FONT_PATH);
114 } else {
115 define('K_PATH_FONTS', $defaultfonts);
119 if (!defined('PDF_FONT_NAME_MAIN')) {
120 define('PDF_FONT_NAME_MAIN', strtolower(PDF_DEFAULT_FONT));
123 tcpdf_init_k_font_path();
125 /** tcpdf installation path */
126 define('K_PATH_MAIN', $CFG->dirroot.'/lib/tcpdf/');
128 /** URL path to tcpdf installation folder */
129 define('K_PATH_URL', $CFG->wwwroot . '/lib/tcpdf/');
131 /** cache directory for temporary files (full path) */
132 define('K_PATH_CACHE', $CFG->cachedir . '/tcpdf/');
134 /** images directory */
135 define('K_PATH_IMAGES', $CFG->dirroot . '/');
137 /** blank image */
138 define('K_BLANK_IMAGE', K_PATH_IMAGES . 'pix/spacer.gif');
140 /** height of cell repect font height */
141 define('K_CELL_HEIGHT_RATIO', 1.25);
143 /** reduction factor for small font */
144 define('K_SMALL_RATIO', 2/3);
146 /** Throw exceptions from errors so they can be caught and recovered from. */
147 define('K_TCPDF_THROW_EXCEPTION_ERROR', true);
149 require_once(dirname(__FILE__).'/tcpdf/tcpdf.php');
152 * Wrapper class that extends TCPDF (lib/tcpdf/tcpdf.php).
153 * Moodle customisations are done here.
155 * @package moodlecore
156 * @copyright Vy-Shane Sin Fat
157 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
159 class pdf extends TCPDF {
162 * Class constructor
164 * See the parent class documentation for the parameters info.
166 public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8') {
167 make_cache_directory('tcpdf');
169 parent::__construct($orientation, $unit, $format, $unicode, $encoding);
171 // theses replace the tcpdf's config/lang/ definitions
172 $this->l['w_page'] = get_string('page');
173 $this->l['a_meta_language'] = current_language();
174 $this->l['a_meta_charset'] = 'UTF-8';
175 $this->l['a_meta_dir'] = get_string('thisdirection', 'langconfig');
179 * Send the document to a given destination: string, local file or browser.
180 * In the last case, the plug-in may be used (if present) or a download ("Save as" dialog box) may be forced.<br />
181 * The method first calls Close() if necessary to terminate the document.
182 * @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.
183 * @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>
184 * @public
185 * @since Moodle 1.0
186 * @see Close()
188 public function Output($name='doc.pdf', $dest='I') {
189 $olddebug = error_reporting(0);
190 $result = parent::output($name, $dest);
191 error_reporting($olddebug);
192 return $result;
196 * Is this font family one of core fonts?
197 * @param string $fontfamily
198 * @return bool
200 public function is_core_font_family($fontfamily) {
201 return isset($this->CoreFonts[$fontfamily]);
205 * Returns list of font families and types of fonts.
207 * @return array multidimensional array with font families as keys and B, I, BI and N as values.
209 public function get_font_families() {
210 $families = array();
211 foreach ($this->fontlist as $font) {
212 if (strpos($font, 'uni2cid') === 0) {
213 // This is not an font file.
214 continue;
216 if (strpos($font, 'cid0') === 0) {
217 // These do not seem to work with utf-8, better ignore them for now.
218 continue;
220 if (substr($font, -2) === 'bi') {
221 $family = substr($font, 0, -2);
222 if (in_array($family, $this->fontlist)) {
223 $families[$family]['BI'] = 'BI';
224 continue;
227 if (substr($font, -1) === 'i') {
228 $family = substr($font, 0, -1);
229 if (in_array($family, $this->fontlist)) {
230 $families[$family]['I'] = 'I';
231 continue;
234 if (substr($font, -1) === 'b') {
235 $family = substr($font, 0, -1);
236 if (in_array($family, $this->fontlist)) {
237 $families[$family]['B'] = 'B';
238 continue;
241 // This must be a Family or incomplete set of fonts present.
242 $families[$font]['R'] = 'R';
245 // Sort everything consistently.
246 ksort($families);
247 foreach ($families as $k => $v) {
248 krsort($families[$k]);
251 return $families;