MDL-19882 Add remaining missing require_login-s before require_capability.
[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
20 *
21 * We currently use the TCPDF library by Nicola Asuni.
22 *
23 * The default location for fonts that are included with TCPDF is
24 * lib/tcpdf/fonts/. If $CFG->datadir.'/fonts/' exists, this directory
25 * will be used instead of lib/tcpdf/fonts/. If there is only one font
26 * present in $CFG->datadir.'/fonts/', the font is used as the default
27 * font.
28 *
29 * See lib/tcpdf/fonts/README for details on how to convert fonts for use
30 * with TCPDF.
31 *
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 *
713b20fa 42 * @package moodlecore
50fcb1d8 43 * @copyright Vy-Shane Sin Fat
44 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
713b20fa 45 */
46
47
48
50fcb1d8 49/** Includes */
884a3e4f 50require_once(dirname(__FILE__).'/tcpdf/tcpdf.php');
713b20fa 51
52
53
54/// Constants
55define('PDF_CUSTOM_FONT_PATH', $CFG->dataroot.'/fonts/');
884a3e4f 56define('PDF_DEFAULT_FONT', 'DejaVuSerif');
713b20fa 57
58
59
60/**
61 * Wrapper class that extends TCPDF (lib/tcpdf/tcpdf.php).
62 * Moodle customisations are done here.
50fcb1d8 63 *
64 * @package moodlecore
65 * @copyright Vy-Shane Sin Fat
66 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
713b20fa 67 */
68class pdf extends TCPDF {
69
70 /**
884a3e4f 71 * Class constructor
72 *
73 * See the parent class documentation for the parameters info.
713b20fa 74 */
884a3e4f 75 public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8') {
713b20fa 76
884a3e4f 77 parent::__construct($orientation, $unit, $format, $unicode, $encoding);
713b20fa 78
79 if (is_dir(PDF_CUSTOM_FONT_PATH)) {
80 $fontfiles = $this->_getfontfiles(PDF_CUSTOM_FONT_PATH);
81
82 if (count($fontfiles) == 1) {
83 $autofontname = substr($fontfile[0], 0, -4);
84 $this->AddFont($autofontname, '', $autofontname.'.php');
85 $this->SetFont($autofontname);
86 } else if (count($fontfiles == 0)) {
87 $this->SetFont(PDF_DEFAULT_FONT);
88 }
89 } else {
90 $this->SetFont(PDF_DEFAULT_FONT);
91 }
92 }
93
94
95 /**
96 * Return fonts path
17951759 97 * Overriding TCPDF::_getfontpath()
50fcb1d8 98 *
99 * @global object
713b20fa 100 */
884a3e4f 101 protected function _getfontpath() {
713b20fa 102 global $CFG;
103
104 if (is_dir(PDF_CUSTOM_FONT_PATH)
105 && count($this->_getfontfiles(PDF_CUSTOM_FONT_PATH)) > 0) {
106 $fontpath = PDF_CUSTOM_FONT_PATH;
107 } else {
108 $fontpath = $CFG->dirroot.'/lib/tcpdf/fonts/';
109 }
110 return $fontpath;
111 }
112
113
114 /**
115 * Get the .php files for the fonts
116 */
884a3e4f 117 protected function _getfontfiles($fontdir) {
713b20fa 118 $dirlist = get_directory_list($fontdir);
119 $fontfiles = array();
120
121 foreach ($dirlist as $file) {
122 if (substr($file, -4) == '.php') {
123 array_push($fontfiles, $file);
124 }
125 }
126 return $fontfiles;
127 }
128
129
130} // End class pdf
131
132
b7064779 133?>