我正在使用飞碟库(旧但开源)使用 XHTML 生成 PDF。我已经成功了,但我还想添加 SVG 图像。我已经开始致力于集成蜡染以尝试使其正常工作,但我遇到了问题。不绘制 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, userAgentCallback, cssWidth, cssHeight);
}
之后我尝试用以下方法绘制它:
import java.awt.Graphics2D;
import java.awt.Point;
import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.bridge.DocumentLoader;
import org.apache.batik.bridge.GVTBuilder;
import org.apache.batik.bridge.UserAgent;
import org.apache.batik.bridge.UserAgentAdapter;
import org.apache.batik.gvt.GraphicsNode;
import org.w3c.dom.svg.SVGDocument;
import org.xhtmlrenderer.css.style.CalculatedStyle;
import org.xhtmlrenderer.layout.LayoutContext;
import org.xhtmlrenderer.pdf.ITextOutputDevice;
import org.xhtmlrenderer.pdf.ITextReplacedElement;
import org.xhtmlrenderer.render.BlockBox;
import org.xhtmlrenderer.render.PageBox;
import org.xhtmlrenderer.render.RenderingContext;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
public class SVGReplacedElement implements ITextReplacedElement {
private Point location = new Point(0, 0);
private SVGDocument svg;
private int cssWidth;
private int cssHeight;
public SVGReplacedElement(SVGDocument importedNode, int cssWidth, int cssHeight) {
this.cssWidth = cssWidth;
this.cssHeight = cssHeight;
this.svg = importedNode;
}
@Override
Methods....
@Override
public void paint(RenderingContext renderingContext, ITextOutputDevice outputDevice,
BlockBox blockBox) {
UserAgent userAgent = new UserAgentAdapter();
DocumentLoader loader = new DocumentLoader(userAgent);
BridgeContext ctx = new BridgeContext(userAgent, loader);
ctx.setDynamicState(BridgeContext.DYNAMIC);
GVTBuilder builder = new GVTBuilder();
blockBox.paintDebugOutline(renderingContext);
PdfContentByte cb = outputDevice.getWriter().getDirectContent();
float width = cssWidth / outputDevice.getDotsPerPoint();
float height = cssHeight / outputDevice.getDotsPerPoint();
PdfTemplate map = cb.createTemplate(width, height);
Graphics2D g2d = map.createGraphics(width, height);
GraphicsNode mapGraphics = builder.build(ctx, svg);
mapGraphics.paint(g2d);
g2d.dispose();
PageBox page = renderingContext.getPage();
float x = blockBox.getAbsX() + page.getMarginBorderPadding(renderingContext, CalculatedStyle.LEFT);
float y = (page.getBottom() - (blockBox.getAbsY() + cssHeight)) + page.getMarginBorderPadding(
renderingContext, CalculatedStyle.BOTTOM);
cb.addTemplate(map, x, y);
}
}
有趣的是,blockBox.paintDebugOutline(renderingContext);
确实绘制了图像应该在的位置的轮廓。 Eclipse 调试还显示正确的文件已连接到 IMG 元素。
CSS 如下所示:
.header {
position: absolute;
display: inline-block;
right: 0;
top: 0;
width: 150px;
height: 54px;
}
我也尝试过display:block;
。我尝试过的xhtml示例:
<img class='header' src='icon.svg' alt='Logo'/>
<svg class='header' type='image/svg+xml' data='icon.svg' />
<object class='header' type='image/svg+xml' data='icon.svg' />
非常感谢您的关注和反馈(以及可能的答案)
编辑:最初的问题略有不同,但我已经解决了。 SVGImage 无法附加到实际文档中。现在它只是不画。我已将 CSS 添加到 display:block 等,如指南中所述。
编辑:更清晰的代码
编辑:添加了更多我尝试过的内容