How to fill PDF form in php
Calling pdftk is a nice way to accomplish this. I'll assume that you know how to execute an external program and leave that out.
First, create an FDF file from your PDF.
pdftk form.pdf generate_fdf output data.fdf
You can now use that as template. The sample that you provided looks like this:
%FDF-1.2
%<E2><E3><CF><D3>
1 0 obj
<<
/FDF
<<
/Fields [
<<
/V ()
/T (Date)
>>
<<
/V /
/T (CheckBox2)
>>
<<
/V /
/T (CheckBox3)
>>
<<
/V /
/T (CheckBox4)
>>
<<
/V /
/T (CheckBox5)
>>
<<
/V ()
/T (Your_Last_Name)
>>
<<
/V ()
/T (Your_First_Name)
>>
<<
/V /
/T (CheckBox1)
>>]
>>
>>
endobj
trailer
<<
/Root 1 0 R
>>
%%EOF
The fields are lines that start with /V ()
. Enter your desired values into those fields. For example:
%FDF-1.2
%<E2><E3><CF><D3>
1 0 obj
<<
/FDF
<<
/Fields [
<<
/V (February 4, 2012)
/T (Date)
...
Finally, merge the FDF with the PDF. Execute the command:
pdftk form.pdf fill_form data.fdf output form_with_data.pdf
If you don't need to keep the FDF and generated PDF files, you can simply pipe the data via stdin and stdout instead of using temp files.
Filling PDF Forms with PHP
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.
Fill Pdf form using php laravel
Change ->flatten()
to needAppearances()
.
Note: When filling in UTF-8 data, you should always add the needAppearances() option. This will make sure, that the PDF reader takes care of using the right fonts for rendering, something that pdftk can't do for you. Also note that flatten() doesn't really work well if you have special characters in your data.
https://github.com/mikehaertl/php-pdftk
Filling PDF form by PHP - PDFtk not working
First u have install pdftk on your server then run this following command
pdftk path/to/the/form.pdf dump_data_fields > field_names.txt
and you get txt file for your pdf file and it's output like this
FieldType: Text
FieldName: data[0].#pageSet[0].TimeSheet[0].KUNNR[0]
FieldNameAlt: User
FieldFlags: 1
FieldValueDefault: 65604
FieldJustification: Left
FieldMaxLength: 10
FieldJustification: Left
---
then u use following package for it
composer require mikehaertl/php-pdftk
then update composer
use mikehaertl\pdftk\Pdf;
// Fill form with data array
$pdf = new Pdf('path of your pdf');
$pdf->fillForm([
'data[0].#pageSet[0].TimeSheet[0].KUNNR[0]'=>'sanjay',
])
->needAppearances()
->saveAs('filled.pdf');
it's work for me.
Fill pdf form image field with image
It is possible to carry an image via FDF however the aim of Forms Data is to carry simple text objects such as text field entries or other comments. So for an image it needs to be as separate annotation stamp and unsure if that can be attached to a field as such.
Here is a stamp added to the "clean" file ( note it is "under" the field entries)
%FDF-1.4
%âãÏÓ
1 0 obj
<<
/FDF <<
/Annots [2 0 R]
/F (clean.pdf)
/ID [<BBB71BA5F45E7149AAF360F13C5FB61A> <B7FE6051163F4F46B31241A24E573F8B>]
/UF (clean.pdf)
>>
/Type /Catalog
>>
endobj
2 0 obj
<<
/AP <<
/N 3 0 R
>>
/C [1 0 0]
/CreationDate (D:20220127132137Z)
/F 4
/IT /Stamp
/M (D:20220127132137Z)
/Name /image.08000006
/NM (1a68adc3-7c8a-45a2-b661dc794cc8ea96)
/Page 0
/Rect [0 399 412 792]
/Subj (Stamp)
/Subtype /Stamp
/T (K)
/Type /Annot
>>
endobj
3 0 obj
<<
/BBox [0 0 412 393]
/Filter /FlateDecode
/Length 34
/Resources <<
/XObject <<
/Im0 4 0 R
>>
>>
/Subtype /Form
/Type /XObject
>>
stream
xÚ+ä214R0...
... continues lots of binary lines ...
(F8F8F1Â1ŠŽŽQ¾KþM
endstream
endobj
trailer
<<
/Root 1 0 R
>>
%%EOF
The image could be a signature and perhaps the stream be converted to a textual stream for simple text transfer or text templating without binary content.
Related Topics
Column Count of MySQL.Proc Is Wrong. Expected 20, Found 16. the Table Is Probably Corrupted
PHP Exec - Check If Enabled or Disabled
Why Does Index.HTML Have Priority Over Index.Php
What Is the Syntax for Sorting an Eloquent Collection by Multiple Columns
Show/Hide Fields Depending on Select Value
PHP Preg_Match (.*) Not Matching Past Line Breaks
Change Date Format (In Db or Output) to Dd/Mm/Yyyy - PHP MySQL
Add PHP Variable Inside Echo Statement as Href Link Address
$Stmt->Execute():How to Know If Db Insert Was Successful
How Are Associative Arrays Implemented in PHP
What Is the Best Practice to Use When Using PHP and HTML
PHP Linefeeds (\N) Not Working
Who Add "_" Single Underscore Query Parameter
Jquery $.Ajax Request of Datatype JSON Will Not Retrieve Data from PHP Script