fixed invalid font substr
[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) */
70define('K_PATH_CACHE', $CFG->dataroot . '/cache/');
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
713b20fa 126 /**
127 * Return fonts path
17951759 128 * Overriding TCPDF::_getfontpath()
50fcb1d8 129 *
130 * @global object
713b20fa 131 */
884a3e4f 132 protected function _getfontpath() {
713b20fa 133 global $CFG;
e9618248 134
713b20fa 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 {
e9618248 139 $fontpath = K_PATH_FONTS;
713b20fa 140 }
141 return $fontpath;
142 }
e9618248 143
713b20fa 144 /**
145 * Get the .php files for the fonts
146 */
884a3e4f 147 protected function _getfontfiles($fontdir) {
713b20fa 148 $dirlist = get_directory_list($fontdir);
149 $fontfiles = array();
e9618248 150
713b20fa 151 foreach ($dirlist as $file) {
152 if (substr($file, -4) == '.php') {
153 array_push($fontfiles, $file);
154 }
155 }
156 return $fontfiles;
157 }
713b20fa 158
e9618248 159}