我正在使用飞碟库(旧但开源)使用XHTML生成PDF。我可以正常工作,但我也想添加SVG图像。Ive开始着手整合蜡染以尝试使其正常工作,但我遇到了问题。未绘制SVG图像。XHTML仍会渲染,但似乎没有显示SVG。我已经将SVG渲染为单独的PDF,但从未与飞碟的结果一起使用。我添加了通常的ReplacedElementFactory(它也可用于常规图像,但尚未包含该代码)。唯一相关的方法(确实会调用所有方法)如下:
@Override
public ReplacedElement createReplacedElement(LayoutContext layoutContext, BlockBox blockBox, UserAgentCallback userAgentCallback, int cssWidth, int cssHeight) {
Element element = blockBox.getElement();
if (element == null) {
return null;
}
String nodeName = element.getNodeName();
if ("img".equals(nodeName)) {
SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(XMLResourceDescriptor.getXMLParserClassName());
SVGDocument svgImage = null;
try {
svgImage = factory.createSVGDocument(new File("logo.svg").toURL()
.toString());
} catch (IOException e) {
e.printStackTrace();
}
Element svgElement = svgImage.getDocumentElement();
Document htmlDoc = element.getOwnerDocument();
Node importedNode = htmlDoc.importNode(svgElement, true);
element.appendChild(importedNode);
return new SVGReplacedElement(svgImage, cssWidth, cssHeight);
}
return this.superFactory.createReplacedElement(layoutContext, blockBox, …Run Code Online (Sandbox Code Playgroud)