我添加了通常的ReplacedElementFactory(它也适用于常规图像,但还没有包含该代码).唯一相关的方法(确实被调用和所有内容)如下:
@OverrIDepublic 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,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 CSSHeight) { this.CSSWIDth = CSSWIDth; this.CSSHeight = CSSHeight; this.svg = importednode;}@OverrIDeMethods....@OverrIDepublic 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来显示:块等,如指南中所述.
编辑:清洁代码
编辑:添加了更多关于我尝试过的内容
解决方法 我不知道为什么,但更换SVGReplacedElement.paint(…)中的代码修复它.新代码:
@OverrIDepublic voID paint(RenderingContext renderingContext,BlockBox blockBox) { PdfcontentByte cb = outputDevice.getWriter().getDirectContent(); float wIDth = (float) (CSSWIDth / outputDevice.getDotsPerPoint()); float height = (float) (CSSHeight / outputDevice.getDotsPerPoint()); pdfTemplate template = cb.createTemplate(wIDth,height); Graphics2D g2d = template.createGraphics(wIDth,height); PrintTranscoder prm = new PrintTranscoder(); Transcoderinput ti = new Transcoderinput(svg); prm.transcode(ti,null); PageFormat pg = new PageFormat(); Paper pp = new Paper(); pp.setSize(wIDth,height); pp.setimageableArea(0,wIDth,height); pg.setPaper(pp); prm.print(g2d,pg,0); g2d.dispose(); PageBox page = renderingContext.getPage(); float x = blockBox.getAbsX() + page.getmarginborderpadding(renderingContext,CalculatedStyle.BottOM); x /= outputDevice.getDotsPerPoint(); y /= outputDevice.getDotsPerPoint(); cb.addTemplate(template,y);}
得到它:http://www.samuelrossille.com/posts/2013-08-13-render-html-with-svg-to-pdf-with-flying-saucer.html
可能与我创建的没有链接到原始文档的新UserAgent和documentLoader等有关.无论如何它现在都有效.希望它能帮助将来的某个人.如果人们想要评论或重新说明为什么现在这样做,这可能有助于其他人稍后阅读.
总结以上是内存溢出为你收集整理的如何将SVG图像添加到使用HTML和Flying Saucer库(和Batik)构建的PDF?全部内容,希望文章能够帮你解决如何将SVG图像添加到使用HTML和Flying Saucer库(和Batik)构建的PDF?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)