有没有办法提高FlyingSaucer的性能? [英] Is there any way improve the performance of FlyingSaucer?

查看:113
本文介绍了有没有办法提高FlyingSaucer的性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经跟着这篇文章使用FlyingSaucer将XHTML转换为PDF并且它很精彩,但有一个主要的垮台......它的速度非常慢!

I've followed this article to use FlyingSaucer to convert XHTML to PDF and it's brilliant but has one major downfall... it's ridiculously slow!

我发现从XHTML渲染PDF需要1到2分钟,无论该页面有多简单。

I'm finding that it takes between 1 and 2 minutes to render a PDF from an XHTML, regardless of how simple that page is.

基本代码:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.DocumentException;

public class FirstDoc {

    public static void main(String[] args) throws IOException, DocumentException {

        String inputFile = "firstdoc.xhtml";
        String url = new File(inputFile).toURI().toURL().toString();
        String outputFile = "firstdoc.pdf";
        OutputStream os = new FileOutputStream(outputFile);

        ITextRenderer renderer = new ITextRenderer();
        renderer.setDocument(url);
        renderer.layout();
        renderer.createPDF(os);

        os.close();
    }
}

示例XHTML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>My First Document</title>
        <style type="text/css"> b { color: green; } </style>
    </head>
    <body>
        <p>
            <b>Greetings Earthlings!</b>
            We've come for your Java.
        </p>
    </body>
</html>

有谁知道如何提高FlyingSaucer的性能?

Does anyone know how to improve the performance of FlyingSaucer?

如果不这样做,是否有人能够推荐一个替代的Java库,它有效地将PDF从URL渲染到带有外部CSS的(X)HTML文档和从URL生成的图像?

Failing that, is anyone able to recommend an alternative Java library which is effective at rendering a PDF from a URL to an (X)HTML document with external CSS and images generated from URLs?

推荐答案

首先我要说的是我使用了你的示例代码和示例xhtml,并且在2675ms内跑了。

Let me start by saying that I used your sample code and sample xhtml, and it "Ran in 2675ms".

我下载了flyingsaucer R8。并将三个罐放入我的类路径中。

I downloaded flyingsaucer R8. And put three of the jars into my classpath.

core-renderer.jar,iText-2.0.8.jar,xml-apis-xerces-2.9.1.jar

core-renderer.jar, iText-2.0.8.jar, xml-apis-xerces-2.9.1.jar

我通过使用工具修改代码来测量运行时间......

I measured the run time by modifying your code with instrumentation...

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.DocumentException;

public class FirstDoc {

    public static void main(String[] args) throws IOException, DocumentException {
        long start = System.currentTimeMillis();
        String inputFile = "firstdoc.xhtml";
        String url = new File(inputFile).toURI().toURL().toString();
        String outputFile = "firstdoc.pdf";
        OutputStream os = new FileOutputStream(outputFile);

        ITextRenderer renderer = new ITextRenderer();
        renderer.setDocument(url);
        renderer.layout();
        renderer.createPDF(os);

        os.close();
        long end = System.currentTimeMillis();
        System.out.println("Ran in " + (end-start) + "ms");
    }
}

现在这个库不是很快,但它似乎也没需要1-2分钟。所以现在我们需要弄清楚为什么它的运行速度这么慢。您能告诉我们您使用的JDK以及使用的平台吗?你还使用哪个版本的flyingsaucer?

Now this library isn't exactly speedy, but it doesn't seem to be taking 1-2 minutes either. So now we need to figure out why it's running so slowly for you. Could you please let us know which JDK your using and on what platform? Also which version of flyingsaucer are you using?

这篇关于有没有办法提高FlyingSaucer的性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