0

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 ?

Bahirji Naik
  • 175
  • 1
  • 2
  • 16

3 Answers3

0

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

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.
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Hello Bruno, Thanks for this details explanation. I am developing my web app in PHP where I want to put data from database into above PDF form. Can I fill this form using iText ? Is yes then is there any tutorial where I can find detail instruction. ? – Bahirji Naik Jun 27 '18 at 17:37
  • As I explained, iText only exists in Java and .NET, not in PHP. There are plenty of tutorials on the official web site http://itextpdf.com – Bruno Lowagie Jun 27 '18 at 17:39
  • We can develop simple API in Java. PHP form will send data to API and API can fill data into PDF form using iText and create new PDF file with filled form. Is it possible ? – Bahirji Naik Jun 27 '18 at 17:45
0

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.

Mokariya Sanjay
  • 194
  • 3
  • 13
  • Yes.. its working.. but It removing XFA structure. So that it is not again readable in software. Is there any way to update XFA structure ? – Bahirji Naik Jun 28 '18 at 10:43
  • Refer this link i hope that help you https://stackoverflow.com/questions/29633873/pdftk-and-removing-the-xfa-format – Mokariya Sanjay Jul 04 '18 at 05:38