Pdf.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 __;
  11. use function count;
  12. use function strlen;
  13. use function strtr;
  14. /**
  15. * PDF export base class providing basic configuration.
  16. */
  17. class Pdf extends TCPDF
  18. {
  19. /** @var array */
  20. public $footerset = [];
  21. /** @var array */
  22. public $alias = [];
  23. /**
  24. * PDF font to use.
  25. */
  26. public const PMA_PDF_FONT = 'DejaVuSans';
  27. /**
  28. * Constructs PDF and configures standard parameters.
  29. *
  30. * @param string $orientation page orientation
  31. * @param string $unit unit
  32. * @param string $format the format used for pages
  33. * @param bool $unicode true means that the input text is unicode
  34. * @param string $encoding charset encoding; default is UTF-8.
  35. * @param bool $diskcache DEPRECATED TCPDF FEATURE
  36. * @param false|int $pdfa If not false, set the document to PDF/A mode and the good version (1 or 3)
  37. *
  38. * @throws Exception
  39. */
  40. public function __construct(
  41. $orientation = 'P',
  42. $unit = 'mm',
  43. $format = 'A4',
  44. $unicode = true,
  45. $encoding = 'UTF-8',
  46. $diskcache = false,
  47. $pdfa = false
  48. ) {
  49. parent::__construct($orientation, $unit, $format, $unicode, $encoding, $diskcache, $pdfa);
  50. $this->setAuthor('phpMyAdmin ' . Version::VERSION);
  51. $this->AddFont('DejaVuSans', '', 'dejavusans.php');
  52. $this->AddFont('DejaVuSans', 'B', 'dejavusansb.php');
  53. $this->setFont(self::PMA_PDF_FONT, '', 14);
  54. $this->setFooterFont([self::PMA_PDF_FONT, '', 14]);
  55. $this->setLanguageArray([
  56. 'a_meta_charset' => 'UTF-8',
  57. 'a_meta_dir' => $GLOBALS['text_dir'],
  58. 'a_meta_language' => $GLOBALS['lang'],
  59. 'w_page' => __('Page number:'),
  60. ]);
  61. }
  62. /**
  63. * This function must be named "Footer" to work with the TCPDF library
  64. */
  65. // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
  66. public function Footer(): void
  67. {
  68. // Check if footer for this page already exists
  69. if (isset($this->footerset[$this->page])) {
  70. return;
  71. }
  72. $this->setY(-15);
  73. $this->setFont(self::PMA_PDF_FONT, '', 14);
  74. $this->Cell(
  75. 0,
  76. 6,
  77. __('Page number:') . ' '
  78. . $this->getAliasNumPage() . '/' . $this->getAliasNbPages(),
  79. 'T',
  80. 0,
  81. 'C'
  82. );
  83. $this->Cell(0, 6, Util::localisedDate(), 0, 1, 'R');
  84. $this->setY(20);
  85. // set footerset
  86. $this->footerset[$this->page] = 1;
  87. }
  88. /**
  89. * Function to set alias which will be expanded on page rendering.
  90. *
  91. * @param string $name name of the alias
  92. * @param string $value value of the alias
  93. */
  94. public function setAlias($name, $value): void
  95. {
  96. // phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
  97. $name = TCPDF_FONTS::UTF8ToUTF16BE($name, false, true, $this->CurrentFont);
  98. $this->alias[$name] = TCPDF_FONTS::UTF8ToUTF16BE($value, false, true, $this->CurrentFont);
  99. // phpcs:enable
  100. }
  101. // phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
  102. /**
  103. * Improved with alias expanding.
  104. */
  105. public function _putpages(): void
  106. {
  107. if (count($this->alias) > 0) {
  108. $nbPages = count($this->pages);
  109. for ($n = 1; $n <= $nbPages; $n++) {
  110. $this->pages[$n] = strtr($this->pages[$n], $this->alias);
  111. }
  112. }
  113. parent::_putpages();
  114. }
  115. // phpcs:enable
  116. /**
  117. * Displays an error message
  118. *
  119. * @param string $error_message the error message
  120. */
  121. // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
  122. public function Error($error_message = ''): void
  123. {
  124. echo Message::error(
  125. __('Error while creating PDF:') . ' ' . $error_message
  126. )->getDisplay();
  127. exit;
  128. }
  129. /**
  130. * Sends file as a download to user.
  131. *
  132. * @param string $filename file name
  133. */
  134. public function download($filename): void
  135. {
  136. $pdfData = $this->getPDFData();
  137. ResponseRenderer::getInstance()->disable();
  138. Core::downloadHeader(
  139. $filename,
  140. 'application/pdf',
  141. strlen($pdfData)
  142. );
  143. echo $pdfData;
  144. }
  145. }