batik
Javascript will not execute with PNGEncoder
I am trying to use the PNGEncoder class in batik to convert svg files to png format. My code pretty much comes from the example given on the apache site. public void writePng(String pngPath) throws Exception{ String parentPath=FileUtils.getParentPath(pngPath); FileUtils.mkdirs(parentPath); PNGTranscoder t = new PNGTranscoder(); t.addTranscodingHint(PNGTranscoder.KEY_EXECUTE_ONLOAD,new Boolean(true)); FileInputStream iStream=new FileInputStream(svgPath); TranscoderInput input = new TranscoderInput(iStream); OutputStream ostream = new FileOutputStream(pngPath); TranscoderOutput output = new TranscoderOutput(ostream); t.transcode(input, output); ostream.flush(); ostream.close(); } An snippet of the input svg with embedded javascript is below <?xml version="1.0" encoding="UTF-8"?> <svg contentScriptType="text/ecmascript" height="600" version="1.2" width="800" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <defs/> <script type="text/ecmascript" xlink:href="file:/D:/dev/svn/deathstar/xfltoalsxml/src/js/helper_functions.js"/> <script type="text/ecmascript" xlink:href="file:/D:/dev/svn/deathstar/xfltoalsxml/src/js/textFlow.js"/> <text id="defid_text_1" transform="matrix(1 0 0 1 269.9 57)"/> <script type="text/ecmascript"><![CDATA[var textNode = document.getElementById('defid_text_1'); textNode.setAttributeNS(null,'x',0); textNode.setAttributeNS(null,'y',0); textNode.setAttributeNS(null,'font-size',18); textNode.setAttributeNS(null,'font-family','ArialMT'); document.documentElement.appendChild(textNode); textFlow('testdata abcd ',textNode,514.1,0,20,false); ]]></script> </svg> Whenever I try to run my writePng function I get the exception ava.lang.Exception: Unknown language: text/ecmascript at org.apache.batik.bridge.BridgeContext.getInterpreter(BridgeContext.java:575) at org.apache.batik.bridge.BaseScriptingEnvironment.getInterpreter(BaseScriptingEnvironment.java:289) at org.apache.batik.bridge.BaseScriptingEnvironment.loadScripts(BaseScriptingEnvironment.java:404) at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:214) at org.apache.batik.transcoder.image.ImageTranscoder.transcode(ImageTranscoder.java:92) at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:142) at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:156) at com.amered.xfltojs.app.FrameToSvg.writePng(FrameToSvg.java:156) at com.amered.xfltojs.app.DocData.generateImageData(DocData.java:172) The files display properly when using squiggle. I have checked everywhere I can think of and can find no explanation for why this is happening. Hopefully someone has some suggestions.
I was struggling with the same exception. For some reason, this worked for me: Instead of TranscoderInput input = new TranscoderInput(new FileReader("myfile.svg")); I use String file = new File("myfile.svg").toURI().toString(); TranscoderInput input = new TranscoderInput(file);
Related Links
Is it possible to set the id for SVG elements drawn with SVGGraphics2D
What is the purpose of CSSEngine class in batik css parser
how to find package org.w3c.dom.svg?
Javascript will not execute with PNGEncoder