Pdf.php 4.1 KB

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