Help with transcoding SVG to PNG Batik 1.9

Al Pacifico <[email protected]> Sun, 23 Jul 2017 07:27:26 -0700
Newsgroups gmane.text.xml.batik.user
Message-ID <CAGRVsLv=3y6CxmKEPXrtd7kxE75cXS9KFPo1YQqZeR-Sb56S2w@mail.gmail.com>
--94eb2c18fe46c903b40554fce58b
Content-Type: text/plain; charset="UTF-8"

Complete newbie to Batik here, so bear with me.

I am trying to transcode SVG's to PNG's using the following code:

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().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();
    }
}

Compiled using Maven, this code repeatedly fails (using a variety of
SVG's) with the following exception.

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)

After numerous Google searches, my theory is that the Maven repositories
are missing a dependency, but my brain says "blame yourself first," which
is why I am asking here.

Using the technique for listing dependencies outlined at
https://grep.codeconsult.ch/2010/07/08/list-all-your-maven-dependencies/, I
get the following list:
    commons-io:commons-io:jar:1.3.1
    commons-logging:commons-logging:jar:1.0.4
    junit:junit:jar:3.8.1
    org.apache.xmlgraphics:batik-anim:jar:1.9
    org.apache.xmlgraphics:batik-awt-util:jar:1.9
    org.apache.xmlgraphics:batik-bridge:jar:1.9
    org.apache.xmlgraphics:batik-constants:jar:1.9
    org.apache.xmlgraphics:batik-css:jar:1.9
    org.apache.xmlgraphics:batik-dom:jar:1.9
    org.apache.xmlgraphics:batik-ext:jar:1.9
    org.apache.xmlgraphics:batik-gvt:jar:1.9
    org.apache.xmlgraphics:batik-i18n:jar:1.9
    org.apache.xmlgraphics:batik-parser:jar:1.9
    org.apache.xmlgraphics:batik-script:jar:1.9
    org.apache.xmlgraphics:batik-svg-dom:jar:1.9
    org.apache.xmlgraphics:batik-svggen:jar:1.9
    org.apache.xmlgraphics:batik-transcoder:jar:1.9
    org.apache.xmlgraphics:batik-util:jar:1.9
    org.apache.xmlgraphics:batik-xml:jar:1.9
    org.apache.xmlgraphics:xmlgraphics-commons:jar:2.2
    xalan:serializer:jar:2.7.2
    xalan:xalan:jar:2.7.2
    xml-apis:xml-apis-ext:jar:1.3.04
    xml-apis:xml-apis:jar:1.3.04

Does anyone see a problem with my code?
-Al
-- 
Please forgive typo's, equally likely to have been typed on cell phone and
to have been typed one-handed.

