lib MDL-19236 Added boilerplates and copyrights
[moodle.git] / lib / pdflib.php
CommitLineData
dd46fc5d 1<?php // $Id$
713b20fa 2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.org //
9// //
b7064779 10// Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
713b20fa 11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
25
26/**
27 * pdflib.php - Moodle PDF library
28 *
29 * We currently use the TCPDF library by Nicola Asuni.
30 *
31 * The default location for fonts that are included with TCPDF is
32 * lib/tcpdf/fonts/. If $CFG->datadir.'/fonts/' exists, this directory
33 * will be used instead of lib/tcpdf/fonts/. If there is only one font
34 * present in $CFG->datadir.'/fonts/', the font is used as the default
35 * font.
36 *
37 * See lib/tcpdf/fonts/README for details on how to convert fonts for use
38 * with TCPDF.
39 *
40 * Example usage:
17951759 41 * $doc = new pdf;
884a3e4f 42 * $doc->setPrintHeader(false);
43 * $doc->setPrintFooter(false);
17951759 44 * $doc->AddPage();
45 * $doc->Write(5, 'Hello World!');
46 * $doc->Output();
713b20fa 47 *
48 * @author Vy-Shane Sin Fat
49 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
50 * @package moodlecore
51 */
52
53
54
55/// Includes
884a3e4f 56require_once(dirname(__FILE__).'/tcpdf/tcpdf.php');
713b20fa 57
58
59
60/// Constants
61define('PDF_CUSTOM_FONT_PATH', $CFG->dataroot.'/fonts/');
884a3e4f 62define('PDF_DEFAULT_FONT', 'DejaVuSerif');
713b20fa 63
64
65
66/**
67 * Wrapper class that extends TCPDF (lib/tcpdf/tcpdf.php).
68 * Moodle customisations are done here.
69 */
70class pdf extends TCPDF {
71
72 /**
884a3e4f 73 * Class constructor
74 *
75 * See the parent class documentation for the parameters info.
713b20fa 76 */
884a3e4f 77 public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8') {
713b20fa 78
884a3e4f 79 parent::__construct($orientation, $unit, $format, $unicode, $encoding);
713b20fa 80
81 if (is_dir(PDF_CUSTOM_FONT_PATH)) {
82 $fontfiles = $this->_getfontfiles(PDF_CUSTOM_FONT_PATH);
83
84 if (count($fontfiles) == 1) {
85 $autofontname = substr($fontfile[0], 0, -4);
86 $this->AddFont($autofontname, '', $autofontname.'.php');
87 $this->SetFont($autofontname);
88 } else if (count($fontfiles == 0)) {
89 $this->SetFont(PDF_DEFAULT_FONT);
90 }
91 } else {
92 $this->SetFont(PDF_DEFAULT_FONT);
93 }
94 }
95
96
97 /**
98 * Return fonts path
17951759 99 * Overriding TCPDF::_getfontpath()
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?>