Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Batik No WriteAdapter is available?

Tags:

svg

batik

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?

like image 979
Al Pacifico Avatar asked Jul 21 '17 13:07

Al Pacifico


1 Answers

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).

like image 147
Al Pacifico Avatar answered Nov 04 '22 16:11

Al Pacifico