]> git.agnieray.net Git - galette.git/blobdiff - galette/lib/Galette/IO/Pdf.php
Rework PDF footer so any height will be ok
[galette.git] / galette / lib / Galette / IO / Pdf.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')
-                );
-            }
         }
     }