zeroTutorials

Java, Server, Database Tutorials

Have a Question?

If you have any question you can ask below or enter what you are looking for!

Maven resources plugin – PDFBox : java.io.IOException head is mandatory

1. The exception :

Using the PDFBox library, we got the following exception :

java.io.IOException: head is mandatory
        at org.apache.fontbox.ttf.TTFParser.parseTables(TTFParser.java:182)
        at org.apache.fontbox.ttf.TTFParser.parse(TTFParser.java:150)
        at org.apache.fontbox.ttf.TTFParser.parse(TTFParser.java:87)
        at org.apache.pdfbox.pdmodel.font.PDType0Font.load(PDType0Font.java:67)

2. The root cause of the exception :

In the project, we use the following configuration of the maven resources plugin :

<build>
  <resources>
     <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
     </resource>
  </resources>
</build>

The resources directory contains :

  • The properties files: these must be filtered by the maven plugin
  • Binaries that should not be filtered by the maven plugin
    • Font file used by the PDFBox library
    • Pictures…

At the maven resources plugin site, it is mentioned :

Warning: Do not filter files with binary content like images! This will most likely result in corrupt output.If you have both text files and binary files as resources it is recommended to have two separated folders. One folder src/main/resources (default) for the resources which are not filtered and another folder src/main/resources-filtered for the resources which are filtered.

The maven resources plugin modified the font file which is used by the PDFBox library and subsequently generated the exception “java.io.IOException head is mandatory”.

3. The solution :

The solution is to use two different resource directories:

  • Le répertoire src/main/resources-filtered for the resources which are filtered.
    • Application.properties
    • Application-dev.properties
    • application-prod.properties
  • Le répertoire src/main/resources (default) for the resources which are not filtered.
    • Images
    • Fonts (used by PDFBox)
<build>
  <resources>
     <resource>
        <directory>src/main/resources-filtered</directory>
        <filtering>true</filtering>
     </resource>
     <resource>
        <directory>src/main/resources</directory>
        <filtering>false</filtering>
     </resource>
  </resources>
</build>

Checking if there has been a change in the content of binary resources using fc command in Windows :

fc.exe /b src\main\resources\font.ttf target\classes\font.ttf
Tags:  ,