Search code examples
javadebianghostscriptapache-fopbatik

SVG generation with FOP doesn't work


On one server, and on my Windows laptop, producing PDFs with this method works fine:

http://www.databasesandlife.com/svg-to-pdf/

But on the other server I get this error:

org.apache.batik.transcoder.TranscoderException: Error while setting up PDFDocumentGraphics2D
Enclosed Exception:
Error while setting up fonts
    at org.apache.fop.svg.PDFTranscoder.transcode(PDFTranscoder.java:189)
    at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(Unknown Source)
    at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(Unknown Source)
    at org.apache.batik.apps.rasterizer.SVGConverter.transcode(Unknown Source)
    at org.apache.batik.apps.rasterizer.SVGConverter.execute(Unknown Source)

I have been Googling and searching for hours, but to no avail. What can I do?

I tried installing the following packages but they didn't help:

sudo apt-get install gsfonts gsfonts-x11 gsfonts-other batik \
    libbatik-java libxmlgraphics-commons-java  \
    libxmlgraphics-commons-java fop sun-java6-fonts

My situation is:

  • Debian 6.0.3
  • Sun Java version "1.6.0_26"
  • JARs: avalon-framework-4.2.0.jar batik-all-1.7.jar commons-io-1.3.1.jar commons-logging-1.0.4.jar fop-0.95.jar log4j-1.2.15.jar xml-apis-ext.jar xmlgraphics-commons-1.3.1.jar

Solution

  • The approach to solving this problem is in log4j.properties I turned up the level to TRACE.

    There I saw the extra log before the TranscoderException that I'd seen previously:

    2012-02-28 11:51:24,863 DEBUG: org.apache.fop.fonts.FontCache: 
       Writing font cache to /usr/share/jetty/.fop/fop-fonts.cache
    org.apache.batik.transcoder.TranscoderException: 
       Error while setting up PDFDocumentGraphics2D
    

    Horray for logs! (And writing a log about what the program is about to do, not just once it has done it, so that if the operation fails, then you know what it was trying to do while it failed.)

    On Debian, the Jetty webserver runs under the user jetty and has its home directory at /usr/share/jetty/. However, the jetty user does not have write-access to its own home directory, therefore this ~/.fop directory could not be created.

    adrian@10770-02:~$ grep jetty /etc/passwd
    jetty:x:107:111::/usr/share/jetty:/bin/false
    adrian@10770-02:~$ ls -ld /usr/share/jetty
    drwxr-xr-x 7 root root 4096 Feb 28 11:52 /usr/share/jetty/
    

    I don't know whether this is by design, or a bug, but creating this directory so that Jetty could write it...

    sudo mkdir -p -m 0777 /usr/share/jetty/.fop
    

    ...solved the problem.