9

I have a PHP application that fills out a form from a database call. At present I am putting this together using PDFtk, I am able to run a number of PDFtk commands with no issue and I am currently working out the desired command at command line.

My call is currently this:

pdftk /var/www/html/CSR/template/job_card.pdf fill_form /var/www/html/CSR/template/wwwwu7mMH.fdf output /var/www/html/CSR/template/filled4.pdf

This exact call run multiple times generates this error sometimes:

    Unhandled Java Exception in create_output():
java.lang.ClassCastException: pdftk.com.lowagie.text.pdf.PdfNull cannot be cast to pdftk.com.lowagie.text.pdf.PdfDictionary
   at pdftk.com.lowagie.text.pdf.FdfReader.readFields(pdftk)
   at pdftk.com.lowagie.text.pdf.FdfReader.readPdf(pdftk)
   at pdftk.com.lowagie.text.pdf.PdfReader.<init>(pdftk)
   at pdftk.com.lowagie.text.pdf.PdfReader.<init>(pdftk)
   at pdftk.com.lowagie.text.pdf.FdfReader.<init>(pdftk)

and this error sometimes:

Unhandled Java Exception in create_output():
Unhandled Java Exception in main():
java.lang.NullPointerException
   at gnu.gcj.runtime.NameFinder.lookup(libgcj.so.10)
   at java.lang.Throwable.getStackTrace(libgcj.so.10)
   at java.lang.Throwable.stackTraceString(libgcj.so.10)
   at java.lang.Throwable.printStackTrace(libgcj.so.10)
   at java.lang.Throwable.printStackTrace(libgcj.so.10)

The error message alternates but the command never works and the form is never filled. As I say though, the PDFtk works with other commands, I have been able to generate encrypted PDFs and run the fixed commands succesfully.

My question is what is causing this error and how do I fix it?

user3192649
  • 313
  • 1
  • 4
  • 13

6 Answers6

12

I see my name in the StackTrace. That's not a coincidence: PdfTk is based on a mighty old version of iText. iText is a Java PDF library that was originally written by me, but used by a third party to create PdfTk.

The error tells you that iText is parsing a PDF that has either an error, or an unexpected feature.

A PDF consists of PDF objects such as PDF string objects, PDF number objects, PDF array objects, PDF dictionary objects, PDF stream objects, and so on. iText is able to retrieve these objects and to reuse them to create a new PDF. In your case, a new PDF with some form fields that are filled out is created based on the objects of the original PDF.

It is impossible to answer your question without seeing the PDF that causes the problem, but let's say that your PDF contains an /AcroForm entry with a /Fields array. In this fields array, there is a reference to a field dictionary. Suppose that one of the field dictionaries in your PDF isn't a dictionary, but a PDF null object. The form shows up perfectly in Adobe Reader, but internally, there is a flaw that prevents proper processing of the form.

In that case, iText will loop over the entries in the fields array, and one of those entries won't return a field dictionary, but a PdfNull object. In that case, you'll get a ClassCastException, because you can't cast PdfNull to PdfDictionary.

This being said:

  • If I see my name in your stack trace, this triggers an alarm, because it means that you're using an iText version that predates iText 5. Such a version should no longer be used. You should use a more recent version of iText. There is a high chance that a more recent version of iText gives you either a better error message, or tolerates (and maybe even fixes) the error in the PDF.
  • If you find a PdfTk version that uses a more recent version of iText, that would surprise me, because as far as I know, PdfTk isn't available under the AGPL, nor is PDF Labs (the owner of PdfTk) a customer of iText Software.
  • If you want to keep on using PdfTk, you shouldn't expect an answer as long as you don't share the PDF document that you're trying to fill.

