weekly release 2.1.6+
[moodle.git] / lib / pdflib.php
blob9c036aa386cbe6b1b11ed6e0ace462d9bb75dfcb
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->dataroot . '/cache/');
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 * Return fonts path
128 * Overriding TCPDF::_getfontpath()
130 * @global object
132 protected function _getfontpath() {
133 global $CFG;
135 if (is_dir(PDF_CUSTOM_FONT_PATH)
136 && count($this->_getfontfiles(PDF_CUSTOM_FONT_PATH)) > 0) {
137 $fontpath = PDF_CUSTOM_FONT_PATH;
138 } else {
139 $fontpath = K_PATH_FONTS;
141 return $fontpath;
145 * Get the .php files for the fonts
147 protected function _getfontfiles($fontdir) {
148 $dirlist = get_directory_list($fontdir);
149 $fontfiles = array();
151 foreach ($dirlist as $file) {
152 if (substr($file, -4) == '.php') {
153 array_push($fontfiles, $file);
156 return $fontfiles;