<?php
if (!defined('ROOT_PATH')) {
    header('HTTP/1.1 403 Forbidden');
    exit;
}

/**
 * TCPDF-subclass for invoice PDFs with a custom Footer() override.
 * Places the company footer at the bottom of each page, regardless of body length.
 *
 * Use:
 *   $pdf = new pjInvoicePdf(...);
 *   $pdf->setInvoiceFooterHtml($footerHtml);
 *   $pdf->setPrintFooter(true);
 *   $pdf->setFooterMargin(15);
 *   $pdf->SetAutoPageBreak(true, 28);
 *   $pdf->AddPage();
 *   $pdf->writeHTML($body);
 *   $pdf->Output($filename, 'S');
 */
class pjInvoicePdf extends TCPDF
{
    /** @var string HTML of the footer */
    protected $invoiceFooterHtml = '';

    /**
     * Set the footer HTML (rendered at the bottom of each page).
     */
    public function setInvoiceFooterHtml($html)
    {
        $this->invoiceFooterHtml = (string) $html;
        return $this;
    }

    /**
     * Override of TCPDF::Footer() — called by TCPDF
     * for each new page if setPrintFooter(true) is set.
     */
    public function Footer()
    {
        if (empty($this->invoiceFooterHtml)) {
            return;
        }
        // Place footer ~22mm above the bottom edge
        $this->SetY(-22);
        $this->writeHTMLCell(0, 0, '', '', $this->invoiceFooterHtml, 0, 0, 0, true, '', true);
    }
}
