MDL-29213 - fix for bug with flash player and IE 9
[moodle.git] / lib / pdflib.php
CommitLineData
50fcb1d8 1<?php
713b20fa 2
50fcb1d8 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.
14//
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/>.
713b20fa 17
18/**
19 * pdflib.php - Moodle PDF library
e9618248 20 *
713b20fa 21 * We currently use the TCPDF library by Nicola Asuni.
22 *
23 * The default location for fonts that are included with TCPDF is
e9618248 24 * lib/tcpdf/fonts/. If PDF_CUSTOM_FONT_PATH exists, this directory
713b20fa 25 * will be used instead of lib/tcpdf/fonts/. If there is only one font
e9618248 26 * present in PDF_CUSTOM_FONT_PATH, the font is used as the default
713b20fa 27 * font.
e9618248 28 *
713b20fa 29 * See lib/tcpdf/fonts/README for details on how to convert fonts for use
30 * with TCPDF.
e9618248 31 *
713b20fa 32 * Example usage:
50fcb1d8 33 * <code>
17951759 34 * $doc = new pdf;
884a3e4f 35 * $doc->setPrintHeader(false);
36 * $doc->setPrintFooter(false);
17951759 37 * $doc->AddPage();
38 * $doc->Write(5, 'Hello World!');
39 * $doc->Output();
50fcb1d8 40 * </code>
41 *
e9618248 42 * @package moodlecore
43 * @copyright Vy-Shane Sin Fat
44 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
713b20fa 45 */
46
e9618248 47defined('MOODLE_INTERNAL') || die();
713b20fa 48
e9618248 49/** defines the site-specific location of fonts */
50define('PDF_CUSTOM_FONT_PATH', $CFG->dataroot.'/fonts/');
713b20fa 51
e9618248 52/** default font to be used if there are more of them available */
53define('PDF_DEFAULT_FONT', 'FreeSerif');
713b20fa 54
e9618248 55/** tell tcpdf it is configured here instead of in its own config file */
56define('K_TCPDF_EXTERNAL_CONFIG', 1);
713b20fa 57
e9618248 58// The configuration constants needed by tcpdf follow
713b20fa 59
e9618248 60/** tcpdf installation path */
61define('K_PATH_MAIN', $CFG->dirroot.'/lib/tcpdf/');
62
63/** URL path to tcpdf installation folder */
64define('K_PATH_URL', $CFG->wwwroot . '/lib/tcpdf/');
65
66/** path for PDF fonts */
67define('K_PATH_FONTS', K_PATH_MAIN . 'fonts/');
68
69/** cache directory for temporary files (full path) */
365bec4c 70define('K_PATH_CACHE', $CFG->cachedir . '/');
e9618248 71
72/** images directory */
73define('K_PATH_IMAGES', $CFG->dirroot . '/');
74
75/** blank image */
76define('K_BLANK_IMAGE', K_PATH_IMAGES . '/pix/spacer.gif');
77
78/** height of cell repect font height */
79define('K_CELL_HEIGHT_RATIO', 1.25);
713b20fa 80
e9618248 81/** reduction factor for small font */
82define('K_SMALL_RATIO', 2/3);
713b20fa 83
e9618248 84require_once(dirname(__FILE__).'/tcpdf/tcpdf.php');
713b20fa 85
86/**
87 * Wrapper class that extends TCPDF (lib/tcpdf/tcpdf.php).
88 * Moodle customisations are done here.
50fcb1d8 89 *
e9618248 90 * @package moodlecore
91 * @copyright Vy-Shane Sin Fat
92 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
713b20fa 93 */
94class pdf extends TCPDF {
e9618248 95
713b20fa 96 /**
884a3e4f 97 * Class constructor
e9618248 98 *
884a3e4f 99 * See the parent class documentation for the parameters info.
713b20fa 100 */
884a3e4f 101 public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8') {
e9618248 102
884a3e4f 103 parent::__construct($orientation, $unit, $format, $unicode, $encoding);
e9618248 104
713b20fa 105 if (is_dir(PDF_CUSTOM_FONT_PATH)) {
106 $fontfiles = $this->_getfontfiles(PDF_CUSTOM_FONT_PATH);
e9618248 107
713b20fa 108 if (count($fontfiles) == 1) {
e05fbdd8 109 $autofontname = substr($fontfiles[0], 0, -4);
713b20fa 110 $this->AddFont($autofontname, '', $autofontname.'.php');
111 $this->SetFont($autofontname);
112 } else if (count($fontfiles == 0)) {
113 $this->SetFont(PDF_DEFAULT_FONT);
114 }
115 } else {
116 $this->SetFont(PDF_DEFAULT_FONT);
117 }
e9618248 118
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';
e372f4c7 123 $this->l['a_meta_dir'] = get_string('thisdirection', 'langconfig');
713b20fa 124 }
e9618248 125
d0a0db42
PS
126 /**
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()
135 */
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;
141 }
142
713b20fa 143 /**
144 * Return fonts path
17951759 145 * Overriding TCPDF::_getfontpath()
50fcb1d8 146 *
147 * @global object
713b20fa 148 */
884a3e4f 149 protected function _getfontpath() {
713b20fa 150 global $CFG;
e9618248 151
713b20fa 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 {
e9618248 156 $fontpath = K_PATH_FONTS;
713b20fa 157 }
158 return $fontpath;
159 }
e9618248 160
713b20fa 161 /**
162 * Get the .php files for the fonts
163 */
884a3e4f 164 protected function _getfontfiles($fontdir) {
713b20fa 165 $dirlist = get_directory_list($fontdir);
166 $fontfiles = array();
e9618248 167
713b20fa 168 foreach ($dirlist as $file) {
169 if (substr($file, -4) == '.php') {
170 array_push($fontfiles, $file);
171 }
172 }
173 return $fontfiles;
174 }
713b20fa 175
e9618248 176}