Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于PDF(Java、蜡染、飞碟)的高效SVG渲染_Java_Performance_Memory_Svg_Pdf Generation - Fatal编程技术网

用于PDF(Java、蜡染、飞碟)的高效SVG渲染

用于PDF(Java、蜡染、飞碟)的高效SVG渲染,java,performance,memory,svg,pdf-generation,Java,Performance,Memory,Svg,Pdf Generation,我正在用XHTML和飞碟渲染PDF。我还添加了SVG图像(图标等)。然而,当我尝试绘制大量图像(如5000+时),渲染需要很长时间(显然)。只有10个左右不同的图像可供绘制,但只需重复多次(相同大小) 有没有一种方法/库可以有效地做到这一点 目前正在使用蜡染、飞碟组合来绘制图像。以下代码用于解析xhtml并查找img标记以放置SVG图像: @Override public ReplacedElement createReplacedElement(LayoutContext layoutCont

我正在用XHTML和飞碟渲染PDF。我还添加了SVG图像(图标等)。然而,当我尝试绘制大量图像(如5000+时),渲染需要很长时间(显然)。只有10个左右不同的图像可供绘制,但只需重复多次(相同大小)

有没有一种方法/库可以有效地做到这一点

目前正在使用蜡染、飞碟组合来绘制图像。以下代码用于解析xhtml并查找img标记以放置SVG图像:

@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(element.getAttribute("src")).toURL().toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        Element svgElement = svgImage.getDocumentElement();
        element.appendChild(element.getOwnerDocument().importNode(svgElement, true));
        return new SVGReplacedElement(svgImage, cssWidth, cssHeight);
    }
    return this.superFactory.createReplacedElement(layoutContext, blockBox, userAgentCallback, cssWidth, cssHeight);
}
为了绘制我使用的图像:

    @Override
public void paint(RenderingContext renderingContext, ITextOutputDevice outputDevice, 
        BlockBox blockBox) {

    PdfContentByte cb = outputDevice.getWriter().getDirectContent();
    float width = cssWidth / outputDevice.getDotsPerPoint();
    float height = 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);
}
缩放的想法。在i5 8gb RAM上,100个图像需要2秒,5000个图像需要42秒


那么,有没有一种方法可以将绘制的SVG存储在内存中,并更快地粘贴它呢?因为现在它似乎把所有的图像都当作单独的图像,吃掉了我所有的记忆,而且永远都不会消失。

通过做两件事来优化内存和速度。 我在
createReplacedElement
方法中预先生成了SVGDocuments,这使它的速度提高了一点。 主要改进是为所有图像预生成所有pdfTemplates。这大大提高了速度,因为模板已经包含渲染图像。 所有常规文本的呈现仍然很慢,因此我可能会关闭DPI

编辑:进一步优化请参见