Pdf.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * TCPDF wrapper class.
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. declare(strict_types=1);
  9. namespace PhpMyAdmin;
  10. use Exception;
  11. use PhpMyAdmin\Core;
  12. use PhpMyAdmin\Message;
  13. use PhpMyAdmin\Response;
  14. use PhpMyAdmin\Util;
  15. use TCPDF;
  16. use TCPDF_FONTS;
  17. /**
  18. * PDF export base class providing basic configuration.
  19. *
  20. * @package PhpMyAdmin
  21. */
  22. class Pdf extends TCPDF
  23. {
  24. public $footerset;
  25. public $Alias = [];
  26. /**
  27. * PDF font to use.
  28. */
  29. public const PMA_PDF_FONT = 'DejaVuSans';
  30. /**
  31. * Constructs PDF and configures standard parameters.
  32. *
  33. * @param string $orientation page orientation
  34. * @param string $unit unit
  35. * @param string $format the format used for pages
  36. * @param boolean $unicode true means that the input text is unicode
  37. * @param string $encoding charset encoding; default is UTF-8.
  38. * @param boolean $diskcache if true reduce the RAM memory usage by caching
  39. * temporary data on filesystem (slower).
  40. * @param boolean $pdfa If TRUE set the document to PDF/A mode.
  41. *
  42. * @throws Exception
  43. * @access public
  44. */
  45. public function __construct(
  46. $orientation = 'P',
  47. $unit = 'mm',
  48. $format = 'A4',
  49. $unicode = true,
  50. $encoding = 'UTF-8',
  51. $diskcache = false,
  52. $pdfa = false
  53. ) {
  54. parent::__construct(
  55. $orientation,
  56. $unit,
  57. $format,
  58. $unicode,
  59. $encoding,
  60. $diskcache,
  61. $pdfa
  62. );
  63. $this->SetAuthor('phpMyAdmin ' . PMA_VERSION);
  64. $this->AddFont('DejaVuSans', '', 'dejavusans.php');
  65. $this->AddFont('DejaVuSans', 'B', 'dejavusansb.php');
  66. $this->SetFont(Pdf::PMA_PDF_FONT, '', 14);
  67. $this->setFooterFont([Pdf::PMA_PDF_FONT, '', 14]);
  68. }
  69. /**
  70. * This function must be named "Footer" to work with the TCPDF library
  71. *
  72. * @return void
  73. */
  74. // @codingStandardsIgnoreLine
  75. public function Footer()
  76. {
  77. // Check if footer for this page already exists
  78. if (! isset($this->footerset[$this->page])) {
  79. $this->SetY(-15);
  80. $this->SetFont(Pdf::PMA_PDF_FONT, '', 14);
  81. $this->Cell(
  82. 0,
  83. 6,
  84. __('Page number:') . ' '
  85. . $this->getAliasNumPage() . '/' . $this->getAliasNbPages(),
  86. 'T',
  87. 0,
  88. 'C'
  89. );
  90. $this->Cell(0, 6, Util::localisedDate(), 0, 1, 'R');
  91. $this->SetY(20);
  92. // set footerset
  93. $this->footerset[$this->page] = 1;
  94. }
  95. }
  96. /**
  97. * Function to set alias which will be expanded on page rendering.
  98. *
  99. * @param string $name name of the alias
  100. * @param string $value value of the alias
  101. *
  102. * @return void
  103. */
  104. public function setAlias($name, $value)
  105. {
  106. $name = TCPDF_FONTS::UTF8ToUTF16BE(
  107. $name,
  108. false,
  109. true,
  110. $this->CurrentFont
  111. );
  112. $this->Alias[$name] = TCPDF_FONTS::UTF8ToUTF16BE(
  113. $value,
  114. false,
  115. true,
  116. $this->CurrentFont
  117. );
  118. }
  119. /**
  120. * Improved with alias expanding.
  121. *
  122. * @return void
  123. */
  124. public function _putpages()
  125. {
  126. if (count($this->Alias) > 0) {
  127. $nbPages = count($this->pages);
  128. for ($n = 1; $n <= $nbPages; $n++) {
  129. $this->pages[$n] = strtr($this->pages[$n], $this->Alias);
  130. }
  131. }
  132. parent::_putpages();
  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. Message::error(
  145. __('Error while creating PDF:') . ' ' . $error_message
  146. )->display();
  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. }