--94eb2c18fe46c903b40554fce58b
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Complete newbie to Batik here, so bear with me.<div><br></=
div><div>I am trying to transcode SVG&#39;s to PNG&#39;s using the followin=
g code:</div><div><pre style=3D"color:rgb(0,0,0);font-family:&quot;DejaVu S=
ans Mono&quot;;font-size:11.3pt"><span style=3D"color:rgb(0,0,128);font-wei=
ght:bold">package </span>com.example;<br><br><span style=3D"color:rgb(0,0,1=
28);font-weight:bold">import </span>java.io.*;<br><span style=3D"color:rgb(=
0,0,128);font-weight:bold">import </span>java.nio.file.Paths;<br><span styl=
e=3D"color:rgb(0,0,128);font-weight:bold">import </span>org.apache.batik.tr=
anscoder.image.PNGTranscoder;<br><span style=3D"color:rgb(0,0,128);font-wei=
ght:bold">import </span>org.apache.batik.transcoder.SVGAbstractTranscoder;<=
br><span style=3D"color:rgb(0,0,128);font-weight:bold">import </span>org.ap=
ache.batik.transcoder.TranscoderInput;<br><span style=3D"color:rgb(0,0,128)=
;font-weight:bold">import </span>org.apache.batik.transcoder.TranscoderOutp=
ut;<br><br><span style=3D"color:rgb(0,0,128);font-weight:bold">public class=
 </span>Main {<br><br>    <span style=3D"color:rgb(0,0,128);font-weight:bol=
d">public static void </span>main(String [] args) <span style=3D"color:rgb(=
0,0,128);font-weight:bold">throws </span>Exception {<br><br>        <span s=
tyle=3D"color:rgb(128,128,128);font-style:italic">// read the input SVG doc=
ument into TranscoderInput<br></span><span style=3D"color:rgb(128,128,128);=
font-style:italic">        </span>String svgURI =3D Paths.<span style=3D"fo=
nt-style:italic">get</span>(args[<span style=3D"color:rgb(0,0,255)">0</span=
>]).toUri().toString();<br>        TranscoderInput input =3D <span style=3D=
"color:rgb(0,0,128);font-weight:bold">new </span>TranscoderInput(svgURI);<b=
r>        <span style=3D"color:rgb(128,128,128);font-style:italic">// defin=
e OutputStream to PNG Image and attach to TranscoderOutput<br></span><span =
style=3D"color:rgb(128,128,128);font-style:italic">        </span>OutputStr=
eam ostream =3D <span style=3D"color:rgb(0,0,128);font-weight:bold">new </s=
pan>FileOutputStream(<span style=3D"color:rgb(0,128,0);font-weight:bold">&q=
uot;out.png&quot;</span>);<br>        TranscoderOutput output =3D <span sty=
le=3D"color:rgb(0,0,128);font-weight:bold">new </span>TranscoderOutput(ostr=
eam);<br>        <span style=3D"color:rgb(128,128,128);font-style:italic">/=
/ create a JPEG transcoder<br></span><span style=3D"color:rgb(128,128,128);=
font-style:italic">        </span>PNGTranscoder t =3D <span style=3D"color:=
rgb(0,0,128);font-weight:bold">new </span>PNGTranscoder();<br>        <span=
 style=3D"color:rgb(128,128,128);font-style:italic">// set the transcoding =
hints<br></span><span style=3D"color:rgb(128,128,128);font-style:italic">  =
      </span>t.addTranscodingHint(SVGAbstractTranscoder.<span style=3D"colo=
r:rgb(102,14,122);font-weight:bold;font-style:italic">KEY_HEIGHT</span>, <s=
pan style=3D"color:rgb(0,0,128);font-weight:bold">new </span>Float(<span st=
yle=3D"color:rgb(0,0,255)">600</span>));<br>        t.addTranscodingHint(SV=
GAbstractTranscoder.<span style=3D"color:rgb(102,14,122);font-weight:bold;f=
ont-style:italic">KEY_WIDTH</span>, <span style=3D"color:rgb(0,0,128);font-=
weight:bold">new </span>Float(<span style=3D"color:rgb(0,0,255)">600</span>=
));<br>        <span style=3D"color:rgb(128,128,128);font-style:italic">// =
convert and write output<br></span><span style=3D"color:rgb(128,128,128);fo=
nt-style:italic">        </span>t.transcode(input, output);<br>        <spa=
n style=3D"color:rgb(128,128,128);font-style:italic">// flush and close the=
 stream then exit<br></span><span style=3D"color:rgb(128,128,128);font-styl=
e:italic">        </span>ostream.flush();<br>        ostream.close();<br>  =
  }<br>}<br></pre></div><div><div>Compiled using Maven, this code repeatedl=
