]>
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
9 * Adds a _parsegif function to convert gif to png
13 * Copyright © 2007-2014 The Galette Team
15 * This file is part of Galette (http://galette.tuxfamily.org).
17 * Galette is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * Galette is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
33 * @author John Perr <johnperr@abul.org>
34 * @author Johan Cwiklinski <johan@x-tnd.be>
35 * @copyright 2007-2014 The Galette Team
36 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
37 * @link http://galette.tuxfamily.org
38 * @since Available since 0.7dev - 2007-07-21
43 use Galette\Core\Preferences
;
44 use Galette\Entity\PdfModel
;
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-2014 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
70 public const FONT
= 'DejaVuSans';
71 public const FONT_SIZE
= 10;
73 protected $preferences;
76 private $paginated = false;
80 * Main constructor, set creator and author
82 * @param Preferences $prefs Preferences
83 * @param PdfModel $model Related model
85 public function __construct(Preferences
$prefs, $model = null)
89 $this->preferences
= $prefs;
91 parent
::__construct('P', 'mm', 'A4', true, 'UTF-8');
93 $this->SetCreator(PDF_CREATOR
);
94 //add helvetica, hard-called from lib
95 $this->SetFont('helvetica');
96 //and then, set real font
97 $this->SetFont(self
::FONT
, '', self
::FONT_SIZE
);
100 $this->preferences
->pref_nom
,
104 $name . ' (using Galette ' . GALETTE_VERSION
. ')'
107 if ($this->i18n
->isRTL()) {
111 if ($model !== null) {
112 if ($model instanceof PdfModel
) {
113 $this->model
= $model;
114 $this->SetTitle($this->model
->htitle
);
116 throw new \
UnexpectedValueException(
117 'Provided model must be an instance of PdfModel!'
124 * Set show pagination
128 public function showPagination()
130 $this->paginated
= true;
136 public function __destruct()
138 parent
::__destruct();
142 * This method is automatically called in case of fatal error;
143 * it simply outputs the message and halts the execution.
144 * An inherited class may override it to customize the error
145 * handling but should always halt the script, or the resulting
146 * document would probably be invalid.
147 * 2004-06-11 :: Nicola Asuni : changed bold tag with strong
148 * 2007-07-21 :: John Perr : changed function to return error to session
149 * 2017-02-14 :: Johan Cwiklinski : use slim's flash message; do not rely on session for redirect
151 * @param string $msg The error message
157 public function Error($msg) // phpcs:ignore PSR1.Methods.CamelCapsMethodName
162 'PDF error: ' . $msg,
166 $container->get('flash')->addMessage(
171 $redirect = (isset($_SERVER['HTTP_REFERER']) ?
172 $_SERVER['HTTP_REFERER'] : $container->get('router')->pathFor('slash'));
173 header('Location: ' . $redirect);
178 * Converts color from HTML format #RRVVBB
179 * to RGB 3 colors array.
181 * @param string $hex6 7 chars string #RRVVBB
185 public function colorHex2Dec($hex6)
188 "R" => hexdec(substr($hex6, 1, 2)),
189 "G" => hexdec(substr($hex6, 3, 2)),
190 "B" => hexdec(substr($hex6, 5, 2))
195 /** FIXME: is this function used somewhere? */
197 * Extract info from a GIF file
198 * (In fact: converts gif image to png and feeds it to _parsepng)
200 * @param string $file path to the gif file
205 protected function parsegif($file)
207 $a = getimagesize($file);
209 $this->Error(_T("Missing or incorrect image file ") . $file);
212 $this->Error(_T("Not a GIF file ") . $file);
215 // Tentative d'ouverture du fichier
216 if (function_exists('gd_info')) {
217 $data = @imagecreatefromgif
($file);
219 // Test d'échec & Affichage d'un message d'erreur
221 $this->Error(_T("Error loading ") . $file);
223 if (imagepng($data, GALETTE_ROOT
. 'tempimages/gif2png.png')) {
224 return $this->_parsepng(GALETTE_ROOT
. 'tempimages/gif2png.png');
226 $this->Error(_T("Error creating temporary png file from ") . $file);
229 $this->Error(_T("Unable to convert GIF file ") . $file);
234 * Draws PDF page Header
238 public function Header() // phpcs:ignore PSR1.Methods.CamelCapsMethodName
240 //just ovverride default header to prevent black line at top
244 * Draws PDF page footer
248 public function Footer() // phpcs:ignore PSR1.Methods.CamelCapsMethodName
251 if (isset($this->model
)) {
253 if (trim($this->model
->hstyles
) !== '') {
254 $hfooter .= "<style>\n" . $this->model
->hstyles
. "\n</style>\n\n";
256 $hfooter .= $this->model
->hfooter
;
257 $this->writeHtml($hfooter);
259 $this->SetFont(self
::FONT
, '', self
::FONT_SIZE
- 2);
260 $this->SetTextColor(0, 0, 0);
262 $name = preg_replace(
264 $this->preferences
->pref_nom
,
268 $address = $this->preferences
->getPostalAddress();
278 if ($this->paginated
) {
279 $this->SetFont(self
::FONT
, '', self
::FONT_SIZE
- 3);
284 $this->getAliasRightShift() . $this->PageNo() .
285 '/' . $this->getAliasNbPages(),
288 ($this->i18n
->isRTL() ?
'L' : 'R')
295 * Draws PDF page header
297 * @param string $title Additionnal title to display just after logo
301 public function PageHeader($title = null) // phpcs:ignore PSR1.Methods.CamelCapsMethodName
303 if (isset($this->model
)) {
304 $this->modelPageHeader($title);
306 $this->standardPageHeader($title);
311 * Draws models PDF page header
313 * @param string $title Additionnal title to display just after logo
317 protected function modelPageHeader($title = null)
320 if (trim($this->model
->hstyles
) !== '') {
321 $html .= "<style>\n" . $this->model
->hstyles
. "\n</style>\n\n";
323 $html .= "<div dir=\"" . ($this->i18n
->isRTL() ?
'rtl' : 'ltr') . "\">" . $this->model
->hheader
. "</div>";
324 $this->writeHtml($html, true, false, true, false, '');
326 if ($title !== null) {
327 $this->writeHtml('<h2 style="text-align:center;">' . $title . '</h2>');
330 if (trim($this->model
->title
) !== '') {
332 if (trim($this->model
->hstyles
) !== '') {
333 $htitle .= "<style>\n" . $this->model
->hstyles
.
336 $htitle .= '<div id="pdf_title">' . $this->model
->htitle
. '</div>';
337 $this->writeHtml($htitle);
339 if (trim($this->model
->subtitle
) !== '') {
341 if (trim($this->model
->hstyles
) !== '') {
342 $hsubtitle .= "<style>\n" . $this->model
->hstyles
.
345 $hsubtitle .= '<div id="pdf_subtitle">' . $this->model
->hsubtitle
.
347 $this->writeHtml($hsubtitle);
350 trim($this->model
->title
) !== ''
351 ||
trim($this->model
->subtitle
) !== ''
358 * Draws standard PDF page header
360 * @param string $title Additionnal title to display just after logo
364 protected function standardPageHeader($title = null)
367 $print_logo = new \Galette\Core\
PrintLogo();
368 $logofile = $print_logo->getPath();
370 // Set logo size to max width 30 mm or max height 25 mm
371 $ratio = $print_logo->getWidth() / $print_logo->getHeight();
373 if ($print_logo->getHeight() > 16) {
376 $hlogo = $print_logo->getHeight();
378 $wlogo = round($hlogo * $ratio);
380 if ($print_logo->getWidth() > 16) {
383 $wlogo = $print_logo->getWidth();
385 $hlogo = round($wlogo / $ratio);
388 $this->SetFont(self
::FONT
, 'B', self
::FONT_SIZE +
4);
389 $this->SetTextColor(0, 0, 0);
393 $ystart = $this->GetY();
398 $this->preferences
->pref_nom
,
400 ($this->i18n
->isRTL() ?
'R' : 'L')
402 $this->SetFont(self
::FONT
, 'B', self
::FONT_SIZE +
2);
404 if ($title !== null) {
405 $this->Cell(0, 6, $title, 0, 1, ($this->i18n
->isRTL() ?
'R' : 'L'), 0);
407 $yend = $this->getY(); //store position at the end of the text
409 $this->SetY($ystart);
410 if ($this->i18n
->isRTL()) {
413 $x = 190 - $wlogo; //right align
415 $this->Image($logofile, $x, $this->GetY(), $wlogo, $hlogo);
416 $this->y +
= $hlogo +
3;
417 //if position after logo is < than position after text,
418 //we have to change y
419 if ($this->getY() < $yend) {
425 * Draws body from model
429 public function PageBody() // phpcs:ignore PSR1.Methods.CamelCapsMethodName
432 if (trim($this->model
->hstyles
) !== '') {
433 $hbody .= "<style>\n" . $this->model
->hstyles
. "\n</style>\n\n";
435 $hbody .= $this->model
->hbody
;
436 $this->writeHtml($hbody);
442 * @param string $text Text content
443 * @param integer $maxsize Maximal size
444 * @param integer $fontsize Font size
445 * @param string $fontstyle Font style (defaults to '')
446 * @param string $fontname Font name (defaults to static::FONT)
450 protected function fixSize($text, $maxsize, $fontsize, $fontstyle = '', $fontname = null)
452 if ($fontname === null) {
453 $fontname = static::FONT
;
455 $this->SetFontSize($fontsize);
456 while ($this->GetStringWidth($text, $fontname, $fontstyle, $fontsize) > $maxsize) {
458 $this->SetFontSize($fontsize);
465 * @param string $str Original string
466 * @param integer $length Max length
470 protected function cut($str, $length)
472 $length = $length - 2; //keep a margin
473 if ($this->GetStringWidth($str) > $length) {
474 while ($this->GetStringWidth($str . '...') > $length) {
475 $str = mb_substr($str, 0, -1, 'UTF-8');
483 * Stretch a header string
485 * @param string $str Original string
486 * @param integer $length Max length
490 protected function stretchHead($str, $length)
492 $this->SetFont(self
::FONT
, 'B', self
::FONT_SIZE
);
494 if ($this->GetStringWidth($str) > $length) {
495 while ($this->GetStringWidth($str) > $length) {
496 $this->setFontStretching(--$stretch);
507 public function getFilename()
509 return $this->filename
;
513 * Download PDF from browser
517 public function download()
519 $this->Output($this->filename
, 'D');