Re: Help with transcoding SVG to PNG Batik 1.9

Peter Coppens <[email protected]> Sun, 23 Jul 2017 16:43:37 +0200
Newsgroups gmane.text.xml.batik.user
Message-ID <[email protected]>
--Apple-Mail=_2151AF30-6FF0-42B4-BEED-EB280FE9B0BC
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=us-ascii

Perhaps https://bz.apache.org/bugzilla/show_bug.cgi?id=3D44682 =
<https://bz.apache.org/bugzilla/show_bug.cgi?id=3D44682> is the same ?

Try adding

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

Hth

Peter


> On 23 Jul 2017, at 16:27, Al Pacifico <[email protected]> wrote:
>=20
> Complete newbie to Batik here, so bear with me.
>=20
> I am trying to transcode SVG's to PNG's using the following code:
> package com.example;
>=20
> 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;
>=20
> public class Main {
>=20
>     public static void main(String [] args) throws Exception {
>=20
>         // read the input SVG document into TranscoderInput
>         String svgURI =3D Paths.get(args[0]).toUri().toString();
>         TranscoderInput input =3D new TranscoderInput(svgURI);
>         // define OutputStream to PNG Image and attach to =
TranscoderOutput
>         OutputStream ostream =3D new FileOutputStream("out.png");
>         TranscoderOutput output =3D new TranscoderOutput(ostream);
>         // create a JPEG transcoder
>         PNGTranscoder t =3D 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.=20
>=20
> 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(ImageTranscode=
r.java:132)
> 	at =
org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTra=
nscoder.java:142)
> 	at =
org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTra=
nscoder.java:156)
> 	at com.example.Main.main(Main.java:26)
>=20
> 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.
>=20
> Using the technique for listing dependencies outlined at =
https://grep.codeconsult.ch/2010/07/08/list-all-your-maven-dependencies/ =
<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
>=20
> Does anyone see a problem with my code?
> -Al
> --=20
> Please forgive typo's, equally likely to have been typed on cell phone =
and to have been typed one-handed.
>=20


