In a recent project, I was asked to create a dynamically generated PDF with the user’s information. In my research, I found that using FPDF was the best the best way to achieve the client’s requirements. Here is a quick tutorial on how you can use a existing PDF with dynamic content using only PHP. This particular example uses composer and less than 50 lines of code.
Installation
Start by installing the following composer packages:
composer require setasign/fpdf setasign/fpdi-fpdf
These two libraries are all you need to edit PDFs.
The Code
Before creating the code, you first need to to find a existing PDF to use. In this tutorial, I will be using the following template. Upload the file you want to use within your project folder so you can reference it in your code.
As for the code, this is what I used:
See the comments within the code to see what’s going on – most of it’s pretty self-explanatory, thanks to the straight-forward function names.
As the PDF I’m using has space for five bits of text, I’ve created 5 cells within the PDF using PHP. To help align these cells, the third parameter is set to 1
, this means a border has been added to see where the cell appears. When you are happy with the placement (using SetXY
) and size (using the first two parameters in Cell
), you can set this to 0
.
For example, the below line sets the X
coordinates to 10
, and Y
coordinates to 89
. This is the position with in the PDF where I want the cell to appear.
$pdf->SetXY(10, 89);
Then, I create a cell with:
- 100% width (first parameter =
0
) - 10 height (second parameter =
10
) - Text is set to
Niraj Shah
(third parameter) - 1px border (forth parameter =
1
) - no fill (fifth parameter =
0
. Use1
for fill) - center alignment (last parameter =
C
. Can also useL
andR
).
$pdf->Cell(0, 10, 'Niraj Shah', 1, 0, 'C');
Then, to remove the border, set the third parameter to 0
:
$pdf->Cell(0, 10, 'Niraj Shah', 0, 0, 'C');
Or to change the alignment of the text, change the last parameter to either L
, R
or C
:
$pdf->Cell(0, 10, 'Niraj Shah', 1, 0, 'L');
Example
With the above code and PDF template, my final output (with borders enabled) results in:
Please note that white border has been removed in screenshot.
Final Result
After updating the code to remove the borders, the final PDF looks like:
The user can then choose to download the PDF or print it using their browser. You can see the downloaded version here.
Source Code
If you would like to see the entire source code I used, along with example PDF and composer settings, you can download the source code ZIP here.
If you have any issues, comments, suggestions, etc., feel free to leave a comment below.
But can you edit it? How?
This tutorial shows how you can edit a PDF by adding additional content to an existing PDF. I haven’t covered how to edit text within a PDF (and haven’t looked into if this is possible yet).
Then change the title to Creating / Adding text to a PDF. I’m looking for edit existing content using php.
Hi .. Thanks for the tutorial ..
When using transparent images.. How to solve the problem “FPDF error: Interlacing not supported”?
I haven’t come across this error myself. Try StackOverflow in case someone has a fix.
where is “vendor/autoload.php” and “fpdi” file ?? cant run errors showing
As mentioned in the Installation section, the
setasign/fpdf
andsetasign/fpdi-fpdf
packages are installed using Composer. Please read the article again.but after using composer also it is giving Uncaught Error: Class ‘FPDI’ not found in C:\xampp\htdocs\pdf\pdf.php
Sounds like you are not using composer correctly. Please read it’s documentation to see what you are doing wrong.
Thank you, Niraj, for this info. I was able to use it today to meet a client deliverable that I was sweating.
I used it with Drupal 8.3.9 on PHP 5.6 with no problem.
Hey, thanks for this tutorial. I was looking everywhere for a way to edit existing pdfs. I hope I can configure composer the right way to make this work. Thanks for everything.
You’re welcome! Let me know how you get on.
Hi. Thanks for this. I am plugging in data from a form, and i was hoping to enter blocks of text. There seems to be no option for word wrap?
I have tried using wordwrap with no luck
http://php.net/manual/en/function.wordwrap.php
Unfortunately the
wordwrap
function cannot be used to wrap the text in the PDF. You would need to create new cells using PHP (for each line), and position these below each other, and then insert the text into each block. New line characters (\n
,PHP_EOL
, etc.) don’t work in the PDF.Hi ,
I want to ask that is there any possibility to edit text in pdf file through laravel
I haven’t looked into editing exiting text in a PDF, but you will probably need to convert the file to a different format before you can edit it. You should investigate the PDF libraries mentioned in this post as they might have editing functionality.
hey sir thanks , thats what i was searching for but i am facing one problem here that is there any way to pas multiple pages of pdf in $pdf->importPage(1); because it generates error on passing array of pages like $pdf->importPage([‘1′,’2′,’3’,]);
If you want to import multiple pages, you need to call the code multiple times:
I followed your instruction. But I got this error
Fatal error: Uncaught Error: Class ‘FPDI’ not found in D:\xampp\htdocs\certificate\index.php:7 Stack trace: #0 {main} thrown in D:\xampp\htdocs\certificate\index.php on line 7
Would you pleased to tell me what the problem is. Thank you
You haven’t included the FPDI class in your code. If you are using composer, make sure you are including the autoload file in your code. Please check that you’ve run through the initial steps in the article.
Hi Niraj,
Thanks for your input The problem is solved after I change composer script
// include composer packages
include “vendor/autoload.php”;
with
require_once(‘vendor/setasign/fpdf/fpdf.php’);
require_once(‘vendor/setasign/fpdi/src/autoload.php’);
use \setasign\Fpdi\Fpdi;
replaced $pdf = new FPDI(‘l’); with $pdf = new \setasign\Fpdi\Fpdi(‘l’);
how to get exact XY parameters for any given pdf?
It will be trial and error – using the border will help you position the box correctly.
Hello, your tutorial is very helpful. But I don´t like such big boxes around the name and the other data. How can I take it off?
The border can be removed by changing the fourth parameter:
Change
$pdf->Cell(0, 10, 'Niraj Shah', 1, 0, 'C');
to:
$pdf->Cell(0, 10, 'Niraj Shah', 0, 0, 'C');
How to bind a html form to a form in pdf? That the data is transferred to the form and that I do not expose the coordinates.
Not sure what you mean. You can’t connect a HTML form to a PDF, but you can write the information captured in a HTML form as text to the PDF.
Can i insert an image instead of a text?
Haven’t tried an image, but it should be possible. It will be more complicated as you will need to calculate the size of the image and where it should be inserted.
is it possible to fill placeholders instead writing on X and Y axis?
You wouldn’t be able to full placeholders unless you are able to find them in the PDF and then overwrite them. In PHP this would be easy to do, but placeholders are much harder in PDFs as you would need to “read” the PDF and replace the placeholders.
$pagecount is not used, why ?
That’s true. In fact, this line doesn’t need to be assigned to `$pagecount` at all, so it can be changed to:
$pdf->setSourceFile( 'certificate.pdf' );
is it possible the edited pdf is download automaticaly?
Yes, you can do the following. The
D
forces the file to download, with the second param being the filename.$pdf->output('D', 'name.pdf');