Is there a way to fill PDF forms and then save (flatten) them to PDF files from within PHP? Which library would you recommend?
Looks like this has been covered before. Click through for relevant code using Zend Framework PDF library.
Try using HTMLDOC, there is a free/opensource version available
There is more info on how to create a pdf using HTMLDOC with PHP:
http://www.htmldoc.org/documentation.php/CallingHTMLDOCfromPHP.html
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 http://www.accesspdf.com/pdftk/
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) >>
...
--
bmb
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.
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.
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)