"Fossies" - the Fresh Open Source Software Archive

Member "phpMyAdmin-5.1.0-english/libraries/classes/Pdf.php" (24 Feb 2021, 4449 Bytes) of package /linux/www/phpMyAdmin-5.1.0-english.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Pdf.php" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 5.0.4-english_vs_5.1.0-english.

    1 <?php
    2 /**
    3  * TCPDF wrapper class.
    4  */
    5 
    6 declare(strict_types=1);
    7 
    8 namespace PhpMyAdmin;
    9 
   10 use Exception;
   11 use TCPDF;
   12 use TCPDF_FONTS;
   13 use function count;
   14 use function strlen;
   15 use function strtr;
   16 
   17 /**
   18  * PDF export base class providing basic configuration.
   19  */
   20 class Pdf extends TCPDF
   21 {
   22     /** @var array */
   23     public $footerset;
   24 
   25     /** @var array */
   26     public $alias = [];
   27 
   28     /**
   29      * PDF font to use.
   30      */
   31     public const PMA_PDF_FONT = 'DejaVuSans';
   32 
   33     /**
   34      * Constructs PDF and configures standard parameters.
   35      *
   36      * @param string $orientation page orientation
   37      * @param string $unit        unit
   38      * @param string $format      the format used for pages
   39      * @param bool   $unicode     true means that the input text is unicode
   40      * @param string $encoding    charset encoding; default is UTF-8.
   41      * @param bool   $diskcache   if true reduce the RAM memory usage by caching
   42      *                            temporary data on filesystem (slower).
   43      * @param bool   $pdfa        If TRUE set the document to PDF/A mode.
   44      *
   45      * @throws Exception
   46      *
   47      * @access public
   48      */
   49     public function __construct(
   50         $orientation = 'P',
   51         $unit = 'mm',
   52         $format = 'A4',
   53         $unicode = true,
   54         $encoding = 'UTF-8',
   55         $diskcache = false,
   56         $pdfa = false
   57     ) {
   58         parent::__construct(
   59             $orientation,
   60             $unit,
   61             $format,
   62             $unicode,
   63             $encoding,
   64             $diskcache,
   65             $pdfa
   66         );
   67         $this->SetAuthor('phpMyAdmin ' . PMA_VERSION);
   68         $this->AddFont('DejaVuSans', '', 'dejavusans.php');
   69         $this->AddFont('DejaVuSans', 'B', 'dejavusansb.php');
   70         $this->SetFont(self::PMA_PDF_FONT, '', 14);
   71         $this->setFooterFont([self::PMA_PDF_FONT, '', 14]);
   72     }
   73 
   74     /**
   75      * This function must be named "Footer" to work with the TCPDF library
   76      *
   77      * @return void
   78      */
   79     // @codingStandardsIgnoreLine
   80     public function Footer()
   81     {
   82         // Check if footer for this page already exists
   83         if (isset($this->footerset[$this->page])) {
   84             return;
   85         }
   86 
   87         $this->SetY(-15);
   88         $this->SetFont(self::PMA_PDF_FONT, '', 14);
   89         $this->Cell(
   90             0,
   91             6,
   92             __('Page number:') . ' '
   93             . $this->getAliasNumPage() . '/' . $this->getAliasNbPages(),
   94             'T',
   95             0,
   96             'C'
   97         );
   98         $this->Cell(0, 6, Util::localisedDate(), 0, 1, 'R');
   99         $this->SetY(20);
  100 
  101         // set footerset
  102         $this->footerset[$this->page] = 1;
  103     }
  104 
  105     /**
  106      * Function to set alias which will be expanded on page rendering.
  107      *
  108      * @param string $name  name of the alias
  109      * @param string $value value of the alias
  110      *
  111      * @return void
  112      */
  113     public function setAlias($name, $value)
  114     {
  115         $name = TCPDF_FONTS::UTF8ToUTF16BE(
  116             $name,
  117             false,
  118             true,
  119             $this->CurrentFont
  120         );
  121         $this->alias[$name] = TCPDF_FONTS::UTF8ToUTF16BE(
  122             $value,
  123             false,
  124             true,
  125             $this->CurrentFont
  126         );
  127     }
  128 
  129     // phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
  130 
  131     /**
  132      * Improved with alias expanding.
  133      *
  134      * @return void
  135      */
  136     public function _putpages()
  137     {
  138         if (count($this->alias) > 0) {
  139             $nbPages = count($this->pages);
  140             for ($n = 1; $n <= $nbPages; $n++) {
  141                 $this->pages[$n] = strtr($this->pages[$n], $this->alias);
  142             }
  143         }
  144         parent::_putpages();
  145         // phpcs:enable
  146     }
  147 
  148     /**
  149      * Displays an error message
  150      *
  151      * @param string $error_message the error message
  152      *
  153      * @return void
  154      */
  155     // @codingStandardsIgnoreLine
  156     public function Error($error_message = '')
  157     {
  158         echo Message::error(
  159             __('Error while creating PDF:') . ' ' . $error_message
  160         )->getDisplay();
  161         exit;
  162     }
  163 
  164     /**
  165      * Sends file as a download to user.
  166      *
  167      * @param string $filename file name
  168      *
  169      * @return void
  170      */
  171     public function download($filename)
  172     {
  173         $pdfData = $this->getPDFData();
  174         Response::getInstance()->disable();
  175         Core::downloadHeader(
  176             $filename,
  177             'application/pdf',
  178             strlen($pdfData)
  179         );
  180         echo $pdfData;
  181     }
  182 }