Font issue with PDFtk
Asked Answered
R

2

24

I'm having difficulties filling in a form using pdftk with text fields with true type fonts.

  • Font files (.ttf) are added to /Library/Fonts (OSX Mavericks)
  • The form is created with Adobe Acrobat Pro
  • The form includes normal (non form) text using these fonts
  • The form text fields also use these fonts
  • The form can successfully be filled and printed using Adobe Acrobat Pro and even Preview

However, pdftk throws an error when trying to fill it using the command:

pdftk ./my_form.pdf fill_form my_data.fdf output ./the_output.pdf

The output is:

Unhandled Java Exception in create_output():
java.lang.ArrayIndexOutOfBoundsException: 0
   at pdftk.com.lowagie.text.pdf.DocumentFont.fillEncoding(pdftk)
   at pdftk.com.lowagie.text.pdf.DocumentFont.doType1TT(pdftk)
   at pdftk.com.lowagie.text.pdf.DocumentFont.<init>(pdftk)
   at pdftk.com.lowagie.text.pdf.AcroFields.getAppearance(pdftk)
   at pdftk.com.lowagie.text.pdf.AcroFields.setField(pdftk)
   at pdftk.com.lowagie.text.pdf.AcroFields.setFields(pdftk)

If I change the font of the text inputs to Helvetica, Times Roman or Courier, pdftk will successfully create a PDF. Oddly though, Arial and Georgia also throw the same error.

I have tried to no avail to embed the fonts in the PDF using Ghostscript as suggested in this question How to repair a PDF file and embed missing fonts. gs may have embedded the fonts, but it removes the form fields so the resulting PDF can't feed back into pdftk.

A working resolution would be greatly appreciated.

Royston answered 30/5, 2014 at 6:46 Comment(2)
PdfTk uses an obsolete version of iText to fill out forms. You'll recognize the reference to my name in the packages. Have you tried filling out the form using a recent version of iText? If the problem still exists, we (the iText Software companies) can take a look. We are not going to look at pdftk. We are not responsible for pdftk, nor do we endorse it. By the way: I'm not happy to see my name turn up in reports about software that wasn't upgraded in the last 5 years, but there's very little I can do about it.Erring
@BrunoLowagie I really appreciate all the work you've put into iText, but with all due respect: pdftk is the only way for many people outside of the Java world. So your advise to use iText is simply not feasible for many of us. But how about providing a nice and clean command line tool with iText to make it available to a much broader audience?Marchall
B
23

I was getting the same java.lang.ArrayIndexOutOfBoundsException: 0 error using pdftk to fill forms on an Adobe Acrobat generated PDF. This question is super old, but I couldn't find a consistent answer on stackoverflow or elsewhere so I figured I'd post my fix.

What ended up working for me:

  1. Opening the PDF in the OS X app Preview
  2. Clicking into a form field, adding text then deleting that text (so nothing is actually changed)
  3. Saving it

  4. Running the PDF through pdftk again

I'm not that familiar with encoding or PDFs in general, but saving the PDF with Preview seems to fix the encoding or at least get it to a place where pdftk can work with it. Good luck.

Babara answered 8/6, 2017 at 18:20 Comment(5)
That fixed if for me too. Had saved the doc in Adobe Acrobat DC and then opened in Preview and edited all the form fields, then saved and the error was gone.Dignity
Worked for me as well, but the pressing question is still: What does this actually do? One has to consider this before submitting it to a repository? I mean, what does it change with the file?Impetuosity
I didn't have the ArrayIndexOutOfBoundsException but I had a problem with the font looking wonky, but this solution fixed that too :-)Whitby
Tip: I was able to get this to work, only after editing each input field and then saving. If I only edited the first input and saved, I encountered the same error.Whopping
I spent a day trying to solve such issue; Your answer helped me, thanks!Fencesitter
G
-1

This was causing a huge headache for me for 2 days. It turns out I was focusing on the wrong end of the problem.

A nice alternative that isn't as manual and only has to be done once is to enter some text in a field of the source PDF form, in your case ./my_form.pdf. I don't know EXACTLY why this works, but it does. that way if you want to create a new file at any time, you dont have to go through this trouble :)

Gallicanism answered 15/10, 2019 at 12:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.