Pdf.php 4.3 KB

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