]>
git.agnieray.net Git - galette.git/blob - galette/lib/Galette/IO/Pdf.php
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * PDF class for galette
7 * Traps tcpdf errors by overloading tcpdf::error method
8 * Adds convenient method to convert color html codes
12 * Copyright © 2007-2023 The Galette Team
14 * This file is part of Galette (http://galette.tuxfamily.org).
16 * Galette is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * Galette is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
32 * @author John Perr <johnperr@abul.org>
33 * @author Johan Cwiklinski <johan@x-tnd.be>
34 * @copyright 2007-2023 The Galette Team
35 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
36 * @link http://galette.tuxfamily.org
37 * @since Available since 0.7dev - 2007-07-21
42 use Galette\Core\Preferences
;
43 use Galette\Entity\PdfModel
;
45 use Slim\Routing\RouteParser
;
48 * TCPDF configuration file for Galette
50 require_once GALETTE_CONFIG_PATH
. 'galette_tcpdf_config.php';
53 * PDF class for galette
58 * @abstract Class for expanding TCPDF.
59 * @author John Perr <johnperr@abul.org>
60 * @author Johan Cwiklinski <johan@x-tnd.be>
61 * @copyright 2007-2023 The Galette Team
62 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
63 * @link http://galette.tuxfamily.org
64 * @since Available since 0.7dev - 2007-07-21
67 class Pdf
extends \TCPDF
69 public const FONT
= 'DejaVuSans';
70 public const FONT_SIZE
= 10;
72 protected $preferences;
75 private $paginated = false;
79 * Main constructor, set creator and author
81 * @param Preferences $prefs Preferences
82 * @param ?PdfModel $model Related model
84 public function __construct(Preferences
$prefs, ?PdfModel
$model = null)
88 $this->preferences
= $prefs;
90 parent
::__construct('P', 'mm', 'A4', true, 'UTF-8');
92 $this->SetCreator(PDF_CREATOR
);
93 //add helvetica, hard-called from lib
94 $this->SetFont('helvetica');
95 //and then, set real font
96 $this->SetFont(self
::FONT
, '', self
::FONT_SIZE
);
99 $this->preferences
->pref_nom
,
103 $name . ' (using Galette ' . GALETTE_VERSION
. ')'
106 if ($this->i18n
->isRTL()) {
110 if ($model !== null) {
111 $this->model
= $model;
112 $this->SetTitle($this->model
->htitle
);
117 * Set show pagination
121 public function showPagination()
123 $this->paginated
= true;
129 public function __destruct()
131 parent
::__destruct();
135 * This method is automatically called in case of fatal error;
136 * it simply outputs the message and halts the execution.
137 * An inherited class may override it to customize the error
138 * handling but should always halt the script, or the resulting
139 * document would probably be invalid.
140 * 2004-06-11 :: Nicola Asuni : changed bold tag with strong
141 * 2007-07-21 :: John Perr : changed function to return error to session
142 * 2017-02-14 :: Johan Cwiklinski : use slim's flash message; do not rely on session for redirect
144 * @param string $msg The error message
150 public function Error($msg) // phpcs:ignore PSR1.Methods.CamelCapsMethodName
155 'PDF error: ' . $msg,
159 $container->get('flash')->addMessage(
164 $redirect = (isset($_SERVER['HTTP_REFERER']) ?
165 $_SERVER['HTTP_REFERER'] : $container->get(RouteParser
::class)->urlFor('slash'));
166 header('Location: ' . $redirect);
171 * Converts color from HTML format #RRVVBB
172 * to RGB 3 colors array.
174 * @param string $hex6 7 chars string #RRVVBB
178 public function colorHex2Dec($hex6)
181 "R" => hexdec(substr($hex6, 1, 2)),
182 "G" => hexdec(substr($hex6, 3, 2)),
183 "B" => hexdec(substr($hex6, 5, 2))
189 * Draws PDF page Header
193 public function Header() // phpcs:ignore PSR1.Methods.CamelCapsMethodName
195 //just ovverride default header to prevent black line at top
199 * Draws PDF page footer
203 public function Footer() // phpcs:ignore PSR1.Methods.CamelCapsMethodName
206 if (isset($this->model
)) {
208 if (trim($this->model
->hstyles
) !== '') {
209 $hfooter .= "<style>\n" . $this->model
->hstyles
. "\n</style>\n\n";
211 $hfooter .= $this->model
->hfooter
;
212 $this->writeHtml($hfooter);
214 $this->SetFont(self
::FONT
, '', self
::FONT_SIZE
- 2);
215 $this->SetTextColor(0, 0, 0);
217 $name = preg_replace(
219 $this->preferences
->pref_nom
,
223 $address = $this->preferences
->getPostalAddress();
233 if ($this->paginated
) {
234 $this->SetFont(self
::FONT
, '', self
::FONT_SIZE
- 3);
239 $this->getAliasRightShift() . $this->PageNo() .
240 '/' . $this->getAliasNbPages(),
243 ($this->i18n
->isRTL() ?
'L' : 'R')
250 * Draws PDF page header
252 * @param string $title Additionnal title to display just after logo
256 public function PageHeader($title = null) // phpcs:ignore PSR1.Methods.CamelCapsMethodName
258 if (isset($this->model
)) {
259 $this->modelPageHeader($title);
261 $this->standardPageHeader($title);
266 * Draws models PDF page header
268 * @param string $title Additionnal title to display just after logo
272 protected function modelPageHeader($title = null)
275 if (trim($this->model
->hstyles
) !== '') {
276 $html .= "<style>\n" . $this->model
->hstyles
. "\n</style>\n\n";
278 $html .= "<div dir=\"" . ($this->i18n
->isRTL() ?
'rtl' : 'ltr') . "\">" . $this->model
->hheader
. "</div>";
279 $this->writeHtml($html, true, false, true, false, '');
281 if ($title !== null) {
282 $this->writeHtml('<h2 style="text-align:center;">' . $title . '</h2>');
285 if (trim($this->model
->title
) !== '') {
287 if (trim($this->model
->hstyles
) !== '') {
288 $htitle .= "<style>\n" . $this->model
->hstyles
.
291 $htitle .= '<div id="pdf_title">' . $this->model
->htitle
. '</div>';
292 $this->writeHtml($htitle);
294 if (trim($this->model
->subtitle
) !== '') {
296 if (trim($this->model
->hstyles
) !== '') {
297 $hsubtitle .= "<style>\n" . $this->model
->hstyles
.
300 $hsubtitle .= '<div id="pdf_subtitle">' . $this->model
->hsubtitle
.
302 $this->writeHtml($hsubtitle);
305 trim($this->model
->title
) !== ''
306 ||
trim($this->model
->subtitle
) !== ''
313 * Draws standard PDF page header
315 * @param string $title Additionnal title to display just after logo
319 protected function standardPageHeader($title = null)
322 $print_logo = new \Galette\Core\
PrintLogo();
323 $logofile = $print_logo->getPath();
325 // Set logo size to max width 30 mm or max height 25 mm
326 $ratio = $print_logo->getWidth() / $print_logo->getHeight();
328 if ($print_logo->getHeight() > 16) {
331 $hlogo = $print_logo->getHeight();
333 $wlogo = round($hlogo * $ratio);
335 if ($print_logo->getWidth() > 16) {
338 $wlogo = $print_logo->getWidth();
340 $hlogo = round($wlogo / $ratio);
343 $this->SetFont(self
::FONT
, 'B', self
::FONT_SIZE +
4);
344 $this->SetTextColor(0, 0, 0);
348 $ystart = $this->GetY();
353 $this->preferences
->pref_nom
,
355 ($this->i18n
->isRTL() ?
'R' : 'L')
357 $this->SetFont(self
::FONT
, 'B', self
::FONT_SIZE +
2);
359 if ($title !== null) {
360 $this->Cell(0, 6, $title, 0, 1, ($this->i18n
->isRTL() ?
'R' : 'L'), 0);
362 $yend = $this->getY(); //store position at the end of the text
364 $this->SetY($ystart);
365 if ($this->i18n
->isRTL()) {
368 $x = 190 - $wlogo; //right align
370 $this->Image($logofile, $x, $this->GetY(), $wlogo, $hlogo);
371 $this->y +
= $hlogo +
3;
372 //if position after logo is < than position after text,
373 //we have to change y
374 if ($this->getY() < $yend) {
380 * Draws body from model
384 public function PageBody() // phpcs:ignore PSR1.Methods.CamelCapsMethodName
387 if (trim($this->model
->hstyles
) !== '') {
388 $hbody .= "<style>\n" . $this->model
->hstyles
. "\n</style>\n\n";
390 $hbody .= $this->model
->hbody
;
391 $this->writeHtml($hbody);
397 * @param string $text Text content
398 * @param integer $maxsize Maximal size
399 * @param integer $fontsize Font size
400 * @param string $fontstyle Font style (defaults to '')
401 * @param string $fontname Font name (defaults to static::FONT)
405 protected function fixSize($text, $maxsize, $fontsize, $fontstyle = '', $fontname = null)
407 if ($fontname === null) {
408 $fontname = static::FONT
;
410 $this->SetFontSize($fontsize);
411 while ((int)$this->GetStringWidth($text, $fontname, $fontstyle, $fontsize) > $maxsize) {
413 $this->SetFontSize($fontsize);
420 * @param string $str Original string
421 * @param integer $length Max length
425 protected function cut($str, $length)
427 $length = $length - 2; //keep a margin
428 if ((int)$this->GetStringWidth($str) > $length) {
429 while ((int)$this->GetStringWidth($str . '...') > $length) {
430 $str = mb_substr($str, 0, -1, 'UTF-8');
438 * Stretch a header string
440 * @param string $str Original string
441 * @param integer $length Max length
445 protected function stretchHead($str, $length)
447 $this->SetFont(self
::FONT
, 'B', self
::FONT_SIZE
);
449 if ((int)$this->GetStringWidth($str) > $length) {
450 while ((int)$this->GetStringWidth($str) > $length) {
451 $this->setFontStretching(--$stretch);
462 public function getFilename()
464 return $this->filename
;
468 * Download PDF from browser
472 public function download(): string
474 return $this->Output($this->filename
, 'D');