Are there PHP libraries which can be used to fill PDF forms and then save (flatten) them to PDF files?
-
4This edited version of the question is hopefully a good candidte for reopen. It's been viewed >36K times and has interesting answers. – jah Jun 14 '15 at 09:59
-
@jah: It's a library recommendation question, which by its very nature is off-topic. Even if it were to be reopened it'd simply be closed again with a more precise reason. – BoltClock Jun 16 '15 at 06:09
-
6@jah: Perhaps if you rephrase the question "In PHP, how can I fill PDF forms and then save (flatten) them to PDF files." Seems some people can't get past the word "library". – bmb Mar 03 '16 at 22:46
8 Answers
The libraries and frameworks mentioned here are good, but if all you want to do is fill in a form and flatten it, I recommend the command line tool called pdftk (PDF Toolkit).
See https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/
You can call the command line from php, and the command is
pdftk
formfile.pdf fill_form
fieldinfo.fdf output
outputfile.pdf flatten
You will need to find the format of an FDF file in order to generate the info to fill in the fields. Here's a good link for that:
http://www.tgreer.com/fdfServe.html
[Edit: The above link seems to be out of commission. Here is some more info...]
The pdftk command can generate an FDF file from a PDF form file. You can then use the generated FDF file as a sample. The form fields are the portion of the FDF file that looks like
...
<< /T(f1-1) /V(text of field) >>
<< /T(f1-2) /V(text of another field) >>
...
You might also check out php-pdftk, which is a library specific to PHP. I have not used it, but commenter Álvaro (below) recommends it.

- 37,464
- 17
- 118
- 167

- 6,058
- 2
- 37
- 58
-
2
-
1UTF-16 issues that may arise with fdf files are addresses in a separate answer below. – Val Redchenko Oct 07 '12 at 19:24
-
4This is a cross-platform and cross-language solution but, since the question is tagged as PHP, I'll recommend [php-pdftk](https://github.com/mikehaertl/php-pdftk) to take care of the internals. My first tests have been impressive. – Álvaro González Aug 14 '15 at 12:32
-
Thanks Álvaro. Since this answer predates that project by about 6 years, it was unknown to me at the time. – bmb Aug 14 '15 at 15:05
A big +1 to the accepted answer, and a little tip if you run into encoding issues with the fdf file. If you generate the fields.fdf and upon running
file -bi fields.fdf
you get
application/octet-stream; charset=binary
then you've most likely run into a UTF-16 character set issue. Try converting the ftf by means of
cat fields.fdf | sed -e's/\x00//g' | sed -e's/\xFE\xFF//g' > better.fdf
I was then able to edit and import the better.fdf file into my PDF form.
Hopefully this saves someone some Google-ing

- 580
- 1
- 8
- 20
For:
- Easier input format then XFDF
- True UTF-8 (Russian) support
- Complete php usage example
Feel free to check my PdfFormFillerUTF-8.
generating fdf File with php: see http://www.php.net/manual/en/book.fdf.php
then fill it into a pdf with pdftk (see above)
I've had plenty of success with using a form that submits to a php script that uses fpdf and passes in the form fields as get variables (maybe not a great best-practice, but it works).
<?php
require('fpdf.php');
$pdf=new PDF();
$pdf->AddPage();
$pdf->SetY(30);
$pdf->SetX(100);
$pdf->MultiCell(10,4,$_POST['content'],0,'J');
$pdf->Output();
?>
and the you could have something like this.
<form action="fooPDF.php" method="post">
<p>PDF CONTENT: <textarea name="content" ></textarea></p>
<p><input type="submit" /></p>
</form>
This skeletal example ought to help ya get started.

- 329
- 3
- 11
-
1Dhek template editor can be useful to define areas where you want to write data over PDF, with a separate template you can load in PHP when merging data, to get bounds/coordinates: https://github.com/applicius/dhek . – cchantep May 22 '14 at 11:25
I wrote a Perl library, CAM::PDF, with a command-line interface that can solve this. I tried using an FDF solution years ago, but found it way too complicated which is why I wrote CAM::PDF in the first place. My library uses a few heuristics to replace the form with the desired text, so it's not perfect. But it works most of the time, and it's fast, free and quite straightforward to use.

- 8,905
- 2
- 35
- 73
Looks like this has been covered before. Click through for relevant code using Zend Framework PDF library.
-
Yes, I saw that response. However, I was wondering if there was some library specific for working with PDF forms, not just flat PDF documents. – mno Sep 17 '08 at 03:36