Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Produces a sample PDF using lib/pdflib.php
  19   *
  20   * @package    core
  21   * @copyright  2009 David Mudrak <david.mudrak@gmail.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require(__DIR__ . '/../../../config.php');
  26  require_once($CFG->libdir . '/pdflib.php');
  27  
  28  require_login();
  29  $context = context_system::instance();
  30  require_capability('moodle/site:config', $context);
  31  
  32  $getpdf     = optional_param('getpdf', 0, PARAM_INT);
  33  $fontfamily = optional_param('fontfamily', PDF_FONT_NAME_MAIN, PARAM_ALPHA);  // to be configurable
  34  
  35  if (!$fontfamily) {
  36      $fontfamily = PDF_FONT_NAME_MAIN;
  37  }
  38  
  39  /**
  40   * Extend the standard PDF class to get access to some protected values we want to display
  41   * at the test page.
  42   *
  43   * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
  44   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  45   */
  46  if ($getpdf) {
  47      $doc = new pdf();
  48  
  49      $doc->SetTitle('Moodle PDF library test');
  50      $doc->SetAuthor('Moodle ' . $CFG->release);
  51      $doc->SetCreator('lib/tests/other/pdflibtestpage.php');
  52      $doc->SetKeywords('Moodle, PDF');
  53      $doc->SetSubject('This has been generated by Moodle as its PDF library test page');
  54      $doc->SetMargins(15, 30);
  55  
  56      $doc->setPrintHeader(true);
  57      $doc->setHeaderMargin(10);
  58      $doc->setHeaderFont(array($fontfamily, 'b', 10));
  59      $doc->setHeaderData('pix/moodlelogo.png', 40, $SITE->fullname, $CFG->wwwroot);
  60  
  61      $doc->setPrintFooter(true);
  62      $doc->setFooterMargin(10);
  63      $doc->setFooterFont(array($fontfamily, '', 8));
  64  
  65      $doc->AddPage();
  66  
  67      $doc->SetTextColor(255,255,255);
  68      $doc->SetFillColor(255,203,68);
  69      $doc->SetFont($fontfamily, 'B', 24);
  70      $doc->Cell(0, 0, 'Moodle PDF library test', 0, 1, 'C', 1);
  71  
  72      $doc->SetFont($fontfamily, '', 12);
  73      $doc->Ln(6);
  74      $doc->SetTextColor(0,0,0);
  75  
  76      $c  = '<h3>General information</h3>';
  77      $c .= 'Moodle release: '            . $CFG->release . '<br />';
  78      $c .= 'PDF producer: TCPDF '        . TCPDF_STATIC::getTCPDFVersion() . ' (http://www.tcpdf.org) <br />';
  79      $c .= 'Font family used: '          . $fontfamily   . '<br />';
  80  
  81      $c .= '<h3>Current settings</h3>';
  82      $c .= '<table border="1"  cellspacing="0" cellpadding="1">';
  83      foreach (array('K_PATH_MAIN', 'K_PATH_URL', 'K_PATH_FONTS', 'PDF_FONT_NAME_MAIN', 'K_PATH_CACHE', 'K_PATH_IMAGES', 'K_BLANK_IMAGE',
  84                          'K_CELL_HEIGHT_RATIO', 'K_SMALL_RATIO', 'PDF_CUSTOM_FONT_PATH', 'PDF_DEFAULT_FONT') as $setting) {
  85          if (defined($setting)) {
  86              $c .= '<tr style="font-size: x-small;"><td>' . $setting . '</td><td>' . constant($setting) . '</td></tr>';
  87          }
  88      }
  89      $c .= '</table><br />';
  90  
  91      $c .= '<h3>Available font families</h3>';
  92      $fontfamilies = $doc->get_font_families();
  93      $list = array();
  94      foreach ($fontfamilies as $family => $fonts) {
  95          $f = $family;
  96          $spacer = '';
  97          if ($doc->is_core_font_family($family)) {
  98              $f .= '<sup>*</sup>';
  99          } else {
 100              $spacer = ' ';
 101          }
 102          if (count($fonts) > 1) {
 103              $f .= $spacer . '<i>(' . implode(', ', $fonts) . ')</i>';
 104          }
 105          $list[] = $f;
 106      }
 107      $c .= implode(', ', $list);
 108      $c .= '<p><i><small>Note: * Standard core fonts are not embedded in PDF files, PDF viewers are using local fonts.</small></i></p>';
 109  
 110      $c .= '<h3>Installed languages and their alphabets</h3>';
 111      $languages = array();
 112      $langdirs = get_list_of_plugins('lang', '', $CFG->dataroot);
 113      array_unshift($langdirs, 'en');
 114      foreach ($langdirs as $langdir) {
 115          $enlangconfig = $CFG->dirroot . '/lang/en/langconfig.php';
 116          if ('en' == $langdir) {
 117              $langconfig = $enlangconfig;
 118          } else {
 119              $langconfig = $CFG->dataroot . '/lang/' . $langdir . '/langconfig.php';
 120          }
 121          // Ignore parents here for now.
 122          $string = array();
 123          if (is_readable($langconfig)) {
 124              include($langconfig);
 125              if (is_array($string)) {
 126                  $languages[$langdir] = new stdClass();
 127                  $languages[$langdir]->langname = isset($string['thislanguage']) ? $string['thislanguage'] : '(unknown)';
 128                  $languages[$langdir]->alphabet = isset($string['alphabet']) ? '"' . $string['alphabet'] . '"': '(no alphabet defined)';
 129              }
 130          }
 131      }
 132      $c .= '<dl>';
 133      foreach ($languages as $langcode => $language) {
 134          $c .= '<dt>' . $language->langname . ' (' . $langcode . ')</dt>';
 135          $c .= '<dd>' . $language->alphabet . '</dd>';
 136      }
 137      $c .= '</dl>';
 138  
 139      $doc->writeHTML($c);
 140  
 141      $doc->Output('pdflibtestpage.pdf');
 142      exit();
 143  }
 144  
 145  $PAGE->set_url('/lib/tests/other/pdflibtestpage.php');
 146  $PAGE->set_context($context);
 147  $PAGE->set_title('PDF library test');
 148  $PAGE->set_heading('PDF library test');
 149  
 150  echo $OUTPUT->header();
 151  echo $OUTPUT->heading('Press the button to generate test PDF', 2);
 152  echo $OUTPUT->continue_button(new moodle_url($PAGE->url, array('getpdf' => 1, 'fontfamily' => PDF_FONT_NAME_MAIN)));
 153  echo $OUTPUT->footer();