]>
git.agnieray.net Git - galette.git/blob - galette/lib/Galette/IO/Pdf.php
e36620cf883cb4a3b04651663b050a4fa366d0bb
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
38 * @link http://galette.tuxfamily.org
39 * @since Available since 0.7dev - 2007-07-21
44 use Galette\Core\Preferences
;
45 use Galette\Entity\PdfModel
;
49 * TCPDF configuration file for Galette
51 require_once GALETTE_CONFIG_PATH
. 'galette_tcpdf_config.php';
54 * PDF class for galette
59 * @abstract Class for expanding TCPDF.
60 * @author John Perr <johnperr@abul.org>
61 * @author Johan Cwiklinski <johan@x-tnd.be>
62 * @copyright 2007-2014 The Galette Team
63 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
64 * @link http://galette.tuxfamily.org
65 * @since Available since 0.7dev - 2007-07-21
68 class Pdf
extends \TCPDF
71 const FONT
= 'DejaVuSans';
74 protected $preferences;
77 private $paginated = false;
81 * Main constructor, set creator and author
83 * @param Preferences $prefs Preferences
84 * @param PdfModel $model Related model
86 public function __construct(Preferences
$prefs, $model = null)
90 $this->preferences
= $prefs;
92 parent
::__construct('P', 'mm', 'A4', true, 'UTF-8');
94 $this->SetCreator(PDF_CREATOR
);
95 //add helvetica, hard-called from lib
96 $this->SetFont('helvetica');
97 //and then, set real font
98 $this->SetFont(self
::FONT
, '', self
::FONT_SIZE
);
101 $this->preferences
->pref_nom
,
105 $name . ' (using Galette ' . GALETTE_VERSION
. ')'
108 if ($this->i18n
->isRTL()) {
112 if ($model !== null) {
113 if ($model instanceof PdfModel
) {
114 $this->model
= $model;
115 $this->SetTitle($this->model
->htitle
);
117 throw new \
UnexpectedValueException(
118 'Provided model must be an instance of PdfModel!'
125 * Set show pagination
129 public function showPagination()
131 $this->paginated
= true;
137 public function __destruct()
139 parent
::__destruct();
143 * This method is automatically called in case of fatal error;
144 * it simply outputs the message and halts the execution.
145 * An inherited class may override it to customize the error
146 * handling but should always halt the script, or the resulting
147 * document would probably be invalid.
148 * 2004-06-11 :: Nicola Asuni : changed bold tag with strong
149 * 2007-07-21 :: John Perr : changed function to return error to session
150 * 2017-02-14 :: Johan Cwiklinski : use slim's flash message; do not rely on session for redirect
152 * @param string $msg The error message
158 public function Error($msg) // phpcs:ignore PSR1.Methods.CamelCapsMethodName
167 $container->get('flash')->addMessage(
172 $redirect = (isset($_SERVER['HTTP_REFERER']) ?
173 $_SERVER['HTTP_REFERER'] :
174 $container->get('router')->pathFor('slash'));
175 header('Location: ' . $redirect);
180 * Converts color from HTML format #RRVVBB
181 * to RGB 3 colors array.
183 * @param string $hex6 7 chars string #RRVVBB
187 public function colorHex2Dec($hex6)
190 "R" => hexdec(substr($hex6, 1, 2)),
191 "G" => hexdec(substr($hex6, 3, 2)),
192 "B" => hexdec(substr($hex6, 5, 2))
197 /** FIXME: is this function used somewhere? */
199 * Extract info from a GIF file
200 * (In fact: converts gif image to png and feeds it to _parsepng)
202 * @param string $file path to the gif file
207 protected function parsegif($file)
209 $a=GetImageSize($file);
211 $this->Error(_T("Missing or incorrect image file ") . $file);
214 $this->Error(_T("Not a GIF file ") . $file);
217 // Tentative d'ouverture du fichier
218 if (function_exists('gd_info')) {
219 $data = @imagecreatefromgif
($file);
221 // Test d'échec & Affichage d'un message d'erreur
223 $this->Error(_T("Error loading ").$file);
225 if (imagepng($data, GALETTE_ROOT
. 'tempimages/gif2png.png')) {
226 return $this->_parsepng(GALETTE_ROOT
. 'tempimages/gif2png.png');
228 $this->Error(_T("Error creating temporary png file from ").$file);
231 $this->Error(_T("Unable to convert GIF file ").$file);
236 * Draws PDF page Header
240 public function Header() // phpcs:ignore PSR1.Methods.CamelCapsMethodName
242 //just ovverride default header to prevent black line at top
246 * Draws PDF page footer
250 public function Footer() // phpcs:ignore PSR1.Methods.CamelCapsMethodName
253 if (isset($this->model
)) {
255 if (trim($this->model
->hstyles
) !== '') {
256 $hfooter .= "<style>\n" . $this->model
->hstyles
. "\n</style>\n\n";
258 $hfooter .= $this->model
->hfooter
;
259 $this->writeHtml($hfooter);
261 $this->SetFont(self
::FONT
, '', self
::FONT_SIZE
- 2);
262 $this->SetTextColor(0, 0, 0);
264 $name = preg_replace(
266 $this->preferences
->pref_nom
,
270 $address = $this->preferences
->getPostalAddress();
280 if ($this->paginated
) {
281 $this->SetFont(self
::FONT
, '', self
::FONT_SIZE
- 3);
286 $this->getAliasRightShift() . $this->PageNo() .
287 '/' . $this->getAliasNbPages(),
290 ($this->i18n
->isRTL() ?
'L' : 'R')
297 * Draws PDF page header
299 * @param string $title Additionnal title to display just after logo
303 public function PageHeader($title = null) // phpcs:ignore PSR1.Methods.CamelCapsMethodName
305 if (isset($this->model
)) {
307 if (trim($this->model
->hstyles
) !== '') {
308 $html .= "<style>\n" . $this->model
->hstyles
. "\n</style>\n\n";
310 $html .= "<div dir=\"".($this->i18n
->isRTL() ?
'rtl' : 'ltr')."\">" . $this->model
->hheader
. "</div>";
311 $this->writeHtml($html, true, false, true, false, '');
313 if ($title !== null) {
314 $this->writeHtml('<h2 style="text-align:center;">' . $title . '</h2>');
317 if (trim($this->model
->title
) !== '') {
319 if (trim($this->model
->hstyles
) !== '') {
320 $htitle .= "<style>\n" . $this->model
->hstyles
.
323 $htitle .= '<div id="pdf_title">' . $this->model
->htitle
. '</div>';
324 $this->writeHtml($htitle);
326 if (trim($this->model
->subtitle
) !== '') {
328 if (trim($this->model
->hstyles
) !== '') {
329 $hsubtitle .= "<style>\n" . $this->model
->hstyles
.
332 $hsubtitle .= '<div id="pdf_subtitle">' . $this->model
->hsubtitle
.
334 $this->writeHtml($hsubtitle);
336 if (trim($this->model
->title
) !== ''
337 ||
trim($this->model
->subtitle
) !== ''
343 $print_logo = new \Galette\Core\
PrintLogo();
344 $logofile = $print_logo->getPath();
346 // Set logo size to max width 30 mm or max height 25 mm
347 $ratio = $print_logo->getWidth()/$print_logo->getHeight();
349 if ($print_logo->getHeight() > 16) {
352 $hlogo = $print_logo->getHeight();
354 $wlogo = round($hlogo*$ratio);
356 if ($print_logo->getWidth() > 16) {
359 $wlogo = $print_logo->getWidth();
361 $hlogo = round($wlogo/$ratio);
364 $this->SetFont(self
::FONT
, 'B', self
::FONT_SIZE +
4);
365 $this->SetTextColor(0, 0, 0);
369 $ystart = $this->GetY();
374 $this->preferences
->pref_nom
,
376 ($this->i18n
->isRTL() ?
'R' : 'L')
378 $this->SetFont(self
::FONT
, 'B', self
::FONT_SIZE +
2);
380 if ($title !== null) {
381 $this->Cell(0, 6, $title, 0, 1, ($this->i18n
->isRTL() ?
'R' : 'L'), 0);
383 $yend = $this->getY();//store position at the end of the text
385 $this->SetY($ystart);
386 if ($this->i18n
->isRTL()) {
389 $x = 190 - $wlogo; //right align
391 $this->Image($logofile, $x, $this->GetY(), $wlogo, $hlogo);
392 $this->y +
= $hlogo +
3;
393 //if position after logo is < than position after text,
394 //we have to change y
395 if ($this->getY() < $yend) {
402 * Draws body from model
406 public function PageBody() // phpcs:ignore PSR1.Methods.CamelCapsMethodName
409 if (trim($this->model
->hstyles
) !== '') {
410 $hbody .= "<style>\n" . $this->model
->hstyles
. "\n</style>\n\n";
412 $hbody .= $this->model
->hbody
;
413 $this->writeHtml($hbody);
419 * @param string $text Text content
420 * @param integer $maxsize Maximal size
421 * @param integer $fontsize Font size
422 * @param string $fontstyle Font style (defaults to '')
423 * @param string $fontname Font name (defaults to static::FONT)
427 protected function fixSize($text, $maxsize, $fontsize, $fontstyle = '', $fontname = null)
429 if ($fontname === null) {
430 $fontname = static::FONT
;
432 $this->SetFontSize($fontsize);
433 while ($this->GetStringWidth($text, $fontname, $fontstyle, $fontsize) > $maxsize) {
435 $this->SetFontSize($fontsize);
442 * @param string $str Original string
443 * @param integer $length Max length
447 protected function cut($str, $length)
449 $length = $length -2; //keep a margin
450 if ($this->GetStringWidth($str) > $length) {
451 while ($this->GetStringWidth($str . '...') > $length) {
452 $str = mb_substr($str, 0, -1, 'UTF-8');
460 * Stretch a header string
462 * @param string $str Original string
463 * @param integer $length Max length
467 protected function stretchHead($str, $length)
469 $this->SetFont(self
::FONT
, 'B', self
::FONT_SIZE
);
471 if ($this->GetStringWidth($str) > $length) {
472 while ($this->GetStringWidth($str) > $length) {
473 $this->setFontStretching(--$stretch);
484 public function getFilename()
486 return $this->filename
;
490 * Download PDF from browser
494 public function download()
496 $this->Output($this->filename
, 'D');