Translated using Weblate (Portuguese)
[phpmyadmin.git] / src / Pdf.php
blobb105fac73cb55f4325720b793997d2d1c62a5754
1 <?php
2 /**
3 * TCPDF wrapper class.
4 */
6 declare(strict_types=1);
8 namespace PhpMyAdmin;
10 use Exception;
11 use TCPDF;
12 use TCPDF_FONTS;
14 use function __;
15 use function count;
16 use function strtr;
18 /**
19 * PDF export base class providing basic configuration.
21 class Pdf extends TCPDF
23 /** @var mixed[] */
24 public array $footerset = [];
26 /** @var mixed[] */
27 public array $alias = [];
29 /**
30 * PDF font to use.
32 public const PMA_PDF_FONT = 'DejaVuSans';
34 /**
35 * Constructs PDF and configures standard parameters.
37 * @param string $orientation page orientation
38 * @param string $unit unit
39 * @param string $format the format used for pages
40 * @param bool $unicode true means that the input text is unicode
41 * @param string $encoding charset encoding; default is UTF-8.
42 * @param bool $diskcache DEPRECATED TCPDF FEATURE
43 * @param false|int $pdfa If not false, set the document to PDF/A mode and the good version (1 or 3)
45 * @throws Exception
47 * @infection-ignore-all
49 public function __construct(
50 string $orientation = 'P',
51 string $unit = 'mm',
52 string $format = 'A4',
53 bool $unicode = true,
54 string $encoding = 'UTF-8',
55 bool $diskcache = false,
56 false|int $pdfa = false,
57 ) {
58 parent::__construct($orientation, $unit, $format, $unicode, $encoding, $diskcache, $pdfa);
60 $this->setAuthor('phpMyAdmin ' . Version::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]);
67 /**
68 * This function must be named "Footer" to work with the TCPDF library
70 // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
71 public function Footer(): void
73 // Check if footer for this page already exists
74 if (isset($this->footerset[$this->page])) {
75 return;
78 $this->setY(-15);
79 $this->setFont(self::PMA_PDF_FONT, '', 14);
80 $this->Cell(
83 __('Page number:') . ' '
84 . $this->getAliasNumPage() . '/' . $this->getAliasNbPages(),
85 'T',
87 'C',
89 $this->Cell(0, 6, Util::localisedDate(), 0, 1, 'R');
90 $this->setY(20);
92 // set footerset
93 $this->footerset[$this->page] = 1;
96 /**
97 * Function to set alias which will be expanded on page rendering.
99 * @param string $name name of the alias
100 * @param string $value value of the alias
102 public function setAlias(string $name, string $value): void
104 // phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
105 $name = TCPDF_FONTS::UTF8ToUTF16BE($name, false, true, $this->CurrentFont);
106 $this->alias[$name] = TCPDF_FONTS::UTF8ToUTF16BE($value, false, true, $this->CurrentFont);
107 // phpcs:enable
110 // phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
113 * Improved with alias expanding.
115 public function _putpages(): void
117 if ($this->alias !== []) {
118 $nbPages = count($this->pages);
119 for ($n = 1; $n <= $nbPages; $n++) {
120 $this->pages[$n] = strtr($this->pages[$n], $this->alias);
124 parent::_putpages();
127 // phpcs:enable
130 * Displays an error message
132 * @param string $msg the error message
134 // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
135 public function Error(mixed $msg = ''): never
137 echo Message::error(
138 __('Error while creating PDF:') . ' ' . $msg,
139 )->getDisplay();
140 ResponseRenderer::getInstance()->callExit();