One thing you could try: open the form in Adobe Acrobat. Save the form in Adobe Acrobat. There is a chance that the saved form no longer has the problem. Adobe Acrobat is very tolerant towards errors in PDFs. It tries to fix as many as it can. Then when you save the form, the error is gone.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Hi Bruno, I really appreciate you taking the time to give the explanation. I am happy to link to the PDF but it sounds like you have a very clear idea of what the problem is. The form I need to populate won't change and there are no options for users to add their own forms, as such perhaps it is easier to generate the form using a different method to bypass this issue? – user3192649 Apr 14 '16 at 08:09
  • Yes. If you shared the form, the first thing I would try is this: open the form in Adobe Acrobat. Save the form in Adobe Acrobat. There is a chance that the saved form no longer has the problem. Adobe Acrobat is very tolerant towards errors in PDFs. It tries to fix as many as it can. Then when you save the form, the error is gone. – Bruno Lowagie Apr 14 '16 at 08:15
  • Hi Mate, I ran the PDF in question through Acrobat but to no avail. The PDF can be found here: https://www.dropbox.com/s/jyh3yz7crr4143p/job_card.pdf?dl=0 If you could take a look I would appreciate it immensely. – user3192649 Apr 14 '16 at 08:50
  • Figured it out, problem was not related to the PDF itself, will post as answer. – user3192649 Apr 14 '16 at 12:00
10

As it turns out the issue was not as Bruno Lowagie suggested regarding the consistency of the PDF.

I had run out of ideas and just thought I would try generating the FDF a different way. By running the command:

pdftk /full/path/to/template.pdf generate_fdf output /full/path/to/output.fdf

And then inspecting the resulting file, I was able to get a more accurate FDF and then when I ran the fill_form command:

pdftk /full/path/to/template.pdf fill_form /full/path/to/output.fdf output /full/path/to/output.pdf

I got a proper response and everything worked. So the problem I was getting was in fact caused by the FDF being malformed in some way.

My final solution was this if anyone is interested. It takes a template PDF with fields, generates an FDF to fill it, creates a new PDF by adding the data from FDF with the template PDF, redirects the browser to the PDFs location.

Big thanks to Bruno Lowagie for helping understand the system better and rule out a few things.

user3192649
  • 313
  • 1
  • 4
  • 13
  • Thank you this is helpful, @user3192649. However, can I check if the FDF file is allowed to have SOME of the fields I would like to replace in the PDF, or does it have to include every field that the PDF expects? – Khom Nazid Jun 02 '18 at 03:08
  • I also had a malformed FDF file after following this [sitepoint](https://www.sitepoint.com/filling-pdf-forms-pdftk-php/) tutorial with some bad code. Reviewing your FDF function showed me where the sitepoint code was wrong. – I wrestled a bear once. Jan 22 '21 at 17:05
4

It looks like PDF TK was not able to process stings that had char ( and ) I replaced them with \) and \( to escape them, and it worked well.

stevieb
  • 9,065
  • 3
  • 26
  • 36
Guest
  • 41
  • 1
1

I had the same problem. In my case changing the string encoding solved it. Previously I was encoding it in utf-8 then I changed it to utf_16_be. Root cause is that form fields data are stored in fdf form where values are stored inside brackets so if your data has brackets then it throws error.

Vaibhav Gupta
  • 43
  • 1
  • 6
0

Font issue: https://stackoverflow.com/a/44442957/2150220

The link above is a better solution than just changing your font.

I was receiving the same error, however, none of the above solutions worked for me.

As I was testing: pdftk a.pdf fill_form a.fdf output b.pdf I was able to generate a pdf if my original pdf had not been altered, IE: all of the acrobat settings where default.

Only when I changed the font to "Arial" for a fill_form element did I receive the error. I changed the font, and it was working again.

mbunch
  • 560
  • 7
  • 21
0

I just wanted to follow up for anyone else who encounters this. In our case, the problem was in the contents of the FDF file. Specifically, we were automating the process of filling in PDFs and user-generated content sometimes includes an unclosed ( [ or { character. These cause this same exception. If this is happening to you, verify that the contents of your FDF file do not contain "unclosed" parens, brackets, or curly braces.

bendalton
  • 778
  • 6
  • 8