]> git.agnieray.net Git - galette.git/commitdiff
Rework PDF footer so any height will be ok
authorJohan Cwiklinski <johan@x-tnd.be>
Tue, 2 Apr 2024 15:19:01 +0000 (17:19 +0200)
committerJohan Cwiklinski <johan@x-tnd.be>
Thu, 4 Apr 2024 05:24:17 +0000 (07:24 +0200)
Enable pagination on attendance sheet; closes #1816

galette/lib/Galette/IO/Pdf.php
galette/lib/Galette/IO/PdfAttendanceSheet.php
galette/lib/Galette/IO/PdfContribution.php
galette/lib/Galette/IO/PdfGroups.php
galette/lib/Galette/IO/PdfMembersCards.php
galette/lib/Galette/IO/PdfMembersLabels.php

index d06e6bb1501204fa579cfb1d43d2f1f9061efdeb..75510523077651b57cbf4668992ce184e9f66c36 100644 (file)
@@ -26,6 +26,7 @@ use Galette\Core\Preferences;
 use Galette\Entity\PdfModel;
 use Analog\Analog;
 use Slim\Routing\RouteParser;
+use TCPDF;
 
 /*
  * TCPDF configuration file for Galette
@@ -39,7 +40,7 @@ require_once GALETTE_CONFIG_PATH . 'galette_tcpdf_config.php';
  * @author Johan Cwiklinski <johan@x-tnd.be>
  */
 