--Apple-Mail=_2151AF30-6FF0-42B4-BEED-EB280FE9B0BC
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=us-ascii

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html =
charset=3Dus-ascii"></head><body style=3D"word-wrap: break-word; =
-webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" =
class=3D"">Perhaps&nbsp;<a =
href=3D"https://bz.apache.org/bugzilla/show_bug.cgi?id=3D44682" =
class=3D"">https://bz.apache.org/bugzilla/show_bug.cgi?id=3D44682</a>&nbsp=
;is the same ?<div class=3D""><br class=3D""></div><div class=3D"">Try =
adding</div><div class=3D""><br class=3D""></div><div class=3D""><div =
class=3D"">&lt;dependency&gt;</div><div class=3D"">&nbsp; &nbsp; =
&lt;groupId&gt;org.apache.xmlgraphics&lt;/groupId&gt;</div><div =
class=3D"">&nbsp; &nbsp; =
&lt;artifactId&gt;batik-codec&lt;/artifactId&gt;</div><div =
class=3D"">&nbsp; &nbsp; &lt;version&gt;1.9&lt;/version&gt;</div><div =
class=3D"">&lt;/dependency&gt;</div><div class=3D""><br =
class=3D""></div><div class=3D"">Hth</div><div class=3D""><br =
class=3D""></div><div class=3D"">Peter</div><div class=3D""><br =
class=3D""></div><div class=3D""><br class=3D""><div><blockquote =
type=3D"cite" class=3D""><div class=3D"">On 23 Jul 2017, at 16:27, Al =
Pacifico &lt;<a href=3D"mailto:[email protected]" =
class=3D"">[email protected]</a>&gt; wrote:</div><br =
class=3D"Apple-interchange-newline"><div class=3D""><div dir=3D"ltr" =
class=3D"">Complete newbie to Batik here, so bear with me.<div =
class=3D""><br class=3D""></div><div class=3D"">I am trying to transcode =
SVG's to PNG's using the following code:</div><div class=3D""><pre =
style=3D"font-family: 'DejaVu Sans Mono'; font-size: 11.3pt;" =
class=3D""><span style=3D"color:rgb(0,0,128);font-weight:bold" =
class=3D"">package </span>com.example;<br class=3D""><br class=3D""><span =
style=3D"color:rgb(0,0,128);font-weight:bold" class=3D"">import =
</span><a href=3D"http://java.io" class=3D"">java.io</a>.*;<br =
class=3D""><span style=3D"color:rgb(0,0,128);font-weight:bold" =
class=3D"">import </span>java.nio.file.Paths;<br class=3D""><span =
style=3D"color:rgb(0,0,128);font-weight:bold" class=3D"">import =
</span>org.apache.batik.transcoder.image.PNGTranscoder;<br =
class=3D""><span style=3D"color:rgb(0,0,128);font-weight:bold" =
class=3D"">import =
</span>org.apache.batik.transcoder.SVGAbstractTranscoder;<br =
class=3D""><span style=3D"color:rgb(0,0,128);font-weight:bold" =
class=3D"">import </span>org.apache.batik.transcoder.TranscoderInput;<br =
class=3D""><span style=3D"color:rgb(0,0,128);font-weight:bold" =
class=3D"">import =
</span>org.apache.batik.transcoder.TranscoderOutput;<br class=3D""><br =
class=3D""><span style=3D"color:rgb(0,0,128);font-weight:bold" =
class=3D"">public class </span>Main {<br class=3D""><br class=3D"">    =
<span style=3D"color:rgb(0,0,128);font-weight:bold" class=3D"">public =
static void </span>main(String [] args) <span =
style=3D"color:rgb(0,0,128);font-weight:bold" class=3D"">throws =
</span>Exception {<br class=3D""><br class=3D"">        <span =
style=3D"color:rgb(128,128,128);font-style:italic" class=3D"">// read =
the input SVG document into TranscoderInput<br class=3D""></span><span =
style=3D"color:rgb(128,128,128);font-style:italic" class=3D"">        =
</span>String svgURI =3D Paths.<span style=3D"font-style:italic" =
class=3D"">get</span>(args[<span style=3D"color:rgb(0,0,255)" =
class=3D"">0</span>]).toUri().toString();<br class=3D"">        =
TranscoderInput input =3D <span =
style=3D"color:rgb(0,0,128);font-weight:bold" class=3D"">new =
</span>TranscoderInput(svgURI);<br class=3D"">        <span =
style=3D"color:rgb(128,128,128);font-style:italic" class=3D"">// define =
OutputStream to PNG Image and attach to TranscoderOutput<br =
class=3D""></span><span style=3D"color:rgb(128,128,128);font-style:italic"=
 class=3D"">        </span>OutputStream ostream =3D <span =
style=3D"color:rgb(0,0,128);font-weight:bold" class=3D"">new =
</span>FileOutputStream(<span =
style=3D"color:rgb(0,128,0);font-weight:bold" =
class=3D"">"out.png"</span>);<br class=3D"">        TranscoderOutput =
output =3D <span style=3D"color:rgb(0,0,128);font-weight:bold" =
class=3D"">new </span>TranscoderOutput(ostream);<br class=3D"">        =
<span style=3D"color:rgb(128,128,128);font-style:italic" class=3D"">// =
create a JPEG transcoder<br class=3D""></span><span =
style=3D"color:rgb(128,128,128);font-style:italic" class=3D"">        =
</span>PNGTranscoder t =3D <span =
style=3D"color:rgb(0,0,128);font-weight:bold" class=3D"">new =
</span>PNGTranscoder();<br class=3D"">        <span =
style=3D"color:rgb(128,128,128);font-style:italic" class=3D"">// set the =
transcoding hints<br class=3D""></span><span =
style=3D"color:rgb(128,128,128);font-style:italic" class=3D"">        =
</span>t.addTranscodingHint(SVGAbstractTranscoder.<span =
style=3D"color:rgb(102,14,122);font-weight:bold;font-style:italic" =
class=3D"">KEY_HEIGHT</span>, <span =
style=3D"color:rgb(0,0,128);font-weight:bold" class=3D"">new =
</span>Float(<span style=3D"color:rgb(0,0,255)" =
class=3D"">600</span>));<br class=3D"">        =
t.addTranscodingHint(SVGAbstractTranscoder.<span =
style=3D"color:rgb(102,14,122);font-weight:bold;font-style:italic" =
class=3D"">KEY_WIDTH</span>, <span =
style=3D"color:rgb(0,0,128);font-weight:bold" class=3D"">new =
</span>Float(<span style=3D"color:rgb(0,0,255)" =
class=3D"">600</span>));<br class=3D"">        <span =
style=3D"color:rgb(128,128,128);font-style:italic" class=3D"">// convert =
and write output<br class=3D""></span><span =
style=3D"color:rgb(128,128,128);font-style:italic" class=3D"">        =
</span>t.transcode(input, output);<br class=3D"">        <span =
style=3D"color:rgb(128,128,128);font-style:italic" class=3D"">// flush =
and close the stream then exit<br class=3D""></span><span =
style=3D"color:rgb(128,128,128);font-style:italic" class=3D"">        =
</span>ostream.flush();<br class=3D"">        ostream.close();<br =
class=3D"">    }<br class=3D"">}<br class=3D""></pre></div><div =
class=3D""><div class=3D"">Compiled using Maven, this code repeatedly =
fails (using a variety of SVG's)&nbsp;with the following =
exception.&nbsp;</div><div class=3D""><br class=3D""></div><div =
class=3D"">Exception in thread "main" =
org.apache.batik.transcoder.TranscoderException: null</div><div =
class=3D"">Enclosed Exception:</div><div class=3D"">Could not write PNG =
file because no WriteAdapter is availble</div><div class=3D""><span =
style=3D"white-space:pre" class=3D"">	</span>at =
org.apache.batik.transcoder.image.ImageTranscoder.transcode(ImageTranscode=
r.java:132)</div><div class=3D""><span style=3D"white-space:pre" =
class=3D"">	</span>at =
org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTra=
nscoder.java:142)</div><div class=3D""><span style=3D"white-space:pre" =
class=3D"">	</span>at =
org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTra=
nscoder.java:156)</div><div class=3D""><span style=3D"white-space:pre" =
class=3D"">	</span>at com.example.Main.main(Main.java:26)</div><div =
class=3D""><br class=3D""></div><div class=3D"">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.</div><div class=3D""><br class=3D""></div><div =
class=3D"">Using the technique for listing dependencies outlined at <a =
href=3D"https://grep.codeconsult.ch/2010/07/08/list-all-your-maven-depende=
ncies/" =
class=3D"">https://grep.codeconsult.ch/2010/07/08/list-all-your-maven-depe=
ndencies/</a>, I get the following list:<br class=3D""></div><div =
class=3D""><div class=3D"">&nbsp; &nbsp; =
commons-io:commons-io:jar:1.3.1</div><div class=3D"">&nbsp; &nbsp; =
commons-logging:commons-logging:jar:1.0.4</div><div class=3D"">&nbsp; =
&nbsp; junit:junit:jar:3.8.1</div><div class=3D"">&nbsp; &nbsp; =
org.apache.xmlgraphics:batik-anim:jar:1.9</div><div class=3D"">&nbsp; =
&nbsp; org.apache.xmlgraphics:batik-awt-util:jar:1.9</div><div =
class=3D"">&nbsp; &nbsp; =
org.apache.xmlgraphics:batik-bridge:jar:1.9</div><div class=3D"">&nbsp; =
&nbsp; org.apache.xmlgraphics:batik-constants:jar:1.9</div><div =
class=3D"">&nbsp; &nbsp; =
org.apache.xmlgraphics:batik-css:jar:1.9</div><div class=3D"">&nbsp; =
&nbsp; org.apache.xmlgraphics:batik-dom:jar:1.9</div><div =
class=3D"">&nbsp; &nbsp; =
org.apache.xmlgraphics:batik-ext:jar:1.9</div><div class=3D"">&nbsp; =
&nbsp; org.apache.xmlgraphics:batik-gvt:jar:1.9</div><div =
class=3D"">&nbsp; &nbsp; =
org.apache.xmlgraphics:batik-i18n:jar:1.9</div><div class=3D"">&nbsp; =
&nbsp; org.apache.xmlgraphics:batik-parser:jar:1.9</div><div =
class=3D"">&nbsp; &nbsp; =
org.apache.xmlgraphics:batik-script:jar:1.9</div><div class=3D"">&nbsp; =
&nbsp; org.apache.xmlgraphics:batik-svg-dom:jar:1.9</div><div =
class=3D"">&nbsp; &nbsp; =
org.apache.xmlgraphics:batik-svggen:jar:1.9</div><div class=3D"">&nbsp; =
&nbsp; org.apache.xmlgraphics:batik-transcoder:jar:1.9</div><div =
class=3D"">&nbsp; &nbsp; =
org.apache.xmlgraphics:batik-util:jar:1.9</div><div class=3D"">&nbsp; =
&nbsp; org.apache.xmlgraphics:batik-xml:jar:1.9</div><div =
class=3D"">&nbsp; &nbsp; =
org.apache.xmlgraphics:xmlgraphics-commons:jar:2.2</div><div =
class=3D"">&nbsp; &nbsp; xalan:serializer:jar:2.7.2</div><div =
class=3D"">&nbsp; &nbsp; xalan:xalan:jar:2.7.2</div><div class=3D"">&nbsp;=
 &nbsp; xml-apis:xml-apis-ext:jar:1.3.04</div><div class=3D"">&nbsp; =
&nbsp; xml-apis:xml-apis:jar:1.3.04</div></div><div class=3D""><br =
class=3D""></div><div class=3D"">Does anyone see a problem with my =
code?</div><div class=3D"">-Al</div>-- <br class=3D""><div =
class=3D"gmail_signature">Please forgive typo's, equally likely to have =
been typed on cell phone and to have been typed one-handed.<div =
class=3D""><br class=3D""></div></div>
</div></div>
</div></blockquote></div><br class=3D""></div></div></body></html>=

--Apple-Mail=_2151AF30-6FF0-42B4-BEED-EB280FE9B0BC--