3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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:
41 * require('config.php');
42 * require_once($CFG->libdir . '/pdflib.php');
43 * TCPDF_FONTS::addTTFfont('/full_path_to/ARIALUNI.TTF', 'TrueTypeUnicode');
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!
52 * $doc->setPrintHeader(false);
53 * $doc->setPrintFooter(false);
55 * $doc->Write(5, 'Hello World!');
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
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() {
90 $defaultfonts = $CFG->dirroot
.'/lib/tcpdf/fonts/';
92 if (!defined('K_PATH_FONTS')) {
93 if (is_dir(PDF_CUSTOM_FONT_PATH
)) {
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');
103 foreach ($somestandardfiles as $file) {
104 if (!file_exists(PDF_CUSTOM_FONT_PATH
. $file . '.php')) {
110 define('K_PATH_FONTS', $defaultfonts);
112 define('K_PATH_FONTS', PDF_CUSTOM_FONT_PATH
);
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
. '/');
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(__DIR__
.'/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
{
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>
188 public function Output($name='doc.pdf', $dest='I') {
189 $olddebug = error_reporting(0);
190 $result = parent
::output($name, $dest);
191 error_reporting($olddebug);
196 * Is this font family one of core fonts?
197 * @param string $fontfamily
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() {
211 foreach ($this->fontlist
as $font) {
212 if (strpos($font, 'uni2cid') === 0) {
213 // This is not an font file.
216 if (strpos($font, 'cid0') === 0) {
217 // These do not seem to work with utf-8, better ignore them for now.
220 if (substr($font, -2) === 'bi') {
221 $family = substr($font, 0, -2);
222 if (in_array($family, $this->fontlist
)) {
223 $families[$family]['BI'] = 'BI';
227 if (substr($font, -1) === 'i') {
228 $family = substr($font, 0, -1);
229 if (in_array($family, $this->fontlist
)) {
230 $families[$family]['I'] = 'I';
234 if (substr($font, -1) === 'b') {
235 $family = substr($font, 0, -1);
236 if (in_array($family, $this->fontlist
)) {
237 $families[$family]['B'] = 'B';
241 // This must be a Family or incomplete set of fonts present.
242 $families[$font]['R'] = 'R';
245 // Sort everything consistently.
247 foreach ($families as $k => $v) {
248 krsort($families[$k]);
255 * Get font list from config.
256 * @return array|string[]
258 public function get_export_fontlist(): array {
261 if (!empty($CFG->pdfexportfont
)) {
262 if (is_array($CFG->pdfexportfont
)) {
263 $fontlist = $CFG->pdfexportfont
;
265 $fontlist[$CFG->pdfexportfont
] = $CFG->pdfexportfont
;
269 $availablefonts = $this->get_font_families();
270 foreach ($fontlist as $key => $value) {
271 if (empty($availablefonts[$key])) {
272 unset($fontlist[$key]);
275 if (empty($fontlist)) {
276 // Default font if there is no value set in CFG.
277 $fontlist = ['freesans' => 'FreeSans'];