- October 2, 2019
- Posted by: Editor
- Categories: PHP, Trending
Updated April 4th, 2023 with example for adding text to multiple pages or specific pages
This article describes how to add text to an existing PDF. If you want to create a PDF from HTML and not just add text to an existing one, check out this article instead.
Here’s a dead simple way to overlay text to a PDF faster than you can make a cup of coffee. First grab a copy of FPDI using composer or from github
If you’re using composer, add the following to your composer.json file
{
"require": {
"setasign/fpdf": "1.8.*",
"setasign/fpdi": "^2.0"
}
}
Then run the following to install the required files.
composer install --no-dev
With PHP if using composer you’ll need include this file using the autoload method:
require __DIR__ . '/vendor/autoload.php';
If downloading manually, grab a copy of FPDF and add the necessary statements to import the classes into your project folder e.g.
require_once(__DIR__.'\fpdf\fpdf.php');
require_once(__DIR__.'\fpdi\autoload.php');
To set our original PDF source we need to specify the path to the file. For this example, we’ll add and use the PDF “template.pdf” inside our project folder.
$pdf->setSourceFile("template.pdf");
Now we initialize the FPDI class, add the X,Y coordinate where we want to add the text and boom, we’re done. Here’s the full code below if you installed the libraries using composer.
require __DIR__ . '/vendor/autoload.php';
use setasign\Fpdi\Fpdi;
// initiate FPDI
$pdf = new Fpdi();
$pdf->AddPage();
$pdf->setSourceFile("template.pdf");
// import page 1
$tplId = $pdf->importPage(1);
// use the imported page and place it at point 5,5 with your preferred width in mm
$pdf->useTemplate($tplId, 5, 5, 210);
$pdf->SetFont('Arial','',10); // Font Name, Font Style (eg. 'B' for Bold), Font Size
$pdf->SetTextColor(0,0,0); // RGB
$pdf->SetXY(65, 30); // X start, Y start in mm
$text = "Hello World!";
$pdf->Write(0, $text);
$pdf->Output();
But how do we add text to a PDF with multiple pages?
To do that we would have to iterate through all the pages, and then we can conditionally add text to the page that matches the page number that we are looking for. Here’s a quick example below.
<?php
require __DIR__ . '/vendor/autoload.php';
use setasign\Fpdi\Fpdi;
// initiate FPDI
$pdf = new FPDI();
// get the page count
$pageCount = $pdf->setSourceFile('book.pdf');
// iterate through all pages
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
// import a page
$templateId = $pdf->importPage($pageNo);
// get the size of the imported page
$size = $pdf->getTemplateSize($templateId);
$pdf->AddPage($size['orientation'], array($size['width'], $size['height']));
// use the imported page
$pdf->useTemplate($templateId);
$pdf->SetFont('Helvetica');
$pdf->SetXY(5, 5);
$pdf->Write(8, 'Some new text');
//Add some text to only a specific page
if($pageNo == 2) {
$pdf->SetXY(5, 65);
$pdf->Write(8, 'This text only on page two');
}
}
// Output the new PDF
$pdf->Output();
?>
Now you can add additional text wherever you want on the page by setting a coordinate and writing new text. Happy coding!
Having issues? Download the example code below as a starting point.
I have a PDF book that I want to sell in my site. I want that when someone purchase the book – his/her email address will be added to the last page of the book, for security.
Can you code help with that?
Yes, with a slight modification. We would have to iterate through all the pages then add text only to the page number where we want the text to show up. I updated the article to provide an example for that scenario.