<p>Are there PHP libraries which can be used to fill PDF forms and then save (flatten) them to PDF files?</p>

[{'answer_id': 77930, 'author': 'pix0r', 'author_id': 72, 'author_profile': 'https://Stackoverflow.com/users/72', 'pm_score': 1, 'selected': False, 'text': '<p>Looks like this has been <a href="https://stackoverflow.com/questions/7364/pdf-editing-in-php">covered before</a>.  Click through for relevant code using Zend Framework PDF library.</p>\n'}, {'answer_id': 77950, 'author': 'foxxtrot', 'author_id': 10369, 'author_profile': 'https://Stackoverflow.com/users/10369', 'pm_score': 1, 'selected': False, 'text': '<p>We use <a href="http://www.pdflib.com/" rel="nofollow noreferrer">PDFLib</a> at work.  The paid version isn\'t very expensive, and there is a more limited open source edition, if you are unable to shell out for the paid version.</p>\n'}, {'answer_id': 78300, 'author': 'bmb', 'author_id': 5298, 'author_profile': 'https://Stackoverflow.com/users/5298', 'pm_score': 7, 'selected': True, 'text': '<p>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).</p>\n\n<p>See <a href="https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/" rel="noreferrer">https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/</a></p>\n\n<p>You can call the command line from php, and the command is</p>\n\n<p><code>pdftk</code> <em>formfile.pdf</em> <code>fill_form</code> <em>fieldinfo.fdf</em> <code>output</code> <em>outputfile.pdf</em> <code>flatten</code></p>\n\n<p>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:</p>\n\n<p><a href="http://www.tgreer.com/fdfServe.html" rel="noreferrer">http://www.tgreer.com/fdfServe.html</a></p>\n\n<p>[Edit: The above link seems to be out of commission. Here is some more info...]</p>\n\n<p>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</p>\n\n<pre><code>...\n&lt;&lt; /T(f1-1) /V(text of field) &gt;&gt;\n&lt;&lt; /T(f1-2) /V(text of another field) &gt;&gt;\n...\n</code></pre>\n\n<p>You might also check out <a href="https://github.com/mikehaertl/php-pdftk" rel="noreferrer">php-pdftk</a>, which is a library specific to PHP. I have not used it, but commenter Álvaro (below) recommends it.</p>\n'}, {'answer_id': 89023, 'author': 'josh.chavanne', 'author_id': 14708, 'author_profile': 'https://Stackoverflow.com/users/14708', 'pm_score': 2, 'selected': False, 'text': '<p>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). </p>\n\n<pre><code> &lt;?php\nrequire(\'fpdf.php\');\n$pdf=new PDF();\n$pdf-&gt;AddPage();\n$pdf-&gt;SetY(30);\n$pdf-&gt;SetX(100);\n$pdf-&gt;MultiCell(10,4,$_POST[\'content\'],0,\'J\');\n$pdf-&gt;Output();\n?&gt;\n</code></pre>\n\n<p>and the you could have something like this.</p>\n\n<pre><code>  &lt;form action="fooPDF.php" method="post"&gt;\n     &lt;p&gt;PDF CONTENT: &lt;textarea name="content" &gt;&lt;/textarea&gt;&lt;/p&gt;\n     &lt;p&gt;&lt;input type="submit" /&gt;&lt;/p&gt;\n    &lt;/form&gt;\n</code></pre>\n\n<p>This skeletal example ought to help ya get started. </p>\n'}, {'answer_id': 112712, 'author': 'Chris Dolan', 'author_id': 14783, 'author_profile': 'https://Stackoverflow.com/users/14783', 'pm_score': 1, 'selected': False, 'text': '<p>I wrote a Perl library, <a href="http://search.cpan.org/dist/CAM-PDF/" rel="nofollow noreferrer">CAM::PDF</a>, with a <a href="http://search.cpan.org/dist/CAM-PDF/bin/fillpdffields.pl" rel="nofollow noreferrer">command-line interface</a> 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.</p>\n'}, {'answer_id': 1078577, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>generating fdf File with php:\nsee <a href="http://www.php.net/manual/en/book.fdf.php" rel="nofollow noreferrer">http://www.php.net/manual/en/book.fdf.php</a></p>\n\n<p>then fill it into a pdf with pdftk (see above)</p>\n'}, {'answer_id': 10674039, 'author': 'Nikolay', 'author_id': 1406316, 'author_profile': 'https://Stackoverflow.com/users/1406316', 'pm_score': 2, 'selected': False, 'text': '<p>For:</p>\n\n<ul>\n<li>Easier input format then XFDF</li>\n<li>True UTF-8 (Russian) support</li>\n<li>Complete php usage example</li>\n</ul>\n\n<p>Feel free to check my <a href="https://sourceforge.net/projects/pdfformfiller2/" rel="nofollow">PdfFormFillerUTF-8</a>.</p>\n'}, {'answer_id': 12772058, 'author': 'Val Redchenko', 'author_id': 572660, 'author_profile': 'https://Stackoverflow.com/users/572660', 'pm_score': 3, 'selected': False, 'text': "<p>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</p>\n\n<pre><code>file -bi fields.fdf\n</code></pre>\n\n<p>you get </p>\n\n<pre><code>application/octet-stream; charset=binary\n</code></pre>\n\n<p>then you've most likely run into a UTF-16 character set issue.\nTry converting the ftf by means of</p>\n\n<pre><code>cat fields.fdf | sed -e's/\\x00//g' | sed -e's/\\xFE\\xFF//g' &gt; better.fdf\n</code></pre>\n\n<p>I was then able to edit and import the better.fdf file into my PDF form.</p>\n\n<p>Hopefully this saves someone some Google-ing</p>\n"}]
['https://Stackoverflow.com/questions/77873', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/14166/']