y fails (using a variety of SVG&#39;s)=C2=A0with the following exception.=
=C2=A0</div><div><br></div><div>Exception in thread &quot;main&quot; org.ap=
ache.batik.transcoder.TranscoderException: null</div><div>Enclosed Exceptio=
n:</div><div>Could not write PNG file because no WriteAdapter is availble</=
div><div><span style=3D"white-space:pre">	</span>at org.apache.batik.transc=
oder.image.ImageTranscoder.transcode(ImageTranscoder.java:132)</div><div><s=
pan style=3D"white-space:pre">	</span>at org.apache.batik.transcoder.XMLAbs=
tractTranscoder.transcode(XMLAbstractTranscoder.java:142)</div><div><span s=
tyle=3D"white-space:pre">	</span>at org.apache.batik.transcoder.SVGAbstract=
Transcoder.transcode(SVGAbstractTranscoder.java:156)</div><div><span style=
=3D"white-space:pre">	</span>at com.example.Main.main(Main.java:26)</div><d=
iv><br></div><div>After numerous Google searches, my theory is that the Mav=
en repositories are missing a dependency, but my brain says &quot;blame you=
rself first,&quot; which is why I am asking here.</div><div><br></div><div>=
Using the technique for listing dependencies outlined at <a href=3D"https:/=
/grep.codeconsult.ch/2010/07/08/list-all-your-maven-dependencies/">https://=
grep.codeconsult.ch/2010/07/08/list-all-your-maven-dependencies/</a>, I get=
 the following list:<br></div><div><div>=C2=A0 =C2=A0 commons-io:commons-io=
:jar:1.3.1</div><div>=C2=A0 =C2=A0 commons-logging:commons-logging:jar:1.0.=
4</div><div>=C2=A0 =C2=A0 junit:junit:jar:3.8.1</div><div>=C2=A0 =C2=A0 org=
.apache.xmlgraphics:batik-anim:jar:1.9</div><div>=C2=A0 =C2=A0 org.apache.x=
mlgraphics:batik-awt-util:jar:1.9</div><div>=C2=A0 =C2=A0 org.apache.xmlgra=
phics:batik-bridge:jar:1.9</div><div>=C2=A0 =C2=A0 org.apache.xmlgraphics:b=
atik-constants:jar:1.9</div><div>=C2=A0 =C2=A0 org.apache.xmlgraphics:batik=
-css:jar:1.9</div><div>=C2=A0 =C2=A0 org.apache.xmlgraphics:batik-dom:jar:1=
.9</div><div>=C2=A0 =C2=A0 org.apache.xmlgraphics:batik-ext:jar:1.9</div><d=
iv>=C2=A0 =C2=A0 org.apache.xmlgraphics:batik-gvt:jar:1.9</div><div>=C2=A0 =
=C2=A0 org.apache.xmlgraphics:batik-i18n:jar:1.9</div><div>=C2=A0 =C2=A0 or=
g.apache.xmlgraphics:batik-parser:jar:1.9</div><div>=C2=A0 =C2=A0 org.apach=
e.xmlgraphics:batik-script:jar:1.9</div><div>=C2=A0 =C2=A0 org.apache.xmlgr=
aphics:batik-svg-dom:jar:1.9</div><div>=C2=A0 =C2=A0 org.apache.xmlgraphics=
:batik-svggen:jar:1.9</div><div>=C2=A0 =C2=A0 org.apache.xmlgraphics:batik-=
transcoder:jar:1.9</div><div>=C2=A0 =C2=A0 org.apache.xmlgraphics:batik-uti=
l:jar:1.9</div><div>=C2=A0 =C2=A0 org.apache.xmlgraphics:batik-xml:jar:1.9<=
/div><div>=C2=A0 =C2=A0 org.apache.xmlgraphics:xmlgraphics-commons:jar:2.2<=
/div><div>=C2=A0 =C2=A0 xalan:serializer:jar:2.7.2</div><div>=C2=A0 =C2=A0 =
xalan:xalan:jar:2.7.2</div><div>=C2=A0 =C2=A0 xml-apis:xml-apis-ext:jar:1.3=
.04</div><div>=C2=A0 =C2=A0 xml-apis:xml-apis:jar:1.3.04</div></div><div><b=
r></div><div>Does anyone see a problem with my code?</div><div>-Al</div>-- =
<br><div class=3D"gmail_signature">Please forgive typo&#39;s, equally likel=
y to have been typed on cell phone and to have been typed one-handed.<div><=
br></div></div>
</div></div>

--94eb2c18fe46c903b40554fce58b--