Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
/lib/ -> pdflib.php (source)

Differences Between: [Versions 311 and 402] [Versions 311 and 403]

   1  <?php
   2  
   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/>.
  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 PDF_CUSTOM_FONT_PATH exists, this directory
  25   * will be used instead of lib/tcpdf/fonts/, the default location is
  26   * $CFG->dataroot.'/fonts/'.
  27   *
  28   * You should always copy all fonts from lib/tcpdf/fonts/ to your
  29   * PDF_CUSTOM_FONT_PATH and then add extra fonts. Alternatively
  30   * you may download all TCPDF fonts from http://www.tcpdf.org/download.php
  31   * and extract them to PDF_CUSTOM_FONT_PATH directory.
  32   *
  33   * You can specify your own default font family in config.php
  34   * by defining PDF_DEFAULT_FONT constant there.
  35   *
  36   * If you want to add True Type fonts such as "Arial Unicode MS",
  37   * you need to create a simple script and then execute it, it should add
  38   * new file to your fonts directory:
  39   * <code>
  40   *   <?php
  41   *   require('config.php');
  42   *   require_once($CFG->libdir . '/pdflib.php');
  43   *   TCPDF_FONTS::addTTFfont('/full_path_to/ARIALUNI.TTF', 'TrueTypeUnicode');
  44   * </code>
  45   * This script will convert the TTF file to format compatible with TCPDF.
  46   *
  47   * Please note you need to have appropriate license to use the font files on your server!
  48   *
  49   * Example usage:
  50   * <code>
  51   *    $doc = new pdf;
  52   *    $doc->setPrintHeader(false);
  53   *    $doc->setPrintFooter(false);
  54   *    $doc->AddPage();
  55   *    $doc->Write(5, 'Hello World!');
  56   *    $doc->Output();
  57   * </code>
  58   *
  59   * @package     moodlecore
  60   * @copyright   Vy-Shane Sin Fat
  61   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  62   */
  63  
  64  defined('MOODLE_INTERNAL') || die();
  65  
  66  if (!defined('PDF_CUSTOM_FONT_PATH')) {
  67      /** Defines the site-specific location of fonts. */
  68      define('PDF_CUSTOM_FONT_PATH', $CFG->dataroot.'/fonts/');
  69  }
  70  
  71  if (!defined('PDF_DEFAULT_FONT')) {
  72      /** Default font to be used. */
  73      define('PDF_DEFAULT_FONT', 'FreeSerif');
  74  }
  75  
  76  /** tell tcpdf it is configured here instead of in its own config file */
  77  define('K_TCPDF_EXTERNAL_CONFIG', 1);
  78  
  79  // The configuration constants needed by tcpdf follow
  80  
  81  /**
  82   * Init K_PATH_FONTS and PDF_FONT_NAME_MAIN constant.
  83   *
  84   * Unfortunately this hack is necessary because the constants need
  85   * to be defined before inclusion of the tcpdf.php file.
  86   */
  87  function tcpdf_init_k_font_path() {
  88      global $CFG;
  89  
  90      $defaultfonts = $CFG->dirroot.'/lib/tcpdf/fonts/';
  91  
  92      if (!defined('K_PATH_FONTS')) {
  93          if (is_dir(PDF_CUSTOM_FONT_PATH)) {
  94              // NOTE:
  95              //   There used to be an option to have just one file and having it set as default
  96              //   but that does not make sense any more because add-ons using standard fonts
  97              //   would fail very badly, also font families consist of multiple php files for
  98              //   regular, bold, italic, etc.
  99  
 100              // Check for some standard font files if present and if not do not use the custom path.
 101              $somestandardfiles = array('courier',  'helvetica', 'times', 'symbol', 'zapfdingbats', 'freeserif', 'freesans');
 102              $missing = false;
 103              foreach ($somestandardfiles as $file) {
 104                  if (!file_exists(PDF_CUSTOM_FONT_PATH . $file . '.php')) {
 105                      $missing = true;
 106                      break;
 107                  }
 108              }
 109              if ($missing) {
 110                  define('K_PATH_FONTS', $defaultfonts);
 111              } else {
 112                  define('K_PATH_FONTS', PDF_CUSTOM_FONT_PATH);
 113              }
 114          } else {
 115              define('K_PATH_FONTS', $defaultfonts);
 116          }
 117      }
 118  
 119      if (!defined('PDF_FONT_NAME_MAIN')) {
 120          define('PDF_FONT_NAME_MAIN', strtolower(PDF_DEFAULT_FONT));
 121      }
 122  }
 123  tcpdf_init_k_font_path();
 124  
 125  /** tcpdf installation path */
 126  define('K_PATH_MAIN', $CFG->dirroot.'/lib/tcpdf/');
 127  
 128  /** URL path to tcpdf installation folder */
 129  define('K_PATH_URL', $CFG->wwwroot . '/lib/tcpdf/');
 130  
 131  /** cache directory for temporary files (full path) */
 132  define('K_PATH_CACHE', $CFG->cachedir . '/tcpdf/');
 133  
 134  /** images directory */
 135  define('K_PATH_IMAGES', $CFG->dirroot . '/');
 136  
 137  /** blank image */
 138  define('K_BLANK_IMAGE', K_PATH_IMAGES . 'pix/spacer.gif');
 139  
 140  /** height of cell repect font height */
 141  define('K_CELL_HEIGHT_RATIO', 1.25);
 142  
 143  /** reduction factor for small font */
 144  define('K_SMALL_RATIO', 2/3);
 145  
 146  /** Throw exceptions from errors so they can be caught and recovered from. */
 147  define('K_TCPDF_THROW_EXCEPTION_ERROR', true);
 148  
 149  require_once (__DIR__.'/tcpdf/tcpdf.php');
 150  
 151  /**
 152   * Wrapper class that extends TCPDF (lib/tcpdf/tcpdf.php).
 153   * Moodle customisations are done here.
 154   *
 155   * @package     moodlecore
 156   * @copyright   Vy-Shane Sin Fat
 157   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 158   */
 159  class pdf extends TCPDF {
 160  
 161      /**
 162       * Class constructor
 163       *
 164       * See the parent class documentation for the parameters info.
 165       */
 166      public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8') {
 167          make_cache_directory('tcpdf');
 168  
 169          parent::__construct($orientation, $unit, $format, $unicode, $encoding);
 170  
 171          // theses replace the tcpdf's config/lang/ definitions
 172          $this->l['w_page']          = get_string('page');
 173          $this->l['a_meta_language'] = current_language();
 174          $this->l['a_meta_charset']  = 'UTF-8';
 175          $this->l['a_meta_dir']      = get_string('thisdirection', 'langconfig');
 176      }
 177  
 178      /**
 179       * Send the document to a given destination: string, local file or browser.
 180       * In the last case, the plug-in may be used (if present) or a download ("Save as" dialog box) may be forced.<br />
 181       * The method first calls Close() if necessary to terminate the document.
 182       * @param $name (string) The name of the file when saved. Note that special characters are removed and blanks characters are replaced with the underscore character.
 183       * @param $dest (string) Destination where to send the document. It can take one of the following values:<ul><li>I: send the file inline to the browser (default). The plug-in is used if available. The name given by name is used when one selects the "Save as" option on the link generating the PDF.</li><li>D: send to the browser and force a file download with the name given by name.</li><li>F: save to a local server file with the name given by name.</li><li>S: return the document as a string (name is ignored).</li><li>FI: equivalent to F + I option</li><li>FD: equivalent to F + D option</li><li>E: return the document as base64 mime multi-part email attachment (RFC 2045)</li></ul>
 184       * @public
 185       * @since Moodle 1.0
 186       * @see Close()
 187       */
 188      public function Output($name='doc.pdf', $dest='I') {
 189          $olddebug = error_reporting(0);
 190          $result  = parent::output($name, $dest);
 191          error_reporting($olddebug);
 192          return $result;
 193      }
 194  
 195      /**
 196       * Is this font family one of core fonts?
 197       * @param string $fontfamily
 198       * @return bool
 199       */
 200      public function is_core_font_family($fontfamily) {
 201          return isset($this->CoreFonts[$fontfamily]);
 202      }
 203  
 204      /**
 205       * Returns list of font families and types of fonts.
 206       *
 207       * @return array multidimensional array with font families as keys and B, I, BI and N as values.
 208       */
 209      public function get_font_families() {
 210          $families = array();
 211          foreach ($this->fontlist as $font) {
 212              if (strpos($font, 'uni2cid') === 0) {
 213                  // This is not an font file.
 214                  continue;
 215              }
 216              if (strpos($font, 'cid0') === 0) {
 217                  // These do not seem to work with utf-8, better ignore them for now.
 218                  continue;
 219              }
 220              if (substr($font, -2) === 'bi') {
 221                  $family = substr($font, 0, -2);
 222                  if (in_array($family, $this->fontlist)) {
 223                      $families[$family]['BI'] = 'BI';
 224                      continue;
 225                  }
 226              }
 227              if (substr($font, -1) === 'i') {
 228                  $family = substr($font, 0, -1);
 229                  if (in_array($family, $this->fontlist)) {
 230                      $families[$family]['I'] = 'I';
 231                      continue;
 232                  }
 233              }
 234              if (substr($font, -1) === 'b') {
 235                  $family = substr($font, 0, -1);
 236                  if (in_array($family, $this->fontlist)) {
 237                      $families[$family]['B'] = 'B';
 238                      continue;
 239                  }
 240              }
 241              // This must be a Family or incomplete set of fonts present.
 242              $families[$font]['R'] = 'R';
 243          }
 244  
 245          // Sort everything consistently.
 246          ksort($families);
 247          foreach ($families as $k => $v) {
 248              krsort($families[$k]);
 249          }
 250  
 251          return $families;
 252      }
 253  }