Skip to content
PHP Logo

How can PHP be used to add numbering to PDF documents and display the number on the document?

To add numbering to PDF documents using PHP, you can utilize a library like TCPDF or FPDF. These libraries allow you to generate PDF documents programmatically and add various elements, including text with numbering. You can create a loop to iterate through the pages of the PDF document and add the desired numbering to each page.

php
require_once('tcpdf/tcpdf.php');

$pdf = new TCPDF();
$pdf->AddPage();

$number = 1;
$total_pages = $pdf->getNumPages();

for ($i = 1; $i <= $total_pages; $i++) {
    $pdf->setPage($i);
    $pdf->SetFont('helvetica', '', 12);
    $pdf->Text(10, 10, 'Page ' . $number);
    $number++;
}

$pdf->Output('numbered_document.pdf', 'I');