Wrapper class with modifications to TCPDF for Moodle usage.
[moodle.git] / lib / pdflib.php
CommitLineData
713b20fa 1<?php
2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.org //
9// //
10// Copyright (C) 1999-2004 Martin Dougiamas http://dougiamas.com //
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:
41 * $pdf = new pdf;
42 * $pdf->print_header = false;
43 * $pdf->print_footer = false;
44 * $pdf->AddPage();
45 * $pdf->Write(5, 'Hello World!');
46 * $pdf->Output();
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
56require_once('tcpdf/tcpdf.php');
57
58
59
60/// Constants
61define('PDF_CUSTOM_FONT_PATH', $CFG->dataroot.'/fonts/');
62define('PDF_DEFAULT_FONT', 'FreeSerif');
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 /**
73 * Constructor
74 */
75 function pdf($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8') {
76
77 parent::TCPDF($orientation, $unit, $format, $unicode, $encoding);
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
97 * Overriden from class TCPDF
98 */
99 function _getfontpath() {
100 global $CFG;
101
102 if (is_dir(PDF_CUSTOM_FONT_PATH)
103 && count($this->_getfontfiles(PDF_CUSTOM_FONT_PATH)) > 0) {
104 $fontpath = PDF_CUSTOM_FONT_PATH;
105 } else {
106 $fontpath = $CFG->dirroot.'/lib/tcpdf/fonts/';
107 }
108 return $fontpath;
109 }
110
111
112 /**
113 * Get the .php files for the fonts
114 */
115 function _getfontfiles($fontdir) {
116 $dirlist = get_directory_list($fontdir);
117 $fontfiles = array();
118
119 foreach ($dirlist as $file) {
120 if (substr($file, -4) == '.php') {
121 array_push($fontfiles, $file);
122 }
123 }
124 return $fontfiles;
125 }
126
127
128} // End class pdf
129
130
131?>