当前位置: 首页 > 面试题库 >

如何将SVG图像添加到使用HTML和Flying Saucer库(和Batik)构建的PDF中?

濮俊美
2023-03-14
问题内容

我正在使用飞碟库(旧但开源)使用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, 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等,如指南中所述。

编辑:清洁代码

编辑:添加了更多我尝试过的


问题答案:

我不知道为什么,但是替换SVGReplacedElement.paint(…)中的代码修复了它。

新代码:

@Override
public void paint(RenderingContext renderingContext, ITextOutputDevice outputDevice, 
        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, 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.LEFT);
    float y = (page.getBottom() - (blockBox.getAbsY() + cssHeight)) + page.getMarginBorderPadding(
            renderingContext, CalculatedStyle.BOTTOM);
    x /= outputDevice.getDotsPerPoint(); 
    y /= outputDevice.getDotsPerPoint();

    cb.addTemplate(template, x, y);
}

教程中得到的。

可能与我创建的未链接到原始文档的新UserAgent和DocumentLoader等有关。无论如何,现在都可以使用。希望它将对将来的人有所帮助。如果人们想评论或添加其他答案以说明为什么现在可行,这可能会帮助其他人稍后阅读。



 类似资料:
  • 我正在使用飞碟库(旧但开源)使用XHTML生成PDF。我已经开始工作,但我也想添加SVG图像。我已经开始努力集成蜡染,试图让它工作,但我遇到了问题。SVG图像没有绘制。XHTML仍然渲染,但它似乎没有显示SVG。我已经让SVG在单独的PDF上渲染,但从未与飞碟结果一起渲染。我添加了通常的ReplacedElementFactory(它也适用于常规图像,但没有包含该代码)。唯一相关的方法(确实被调用

  • 问题内容: 我需要将SVG图形添加​​到PDF文件中。 使用iText7是否有可能? 使用iText5: 我在以下页面中发现了这一点: PdfPTable和PdfTemplate 有一种创建类似于Template的方法: 如何创建Graphics2D? 问题答案: 巧合的是,我们今天发布了SVG实现。我们目前尚不支持全部功能集,我们仍将在第二季度及以后的时间里进行开发,但是您已经可以使用它了。该工

  • 我正在尝试使用蜡染将 SVG 转换为 JPG。在SVG中,我正在使用外部谷歌字体,因此不幸的是,这失败了,并出现以下错误: 有没有办法解决这个问题,或者有一个简单的方法来扩展蜡染来支持这一点? 这是我使用的Java代码: 这是我的SVG文件: 谢谢,杰拉尔德

  • 当我试图使用pdfBox绘制png图像时,页面保持空白。有什么方法可以使用PDFBOX插入png图像吗?

  • 我正在使用iText生成Pdf。但当我试图在pdf中添加图像时, 我mage.get实例(新的URL(timetableResource.getImageUrl()));document.add(学校标志); 但我得到的错误是 HTTP状态500-服务器为URL返回了HTTP响应代码400:http://139.59.72.150:8080/sms/attachments/23/42/school

  • 我试图使用itext7将图像添加到我的pdf文档中,但我得到了DirectoryNotFoundExctive。我使用的是itextSharp,但itext7的情况并非如此,StackOverflow的一些类似问题向我展示了它是如何完成的,但它不会读取资产文件夹中的文件。这是我的代码: 当我使用按钮点击事件运行此代码时,它会抛出以下错误: