I have a SVG file, structured like this:
<svg xmlns="http://www.w3.org/2000/svg" width="506px" height="261px" viewBox="0 0 506 261" style="overflow: hidden; display: block; width: 506px; height: 261px;">
<defs>
<path fill="none" stroke="rgb(211,211,211)" d="M 5,-5 L 0,0 L 5,5" stroke-opacity="1" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="10" id="ygc10_0" />
<path fill="none" stroke="rgb(211,211,211)" d="M 5,-5 L 0,0 L 5,5" stroke-opacity="1" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="10" id="ygc10_1" />
<path fill="none" stroke="rgb(211,211,211)" d="M 5,-5 L 0,0 L 5,5" stroke-opacity="1" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="10" id="ygc10_2" />
<path fill="none" stroke="rgb(211,211,211)" d="M 5,-5 L 0,0 L 5,5" stroke-opacity="1" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="10" id="ygc10_3" />
<path fill="none" stroke="rgb(211,211,211)" d="M 5,-5 L 0,0 L 5,5" stroke-opacity="1" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="10" id="ygc10_4" />
</defs>
<g style="pointer-events:visiblePainted" transform="translate(-57.822952932363535 -41.05614567526554)" image-rendering="auto" shape-rendering="auto">
<g>
<path fill="none" stroke="rgb(211,211,211)" d="M 257.9119715596548,73.05614567526554 L 257.9119715596548,135.06828972638465" stroke-opacity="1" stroke-width="2" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="10" />
<use href="#ygc10_0" transform="matrix(0 -2 2 0 257.912 135.068)" style="pointer-events: none;" />
</g>
<g>
<path fill="none" stroke="rgb(211,211,211)" d="M 107.82295293236353,207.4377203121161 L 107.82295293236353,261.03014731718906" stroke-opacity="1" stroke-width="2" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="10" />
<use href="#ygc10_1" transform="matrix(0 -2 2 0 107.823 261.03)" style="pointer-events: none;" />
</g>
...
And I am trying to generate a PNG from its ByteArrayInputStream
, like this (please, don't consider the lack of exceptions' handling for now):
static ByteArrayOutputStream svgToPng(ByteArrayInputStream streamBytes) throws TranscoderException, IOException {
PNGTranscoder t = new PNGTranscoder();
TranscoderInput input = new TranscoderInput(streamBytes);
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
TranscoderOutput output = new TranscoderOutput(ostream);
t.transcode(input, output);
ostream.flush();
return ostream;
}
However, I get the Exception on the transcode()
's invocation:
org.apache.batik.bridge.BridgeException: null:-1
The attribute "xlink:href" of the element <use> is required
at org.apache.batik.bridge.SVGUseElementBridge.buildCompositeGraphicsNode(Unknown Source)
at org.apache.batik.bridge.SVGUseElementBridge.createGraphicsNode(Unknown Source)
at org.apache.batik.bridge.GVTBuilder.buildGraphicsNode(Unknown Source)
at org.apache.batik.bridge.GVTBuilder.buildComposite(Unknown Source)
at org.apache.batik.bridge.GVTBuilder.buildGraphicsNode(Unknown Source)
at org.apache.batik.bridge.GVTBuilder.buildComposite(Unknown Source)
...
Can you please help me? I also try, after my PNGTranscoder
declaration:
t.addTranscodingHint(SVGAbstractTranscoder.KEY_DOCUMENT_ELEMENT_NAMESPACE_URI,
SVGDOMImplementation.SVG_NAMESPACE_URI);
But with no effect. Thank you all.
Your SVG file is invalid, one way to fix it would be to add an xmlns:xlink attribute on the root SVG element like so...
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
width="506px" height="261px" viewBox="0 0 506 261">
As Paul points out you also need to change the href attributes to xlink:href as href is SVG 2 and Batik does not support that yet.