Reputation: 11
I am trying to fill XFA pdf forms using python, however I don’t see any library that can perform this.
I have used fillpdf
to update the forms but that does not work.
Below is my code:
import fillpdf
from fillpdf import fillpdfs
fillpdfs.get_form_fields('f1040.pdf', sort=False, page_number=1)
Data_dict= {*updated form fields*}
fillpdfs.write_fillable_pdf('f1040.pdf', 'output.pdf', data_dict, flatten=False)
Any suggestion about alternative libraries?
Upvotes: 1
Views: 2616
Reputation: 11857
From the Adobe API support partners (Datalogics)
XFA was deprecated in the new PDF 2.0 format and is not permitted for use with special ISO formats for PDF documents ... some industries, i.e., government institutions, are still using legacy XFA forms today, and often need to convert them to a more widely used format like AcroForm or standard PDF to make them easier to use or to use them at all.
The Fix: How to Edit XFA Forms
If you are working with XFA forms, here are the solutions we recommend:
Convert XFA forms to AcroForm
Most form fillers will use PDFtk as its one command line to export and we can export the FDF data direct. to allow bulk editing of say one file per person.
pdftk f1040-fred.pdf generate_fdf output f1040-fred.fdf verbose
But here is the Problem The Edited FDF can open the PDF but cannot repopulate the static fields as its not flattened. Thus there is a lack of tools to work with Adobe licensed proprietary XFA content. You need Adobe Acrobat to be sure of XFA working across the board.
Hence the comments from Adobe Support Partner simplify the XFA into a standard AcroForm (which should work with PDFtk and other [x]FDF applications)
Note that many libraries that can work with PDF will have problems with XFA as it is only the coverpage (Adobe advert to use Acrobat) that is a true PDF object.
Some tools have trouble converting dynamic XFA to PDF
Aspose have libraries that can convert XFA.xmp into AcroForm.PDF, here we see the dynamic version in top and the workable AcroForm in lower picture after conversion by Aspose
Finally we can work with standard FDF in the now regular.pdf
and it would be similar for a Browser, until Acrobat convince MS to use their viewer!
Later Edit interesting that currently Edge is now using Adobe Plug-in but that XFA form is still NOT usable ?
However Acrobat official statement is
XFA-based PDF forms and policy-protected documents require Adobe® Acrobat® or Adobe® Reader®, version 8 and later. See Adobe Downloads for downloading the latest Reader or Acrobat. Browsers such as Mozilla Firefox and Google Chrome offer a built-in PDF viewer that does not support XFA-based PDF forms. To view XFA-based PDF forms in these browsers, you must configure to open PDFs using Acrobat or Reader.
So I presume that means the full Adobe plug-in. https://helpx.adobe.com/livecycle/kb/xfa-forms-firefox-chrome.html
Upvotes: 0
Reputation: 21
This helper is python script for examining XFA objects using pikepdf: https://github.com/AF-VCD/pdf-xfa-tools/blob/master/xfaTools.py. You can then use the ElementTree XML for python to examine the fields and update the value of the field text.
Upvotes: 2