use Galette\Entity\PdfModel;
use Analog\Analog;
use Slim\Routing\RouteParser;
+use TCPDF;
/*
* TCPDF configuration file for Galette
* @author Johan Cwiklinski <johan@x-tnd.be>
*/
-class Pdf extends \TCPDF
+class Pdf extends TCPDF
{
public const FONT = 'DejaVuSans';
public const FONT_SIZE = 10;
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
$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;
}
/**
/**
* 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')
- );
- }
}
}