Files
PhpSpreadsheet/samples/Pdf/Tcpdf_Custom_Headers.php
oleibman 15fbc9a9d9 Headers and Footers in Pdf
Fix #3159, at least as far as we can. Requester would probably prefer that we automatically translate the PhpSpreadsheet header and footer information to forms that the Pdf vendors can handle. That seems like it would be very difficult. On the other hand, we can add some new properties to the Pdf writers to make it *relatively* easy for the user to define their own headers and footers for the writers.

Mpdf, as mentioned in the issue, can already handle this in conjunction with the `editHtmlCallback` property. This is demonstraded in Sample21c, which is now renamed to something more descriptive, `Mpdf_Custom_Headers`. That is the only change needed for Mpdf.

Tcpdf, which currently automatically prevents any footers or headers, needs some new properties to allow them. Even with the new properties, a user would need to extend both PhpSpreadsheet's Tcpdf Writer, and the vendor's own class (extending that class is actually the vendor's recommendation). A new sample `Tcpdf_Custom_Headers` demonstrates how this would be done.

Dompdf has 2 ways to add headers and footers. One is pretty good at styling, but doesn't offer much flexibility for dynamic properties other than the current page number; it can't handle different odd/even/first headers, nor the number of pages in the document. The second makes the dynamic properties possible, but seems difficult to style. Two new samples `Dompdf_Custom_Headers` (using `editHtmlCallback`) and `Dompdf_Canvas_Headers` (using a new protected method `callPageScript`) illustrate the use of each.

None of the samples demonstrates the full capabilities of what can go into a header or footer. I consider that out of scope; enabling the ability is one thing, perfecting it is quite another.
2026-01-26 21:08:46 -08:00

76 lines
2.4 KiB
PHP

<?php
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Pdf\TcpdfNoDie;
use TCPDF as VendorTcpdf;
require __DIR__ . '/../Header.php';
// Override PhpSpreadsheet class.
class Tcpdf2 extends TcpdfNoDie // phpcs:ignore
{
protected bool $writeHeader = true;
protected bool $writeFooter = true;
protected function createExternalWriterInstance(string $orientation, string $unit, $paperSize): VendorTcpdf
{
$this->defines();
return new Tcpdf2Class($orientation, $unit, $paperSize);
}
}
// Override vendor class.
class Tcpdf2Class extends VendorTcpdf // phpcs:ignore
{
// Page header
public function Header(): void // phpcs:ignore
{
// Position at 15 mm from top
$this->setY(15);
// Set font
$this->setFont('helvetica', 'B', 12);
// Title
$pageNum = $this->getPage();
if ($pageNum === 1) {
$this->Cell(0, 15, 'Tcpdf first header', 0, 0, 'C', false, '', 0, false, 'M', 'M');
} elseif ($pageNum % 2 === 0) {
$this->Cell(0, 15, 'Tcpdf even header', 0, 0, 'C', false, '', 0, false, 'M', 'M');
} else {
$this->Cell(0, 15, 'Tcpdf odd header', 0, 0, 'C', false, '', 0, false, 'M', 'M');
}
}
// Page footer
public function Footer(): void // phpcs:ignore
{
// Position at 15 mm from bottom
$this->setY(-15);
// Set font
$this->setFont('helvetica', 'I', 8);
// Page number
$this->Cell(0, 10, 'Page ' . $this->getAliasNumPage() . '/' . $this->getAliasNbPages(), 0, 0, 'C', false, '', 0, false, 'T', 'M');
}
}
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$counter = 0;
/** @var PhpOffice\PhpSpreadsheet\Helper\Sample $helper */
$helper->log('Populate spreadsheet');
for ($row = 1; $row < 1001; ++$row) {
$sheet->getCell("A$row")->setValue(++$counter);
// Add many styles by using slight variations of font color for each.
$sheet->getCell("A$row")->getStyle()->getFont()
->getColor()->setRgb(sprintf('%06x', $counter));
$sheet->getCell("B$row")->setValue(++$counter);
$sheet->getCell("C$row")->setValue(++$counter);
}
$helper->log('Write to Tcpdf with headers and footers');
IOFactory::registerWriter('Pdf', Tcpdf2::class);
$helper->write($spreadsheet, __FILE__, ['Pdf']);
$spreadsheet->disconnectWorksheets();