Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/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
Java 飞碟-iTextPDF非常慢_Java_Pdf_Html Parsing_Flying Saucer - Fatal编程技术网

Java 飞碟-iTextPDF非常慢

Java 飞碟-iTextPDF非常慢,java,pdf,html-parsing,flying-saucer,Java,Pdf,Html Parsing,Flying Saucer,我正在尝试使用iText和飞碟从Html生成PDF。我使用了上面建议的不同技术来快速启用DocumentBuilder解析 然而,itextreadercreatepdf(outputstream)已经成为一个瓶颈。这是非常缓慢的,我不知道如何提高过程的速度 任何帮助都将不胜感激 private Document getDocument(String htmlContent) throws Exception { DocumentBuilderFactory factory = Docu

我正在尝试使用iText和飞碟从Html生成PDF。我使用了上面建议的不同技术来快速启用
DocumentBuilder
解析

然而,
itextreadercreatepdf(outputstream)
已经成为一个瓶颈。这是非常缓慢的,我不知道如何提高过程的速度

任何帮助都将不胜感激

private Document getDocument(String htmlContent) throws Exception
{
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    //For faster document.
    factory.setNamespaceAware(false);
    factory.setValidating(false);
    factory.setFeature("http://xml.org/sax/features/namespaces", false);
    factory.setFeature("http://xml.org/sax/features/validation", false);
    factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    builder.setEntityResolver(FSEntityResolver.instance());
    return builder.parse(new ByteArrayInputStream(htmlContent.getBytes()));
}

public void printHtmlToPdf(final String htmlContent, String tempFile, String title) throws Exception
{
    Document document =  getDocument(htmlContent);
    ITextRenderer renderer = new ITextRenderer();
    renderer.setDocument(document, null);
    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(tempFile)); 
    renderer.layout();
    renderer.createPDF(outputStream);
    outputStream.close();
}

这可能只是一个缓冲问题。如果您还没有这样做,请尝试使用
BufferedOutputStream
包装输出流,并将其用作
createPdf
参数。

我也遇到了同样的问题,PDF创建非常慢,
没有得到解决

这是因为没有使用HTTP代理,我猜有很多“HTTP://”超时

在我的情况下,解决办法是

System.setProperty("java.net.useSystemProxies", "true");

下面的一段应该可以解决这个问题

package com.pdf.web;

import com.lowagie.text.DocumentException;
import java.io.*;
import org.xhtmlrenderer.layout.SharedContext;
import org.xhtmlrenderer.pdf.ITextRenderer;
import org.xhtmlrenderer.resource.XMLResource;
import org.xml.sax.InputSource;

// Referenced classes of package com.pdf.web:
//            PDFRender

public class createPDF
{

    public createPDF()
    {
    }

    public createPDF(String url, String pdf)
        throws IOException, DocumentException
    {
        OutputStream os;
        os = null;
        os = new FileOutputStream(pdf);
        ITextRenderer renderer = new ITextRenderer();
        PDFRender.ResourceLoaderUserAgent callback = new PDFRender.ResourceLoaderUserAgent(renderer.getOutputDevice());
        callback.setSharedContext(renderer.getSharedContext());
        renderer.getSharedContext().setUserAgentCallback(callback);
        org.w3c.dom.Document doc = XMLResource.load(new InputSource(url)).getDocument();
        renderer.setDocument(doc, url);
        renderer.layout();
        renderer.createPDF(os);
        os.close();
        os = null;
        if(os != null)
        {
            try
            {
                os.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }
        break MISSING_BLOCK_LABEL_143;
        Exception exception;
        exception;
        if(os != null)
        {
            try
            {
                os.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }
        throw exception;
    }
}

BufferedOutputStream对我帮助不大。我已将我的代码添加到问题中。我没有任何其他建议。。。除了“profile it”。这个问题的答案可能会有帮助:在我的情况下,加载更多的css(例如:bootstrap.css)和图像会导致layout()任务变慢。你找到解决方案了吗?谢谢你的建议。但是,我没有图像(或任何其他必须从web外部获取的资源)。