0
votes

I have PDF form generated in SAP. There are approximately 30 fields in that form. Some are already filled. While others will fill in by an employee. Here is the sample of that form.

enter image description here

Now I am going to create a php application which can accept data from user and fill in above form and generate that PDF file. I read tutorials of filling PDF form by PDFtk Server Utility. So I download and install this tool in my windows 7. Before creating my PHP application I thought that I have to try to fill the form manually on my PC. So first I dump data fields using following command and try to fill one variable.

pdftk sample.pdf dump_data_fields > fields_names.txt

output :

.
.
FieldType: Text
FieldName: data[0].#pageSet[0].TimeSheet[0].CURRENTWEEK[0]
FieldNameAlt: Week
FieldFlags: 0
FieldValueDefault: 26
FieldJustification: Left
FieldMaxLength: 2
---
FieldType: Text
FieldName: data[0].#pageSet[0].TimeSheet[0].CURRENTYEAR[0]
FieldNameAlt: Year
FieldFlags: 0
FieldValueDefault: 2018
FieldJustification: Left
FieldMaxLength: 4
.
.

Then I create FDF file to fill CURRENTWEEK variable..

%FDF-1.2
%âãÏÓ
1 0 obj 
<<
/FDF 
<<
/Fields [
<</T(data[0].#pageSet[0].TimeSheet[0].CURRENTWEEK[0])/V(80)>>
]
>>
>>
endobj
trailer
<<
/Root 1 0 R
>>
%%EOF

I run this command to generate filled form

pdftk sample.pdf fill_form data.fdf output output.pdf

PDF form doesn't get filled. Still showing original PDF form. Again when I dump fields of newly created form it showing..

---
FieldType: Text
FieldName: data[0].#pageSet[0].TimeSheet[0].CURRENTWEEK[0]
FieldNameAlt: Week
FieldFlags: 0
FieldValue: 80
FieldValueDefault: 26
FieldJustification: Left
FieldMaxLength: 2
---

FieldValue:80 get added but there is no data in form. Showing same 26 value in field. Is there any mistake while creating FDF or running command ?

3

3 Answers

0
votes

I think it is issue of fdf file format so first of all update your fdf file with following file:

%FDF-1.2
1 0 obj<</FDF<< /Fields[

<</T(data[0].#pageSet[0].TimeSheet[0].CURRENTWEEK[0])/V(80)>>

] >> >>
endobj
trailer
<</Root 1 0 R>>
%%EOF

after update fdf file run following command in terminal:

pdftk sample.pdf fill_form data.fdf output output.pdf

Note: check all file's path correctly

I hope it will help you.

0
votes

There are two types of forms technology in PDF.

  • AcroForm technology: the form is defined using PDF syntax; field names look like this: timeSheet.currentweek.
  • the XML Forms Architecture (aka XFA): the form is defined using XML; field names look like this: data[0].#pageSet[0].TimeSheet[0].CURRENTWEEK[0]

Sometimes, a PDF contains both technologies. This is often referred to as a hybrid form.

Based on the info you share in your question, I assume that your form is a hybrid form. The form has an XFA stream inside, and at the same time, the fields are defined using PDF syntax. When you fill it out, you need to make sure that you update the AcroForm form fields as well as the XML of the XFA form.

Why doesn't pdfTk do this correctly?

pdfTk is a rip-off of a iText that uses a version of iText that should no longer be used compiled to an executible with GCJ. You may notice my family name in some of the pdfTk error messages since I'm the original developer of iText. The iText version used by pdfTk predates XFA support in iText, hence it is normal that pdfTk can't fill out the form correctly.

How to work around this problem?

  • One workaround would be to remove the XFA part before filling out the form. See PDFTK and removing the XFA format
  • Another workaround would be to switch to Java or C# and to use the latest version of iText instead of an old version compiled to an executable.
0
votes

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.