tecnickcom / TCPDF

Official clone of PHP library to generate PDF documents and barcodes
https://tcpdf.org
Other
4.18k stars 1.51k forks source link

Example for split footer (or I'm overlooking it) #612

Open stgoos opened 1 year ago

stgoos commented 1 year ago

I started with TCPDF today and thanks for creating and maintaining it! Getting the hang of it via the examples works but I'm kind of missing proper documentation to see all the options and the parameters more easily as an overview. https://tcpdf.org/docs/ only shows a searchbar and an ads area.

Anyways, while working on my little PDF export project I ran into the question of how to create a split footer with on the leftside the ability to insert custom text and on the rightside the page numbering.

I reckon it would be valuable to make an example for it as well. If there is already an example containing it, then I've overlooked it.

With help of Google I landed myself on:

// Extend TCPDF with custom Footer() method
class MYPDF extends TCPDF {
    // Custom footer content
    private $footer_text = '';

    // Set custom footer text
    public function setFooterText($text) {
        $this->footer_text = $text;
    }

    // Page footer
    public function Footer() {
        // Set font
        $this->SetFont('helvetica', '', 8);

        // Position at 15 mm from bottom
        $this->SetY(-15);

        // Enable footer line
        $this->setDrawColor(192,192,192);
        $this->setLineWidth(0.1);
        $this->Line($this->GetX(), $this->GetY(), $this->GetX() + $this->getPageWidth() - $this->getMargins()['left'] - $this->getMargins()['right'], $this->GetY());

        // Set left footer content
        $this->Cell(0, 10, $this->footer_text, 0, false, 'L', 0, '', 0, false, 'T', 'M');

        // Set right footer content
        $this->Cell(0, 10, 'page ' . $this->getAliasNumPage() . '/' . $this->getAliasNbPages(), 0, false, 'R', 0, '', 0, false, 'T', 'M');
    }
}

$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->setFooterText("Left side of the footer area");

However, there is still 1 open point for me: how do I align the page numbering text (in the cell on the rightside of the footer) to the right of the cell?