Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filling PDF Forms with PHP [closed]

Tags:

php

pdf

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

like image 369
mno Avatar asked Sep 16 '08 22:09

mno


People also ask

Why is my PDF fillable form not working?

If you can't type into a form field on a pdf, it may be due to a browser's default viewer for pdfs. Fillable forms require Adobe Acrobat or Acrobat Reader/Acrobat DC to fill them out online or on your computer. Many browsers use a different pdf viewer by default that doesn't support fillable form fields.

Why are my PDF forms not showing filled in form fields?

If the fillable fields in a PDF show as blank after getting filled in, the PDF will need to be printed to a new PDF to resolve this issue. This is typically caused when the PDF is filled using something other than Acrobat (i.e., a web browser or other PDF editing software).

Why can't I fill in a fillable PDF after saving?

If you're having trouble filling in and submitting forms, check the following conditions: Make sure that the security settings allow form filling. (See File > Properties > Security.) Make sure that the PDF includes interactive, or fillable, form fields.


2 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.

like image 197
bmb Avatar answered Oct 24 '22 19:10

bmb


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

like image 38
Val Redchenko Avatar answered Oct 24 '22 19:10

Val Redchenko