使用 Batik 将 svg 转换为图像时出现 TranscoderException
Posted
技术标签:
【中文标题】使用 Batik 将 svg 转换为图像时出现 TranscoderException【英文标题】:TranscoderException when converting svg to image using Batik 【发布时间】:2021-02-13 23:17:02 【问题描述】:我正在尝试将svg
内容转换为pdf
或png
文件。我正在使用蜡染来做到这一点。我的代码如下:
Transcoder transcoder = new PDFTranscoder();
TranscoderOutput transcoderOutput = new TranscoderOutput(new FileOutputStream(new File(outputFilePath + "\\" + outputFileName + ".pdf")));
transcoder.transcode(transcoderInput, transcoderOutput);
但是,我得到以下异常:
Exception in thread "main" org.apache.batik.transcoder.TranscoderException: null
Enclosed Exception:
Content is not allowed in prolog.
at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:226)
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.we.poc.svgConverterdemo.conv.POC.main(POC.java:35)
Caused by: org.apache.batik.bridge.BridgeException: Content is not allowed in prolog.
at org.apache.batik.bridge.BridgeContext.getReferencedNode(BridgeContext.java:788)
at org.apache.batik.bridge.BridgeContext.getReferencedElement(BridgeContext.java:804)
at org.apache.batik.bridge.PaintServer.convertURIPaint(PaintServer.java:365)
at org.apache.batik.bridge.PaintServer.convertPaint(PaintServer.java:273)
at org.apache.batik.bridge.PaintServer.convertFillPaint(PaintServer.java:242)
at org.apache.batik.bridge.PaintServer.convertFillAndStroke(PaintServer.java:160)
at org.apache.batik.bridge.SVGShapeElementBridge.createShapePainter(SVGShapeElementBridge.java:117)
at org.apache.batik.bridge.SVGDecoratedShapeElementBridge.createFillStrokePainter(SVGDecoratedShapeElementBridge.java:58)
at org.apache.batik.bridge.SVGDecoratedShapeElementBridge.createShapePainter(SVGDecoratedShapeElementBridge.java:84)
at org.apache.batik.bridge.SVGShapeElementBridge.buildGraphicsNode(SVGShapeElementBridge.java:91)
at org.apache.batik.bridge.GVTBuilder.buildGraphicsNode(GVTBuilder.java:224)
at org.apache.batik.bridge.GVTBuilder.buildComposite(GVTBuilder.java:171)
at org.apache.batik.bridge.GVTBuilder.buildGraphicsNode(GVTBuilder.java:219)
at org.apache.batik.bridge.GVTBuilder.buildComposite(GVTBuilder.java:171)
at org.apache.batik.bridge.GVTBuilder.buildGraphicsNode(GVTBuilder.java:219)
at org.apache.batik.bridge.GVTBuilder.buildComposite(GVTBuilder.java:171)
at org.apache.batik.bridge.GVTBuilder.buildGraphicsNode(GVTBuilder.java:219)
at org.apache.batik.bridge.GVTBuilder.buildComposite(GVTBuilder.java:171)
at org.apache.batik.bridge.GVTBuilder.buildGraphicsNode(GVTBuilder.java:219)
at org.apache.batik.bridge.GVTBuilder.buildComposite(GVTBuilder.java:171)
at org.apache.batik.bridge.GVTBuilder.buildGraphicsNode(GVTBuilder.java:219)
at org.apache.batik.bridge.GVTBuilder.buildComposite(GVTBuilder.java:171)
at org.apache.batik.bridge.GVTBuilder.buildGraphicsNode(GVTBuilder.java:219)
at org.apache.batik.bridge.GVTBuilder.buildComposite(GVTBuilder.java:171)
at org.apache.batik.bridge.GVTBuilder.build(GVTBuilder.java:141)
at org.apache.batik.bridge.SVGImageElementBridge.createSVGImageNode(SVGImageElementBridge.java:697)
at org.apache.batik.bridge.SVGImageElementBridge.createImageGraphicsNode(SVGImageElementBridge.java:296)
at org.apache.batik.bridge.SVGImageElementBridge.buildImageGraphicsNode(SVGImageElementBridge.java:179)
at org.apache.batik.bridge.SVGImageElementBridge.createGraphicsNode(SVGImageElementBridge.java:121)
at org.apache.batik.bridge.GVTBuilder.buildGraphicsNode(GVTBuilder.java:213)
at org.apache.batik.bridge.GVTBuilder.buildComposite(GVTBuilder.java:171)
at org.apache.batik.bridge.GVTBuilder.buildGraphicsNode(GVTBuilder.java:219)
at org.apache.batik.bridge.GVTBuilder.buildComposite(GVTBuilder.java:171)
at org.apache.batik.bridge.GVTBuilder.build(GVTBuilder.java:82)
at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:208)
... 4 more
【问题讨论】:
您使用什么内容作为输入? 【参考方案1】:从您的堆栈跟踪来看,某些填充定义似乎引用了外部文件。仔细查看那个文件,确保它是valid XML:
在 org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:142) ... 原因: org.apache.batik.bridge.BridgeException:prolog 中不允许有内容。 在 org.apache.batik.bridge.BridgeContext.getReferencedNode(BridgeContext.java:788) 在 org.apache.batik.bridge.BridgeContext.getReferencedElement(BridgeContext.java:804) 在 org.apache.batik.bridge.PaintServer.convertURIPaint(PaintServer.java:365) 在 org.apache.batik.bridge.PaintServer.convertPaint(PaintServer.java:273) 在 org.apache.batik.bridge.PaintServer.convertFillPaint(PaintServer.java:242)
【讨论】:
以上是关于使用 Batik 将 svg 转换为图像时出现 TranscoderException的主要内容,如果未能解决你的问题,请参考以下文章
在 Firefox 中的画布上下文对象上使用 svg 调用 drawImage() 时出现问题