Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Font issue with PDFtk

Tags:

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.

like image 373
davur Avatar asked May 30 '14 06:05

davur


People also ask

How do I fix the font on a PDF?

Choose Tools > Edit PDF > Edit . The dotted outlines identify the text and images you can edit. Select the text you want to change. In the right-side Format panel, select a font, font size, or other formatting options.

What fonts work with PDF?

The 14 standard PDF fonts are Courier (Regular, Oblique, Bold, Bold Oblique), Helvetica (Regular, Oblique, Bold, Bold Oblique), Times (Roman, Italic, Bold, Bold Italic), Symbol, and ITC Zapf Dingbats. Fonts included with Adobe Cloud subscriptions.

How do I embed a font in an existing PDF?

Bring up the Adobe PDF settings and properties, then Adobe PDF settings. Embed your font. Edit the default settings and navigate to Font, click the Embed all fonts option. Your font should now be installed for use.


1 Answers

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.

like image 195
George Avatar answered Oct 04 '22 07:10

George