-class Pdf extends \TCPDF
+class Pdf extends TCPDF
 {
     public const FONT = 'DejaVuSans';
     public const FONT_SIZE = 10;
@@ -49,6 +50,8 @@ class Pdf extends \TCPDF
     private PdfModel $model;
     private bool $paginated = false;
     protected string $filename;
+    private bool $has_footer = true;
+    protected float $footer_height;
 
     /**
      * Main constructor, set creator and author
@@ -86,6 +89,59 @@ class Pdf extends \TCPDF
             $this->model = $model;
             $this->SetTitle($this->model->htitle);
         }
+
+        $this->init();
+        if ($this->has_footer) {
+            $this->calculateFooterHeight();
+        }
+    }
+
+    /**
+     * Initialize PDF
+     *
+     * @return void
+     */
+    public function init(): void
+    {
+        $this->Open();
+        $this->AddPage();
+    }
+
+    /**
+     * No header
+     *
+     * @return void
+     */
+    protected function setNoHeader(): void
+    {
+        $this->SetPrintHeader(false);
+        $this->setHeaderMargin(0);
+    }
+
+    /**
+     * No footer
+     *
+     * @return void
+     */
+    protected function setNoFooter(): void
+    {
+        $this->SetPrintFooter(false);
+        $this->setFooterMargin(0);
+        $this->has_footer = false;
+    }
+
+    /**
+     * Calculate footer height
+     *
+     * @return void
+     */
+    private function calculateFooterHeight(): void
+    {
+        $pdf = clone $this;
+        $y_orig = $pdf->getY();
+        $this->Footer($pdf);
+        $y_end = $pdf->getY();
+        $this->footer_height = $y_end - $y_orig;
     }
 
     /**
@@ -173,51 +229,42 @@ class Pdf extends \TCPDF
     /**
      * Draws PDF page footer
      *
+     * @param ?TCPDF $pdf PDF instance
+     *
      * @return void
      */
-    public function Footer(): void // phpcs:ignore PSR1.Methods.CamelCapsMethodName
+    public function Footer(TCPDF $pdf = null): void // phpcs:ignore PSR1.Methods.CamelCapsMethodName
     {
-        $this->SetY(-20);
+        if ($pdf === null) {
+            $pdf = $this;
+            $pdf->SetY(-($this->footer_height + 15));
+        }
         if (isset($this->model)) {
             $hfooter = '';
             if (trim($this->model->hstyles) !== '') {
                 $hfooter .= "<style>\n" . $this->model->hstyles . "\n</style>\n\n";
             }
             $hfooter .= $this->model->hfooter;
-            $this->writeHtml($hfooter);
+            $pdf->writeHtml($hfooter);
         } else {
-            $this->SetFont(self::FONT, '', self::FONT_SIZE - 2);
-            $this->SetTextColor(0, 0, 0);
-
-            $name = preg_replace(
-                '/%s/',
-                $this->preferences->pref_nom,
-                _T("Association %s")
-            );
-
             $address = $this->preferences->getPostalAddress();
+            $hfooter = '<style>div#pdf_footer {text-align: center;font-size: 0.7em;}</style>';
+            $hfooter .= '<div id="pdf_footer">' . nl2br($address) . '</div>';
+            $pdf->writeHTML($hfooter);
+        }
 
-            $this->MultiCell(
+        if ($this->paginated) {
+            $pdf->SetFont(self::FONT, '', self::FONT_SIZE - 3);
+            $pdf->Ln();
+            $pdf->Cell(
                 0,
                 4,
-                $address,
+                $this->getAliasRightShift() . $this->PageNo() .
+                '/' . $this->getAliasNbPages(),
                 0,
-                'C'
+                1,
+                ($this->i18n->isRTL() ? 'L' : 'R')
             );
-
-            if ($this->paginated) {
-                $this->SetFont(self::FONT, '', self::FONT_SIZE - 3);
-                $this->Ln();
-                $this->Cell(
-                    0,
-                    4,
-                    $this->getAliasRightShift() . $this->PageNo() .
-                    '/' . $this->getAliasNbPages(),
-                    0,
-                    1,
-                    ($this->i18n->isRTL() ? 'L' : 'R')
-                );
-            }
         }
     }
 
index bbd3c5fe40e4ddcb3526007db30e282113adba61..61e7dae84ddf11d9c81871f86110bd014d8e302a 100644 (file)
@@ -23,10 +23,8 @@ namespace Galette\IO;
 
 use Galette\Core\Db;
 use Galette\Core\Preferences;
-use Galette\Core\PrintLogo;
 use Galette\Entity\Adherent;
 use Galette\Entity\PdfModel;
-use Analog\Analog;
 
 /**
  * Attendance sheet
@@ -106,26 +104,32 @@ class PdfAttendanceSheet extends Pdf
         }
 
         parent::__construct($prefs, $model);
-        $this->init();
+        // Enable Auto Page breaks
+        $this->SetAutoPageBreak(true, $this->footer_height + 10);
     }
+
     /**
      * Initialize PDF
      *
      * @return void
      */
-    private function init(): void
+    public function init(): void
     {
         // Set document information
         $this->SetSubject(_T("Generated by Galette"));
         $this->SetKeywords(_T("Attendance sheet"));
 
-        // No hearders and footers
         $this->setHeaderMargin(10);
 
         // Set colors
         $this->SetDrawColor(160, 160, 160);
         $this->SetTextColor(0);
         $this->SetFont(Pdf::FONT, '', Pdf::FONT_SIZE - 2);
+
+        //enable pagination
+        $this->showPagination();
+
+        parent::init();
     }
 
     /**
@@ -137,8 +141,6 @@ class PdfAttendanceSheet extends Pdf
      */
     public function drawSheet(array $members): void
     {
-        $this->Open();
-        $this->AddPage();
         $this->PageHeader($this->doc_title);
 
         if ($this->sheet_date) {
index a807ce96f0f1ee8a43f247347577a6b019d2544b..855f8c297827fbf82a6f7b764646db393cbb3d13 100644 (file)
@@ -71,9 +71,6 @@ class PdfContribution extends Pdf
 
         parent::__construct($prefs, $this->model);
 
-        $this->Open();
-
-        $this->AddPage();
         $this->PageHeader();
         $this->PageBody();
     }
index 195e2b2825af9cefc72aebd53917a4849b505baf..a66a227c97fd0c03afb67fd726f8c5ab81a717f3 100644 (file)
@@ -22,7 +22,6 @@
 namespace Galette\IO;
 
 use Galette\Core\Preferences;
-use Galette\Core\PrintLogo;
 use Analog\Analog;
 use Galette\Core\Login;
 use Galette\Entity\Group;
@@ -46,9 +45,10 @@ class PdfGroups extends Pdf
      */
     public function __construct(Preferences $prefs)
     {
-        parent::__construct($prefs);
         $this->filename = __('groups_list') . '.pdf';
-        $this->init();
+        parent::__construct($prefs);
+        // Enable Auto Page breaks
+        $this->SetAutoPageBreak(true, $this->footer_height + 10);
     }
 
     /**
@@ -79,21 +79,18 @@ class PdfGroups extends Pdf
      *
      * @return void
      */
-    private function init(): void
+    public function init(): void
     {
         // Set document information
         $this->doc_title = _T("Members by groups");
         $this->SetTitle($this->doc_title);
         $this->SetSubject(_T("Generated by Galette"));
 
-        // Enable Auto Page breaks
-        $this->SetAutoPageBreak(true, 20);
-
         // Set colors
         $this->SetTextColor(0, 0, 0);
 
         // Set margins
-        $this->setMargins(10, 20);
+        $this->setMargins(10, 25);
         $this->setHeaderMargin(10);
 
         // Set font
@@ -101,6 +98,8 @@ class PdfGroups extends Pdf
 
         //enable pagination
         $this->showPagination();
+
+        parent::init();
     }
 
     /**
@@ -113,8 +112,6 @@ class PdfGroups extends Pdf
      */
     public function draw(array $groups, Login $login): void
     {
-        $this->Open();
-        $this->AddPage();
         $this->PageHeader($this->doc_title);
 
         $first = true;
@@ -159,7 +156,7 @@ class PdfGroups extends Pdf
             $this->Cell(30, 7, _T("Phone"), 1, 0, 'C', true);
             $this->Cell(30, 7, _T("GSM"), 1, 1, 'C', true);
 
-            $this->SetFont('', 'B');
+            $this->SetFont('', '');
 
             $members = $group->getMembers();
 
index b733a520e5f92cb3b927df851ff53e1f0d3242de..af37a141286492f977b142272a2b9bbf49b67f72 100644 (file)
@@ -72,28 +72,26 @@ class PdfMembersCards extends Pdf
      */
     public function __construct(Preferences $prefs)
     {
-        parent::__construct($prefs);
         $this->setRTL(false);
         $this->filename = __('cards') . '.pdf';
-        $this->init();
+        parent::__construct($prefs);
     }
+
     /**
      * Initialize PDF
      *
      * @return void
      */
-    private function init(): void
+    public function init(): void
     {
         // Set document information
         $this->SetTitle(_T("Member's Cards"));
         $this->SetSubject(_T("Generated by Galette"));
         $this->SetKeywords(_T("Cards"));
 
-        // No hearders and footers
-        $this->SetPrintHeader(false);
-        $this->SetPrintFooter(false);
-        $this->setFooterMargin(0);
-        $this->setHeaderMargin(0);
+        // No headers and footers
+        $this->setNoHeader();
+        $this->setNoFooter();
 
         // Show full page
         $this->SetDisplayMode('fullpage');
@@ -125,7 +123,7 @@ class PdfMembersCards extends Pdf
 
         // Card width
         $this->wi = self::getWidth();
-        // Card heigth
+        // Card height
         $this->he = self::getHeight();
         // Number of colons
         $this->nbcol = self::getCols();
index f668977908985d6aa0b0fbc782071d1f974f6386..b7e7ed588671dcb129120e17b8ef11b0813ac32e 100644 (file)
@@ -46,10 +46,9 @@ class PdfMembersLabels extends Pdf
      */
     public function __construct(Preferences $prefs)
     {
-        parent::__construct($prefs);
         $this->setRTL(false);
         $this->filename = __('labels_print_filename') . '.pdf';
-        $this->init();
+        parent::__construct($prefs);
     }
 
     /**
@@ -57,18 +56,16 @@ class PdfMembersLabels extends Pdf
      *
      * @return void
      */
-    private function init(): void
+    public function init(): void
     {
         // Set document information
         $this->SetTitle(_T("Member's Labels"));
         $this->SetSubject(_T("Generated by Galette"));
         $this->SetKeywords(_T("Labels"));
 
-        // No hearders and footers
-        $this->SetPrintHeader(false);
-        $this->SetPrintFooter(false);
-        $this->setFooterMargin(0);
-        $this->setHeaderMargin(0);
+        // No headers and footers
+        $this->setNoHeader();
+        $this->setNoFooter();
 
         // Show full page
         $this->SetDisplayMode('fullpage');