14

I'm writing code to convert SVG's to PNG's:

package com.example;

import java.io.*;
import java.nio.file.Paths;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.batik.transcoder.SVGAbstractTranscoder;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;

public class Main {

    public static void main(String [] args) throws Exception {

        // read the input SVG document into TranscoderInput
        String svgURI = Paths.get(args[0]).toUri().toURL().toString();
        TranscoderInput input = new TranscoderInput(svgURI);
        // define OutputStream to PNG Image and attach to TranscoderOutput
        OutputStream ostream = new FileOutputStream("out.png");
        TranscoderOutput output = new TranscoderOutput(ostream);
        // create a JPEG transcoder
        PNGTranscoder t = new PNGTranscoder();
        // set the transcoding hints
        t.addTranscodingHint(SVGAbstractTranscoder.KEY_HEIGHT, new Float(600));
        t.addTranscodingHint(SVGAbstractTranscoder.KEY_WIDTH, new Float(600));
        // convert and write output
        t.transcode(input, output);
        // flush and close the stream then exit
        ostream.flush();
        ostream.close();
    }
}

I get the following exceptions executing it with a variety of SVG's:

Exception in thread "main" org.apache.batik.transcoder.TranscoderException: null
Enclosed Exception:
Could not write PNG file because no WriteAdapter is availble
    at org.apache.batik.transcoder.image.ImageTranscoder.transcode(ImageTranscoder.java:132)
    at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:142)
    at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:156)
    at com.example.Main.main(Main.java:26)

Batik version (reported by Maven):

version=1.9
groupId=org.apache.xmlgraphics
artifactId=batik-transcoder

I get the same error with Batik 1.7.

Suggestions?

Al Pacifico
  • 592
  • 3
  • 14

1 Answers1

17

The problem was solved by Peter Coppens on the xmlgraphics-batik-users mailing list. The problem is that the Maven repository for Batik 1.9 is missing a dependency, which can be addressed by adding to pom.xml:

<dependency>
    <groupId>org.apache.xmlgraphics</groupId>
    <artifactId>batik-codec</artifactId>
    <version>1.9</version>
</dependency>

The cryptic exception disappears and the code functions as expected with this addition. This was reported as a bug for Batk 1.7 (https://bz.apache.org/bugzilla/show_bug.cgi?id=44682).

Al Pacifico
  • 592
  • 3
  • 14
  • where to find pom.xml ? – Lokesh Pandey Aug 23 '17 at 12:25
  • 1
    The dependencies node of my pom.xml included: org.apache.xmlgraphics batik-transcoder 1.9 org.apache.xmlgraphics batik-codec 1.9 Hope that helps. – Al Pacifico Aug 24 '17 at 13:10
  • 1
    I have experienced same issue with version 1.10 as well – ankitkpd Nov 06 '18 at 21:54
  • 1
    same with version 1.11 – rumman0786 Mar 13 '19 at 04:55
  • @rumman0786: I suggest crafting a simple example (you could even copy my code above and use the newer batik as a dependency) and submit it to the mailing list (can sign up at https://xmlgraphics.apache.org/batik/mailing-lists.html ). When I have a chance, I can double-check which batik version the deployed version of my code is using and if the dependency has been upgraded, see if it works... – Al Pacifico Mar 14 '19 at 11:50
  • 1
    Still same in 1.13 – ZhekaKozlov Jul 20 '20 at 13:22