FOP giving NoSuchMethodError when font auto-detect enable

1.4k views Asked by At

I'm getting the bellow error when generating a PDF from Apache FOP,

  FopFactory fopFactory = FopFactory.newInstance(new File("/Users/vinurip/cloud/Stripe/Full/pdfboxtut/src/main/resources/fop.xconf"));
    OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(outFile)));
    Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

OutFile is a empty pdf file location. fop.xconf is the same one which comes in the FOP source

  Exception in thread "main" java.lang.NoSuchMethodError: org.apache.fontbox.cff.CFFFont.getProperty(Ljava/lang/String;)Ljava/lang/Object;
    at org.apache.fop.fonts.truetype.OTFFile.readName(OTFFile.java:134)
    at org.apache.fop.fonts.truetype.OpenFont.readFont(OpenFont.java:740)
    at org.apache.fop.fonts.truetype.OFFontLoader.read(OFFontLoader.java:109)
    at org.apache.fop.fonts.truetype.OFFontLoader.read(OFFontLoader.java:93)
    at org.apache.fop.fonts.FontLoader.getFont(FontLoader.java:124)
    at org.apache.fop.fonts.FontLoader.loadFont(FontLoader.java:108)
    at org.apache.fop.fonts.autodetect.FontInfoFinder.find(FontInfoFinder.java:254)
    at org.apache.fop.fonts.FontAdder.add(FontAdder.java:63)
    at org.apache.fop.fonts.FontDetectorFactory$DefaultFontDetector.detect(FontDetectorFactory.java:105)
    at org.apache.fop.fonts.FontManager.autoDetectFonts(FontManager.java:229)
    at org.apache.fop.fonts.DefaultFontConfigurator.configure(DefaultFontConfigurator.java:82)
    at org.apache.fop.render.PrintRendererConfigurator.getCustomFontCollection(PrintRendererConfigurator.java:147)
    at org.apache.fop.render.PrintRendererConfigurator.setupFontInfo(PrintRendererConfigurator.java:127)
    at org.apache.fop.render.intermediate.IFUtil.setupFonts(IFUtil.java:170)
    at org.apache.fop.render.intermediate.IFRenderer.setupFontInfo(IFRenderer.java:187)
    at org.apache.fop.area.RenderPagesModel.<init>(RenderPagesModel.java:75)
    at org.apache.fop.area.AreaTreeHandler.setupModel(AreaTreeHandler.java:135)
    at org.apache.fop.area.AreaTreeHandler.<init>(AreaTreeHandler.java:105)
    at org.apache.fop.render.RendererFactory.createFOEventHandler(RendererFactory.java:350)
    at org.apache.fop.fo.FOTreeBuilder.<init>(FOTreeBuilder.java:107)
    at org.apache.fop.apps.Fop.createDefaultHandler(Fop.java:104)
    at org.apache.fop.apps.Fop.<init>(Fop.java:78)
    at org.apache.fop.apps.FOUserAgent.newFop(FOUserAgent.java:182)
    at org.apache.fop.apps.FopFactory.newFop(FopFactory.java:220)
    at foptest.fo2PDF(foptest.java:73)
    at foptest.main(foptest.java:57)

This issue seems to occuring only when the font auto-detect is enabled

2

There are 2 answers

0
snorbi On

You can find some info using search engines - eg. https://qnalist.com/questions/6434450/pdfbox-2-0-and-batik - although these are not very useful...

My workaround was using <directory recursive="true">/usr/share/fonts</directory> instead of <auto-detect />.

0
Abdull On

I ran into this issue because of a Maven dependency mismatch between Apache FOP and Apache PDFBox/FontBox.

I resolved this issue by migrating my project's pom.xml to the latest dependency versions as of this writing:

<dependency>
  <groupId>org.apache.xmlgraphics</groupId>
   <artifactId>fop</artifactId>
   <version>2.2</version>
</dependency>
<dependency>
   <groupId>org.apache.pdfbox</groupId>
   <artifactId>pdfbox</artifactId>
   <version>2.0.9</version>
</dependency>
<dependency>
  <groupId>org.apache.pdfbox</groupId>
  <artifactId>fontbox</artifactId>
  <version>2.0.9</version>
</dependency>

To migrate to Apache FOP 2.2, follow the embedding guidelines and migration guideslines.