gpt4 book ai didi

java - 错误 : 'Premature end of file.'

转载 作者:行者123 更新时间:2023-11-29 03:36:26 25 4
gpt4 key购买 nike

import java.io.*;
import com.lowagie.text.DocumentException;
import org.xhtmlrenderer.pdf.ITextRenderer;

public class FirstDoc {

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

String url= "http://www.facebook.com";

String outputFile = "firstdoc.pdf";
OutputStream os = new FileOutputStream(outputFile);

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

os.close();
}
}

但是我收到了这个错误

> ERROR:  'Premature end of file.'
Exception in thread "main" org.xhtmlrenderer.util.XRRuntimeException: Can't load the XML resource (using TRaX transformer). org.xml.sax.SAXParseException: Premature end of file.
at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource.java:191)
at org.xhtmlrenderer.resource.XMLResource.load(XMLResource.java:71)
at org.xhtmlrenderer.swing.NaiveUserAgent.getXMLResource(NaiveUserAgent.java:205)
at org.xhtmlrenderer.pdf.ITextRenderer.loadDocument(ITextRenderer.java:102)
at org.xhtmlrenderer.pdf.ITextRenderer.setDocument(ITextRenderer.java:106)
at com.test.java.FirstDoc.main(FirstDoc.java:17)

Caused by: javax.xml.transform.TransformerException: org.xml.sax.SAXParseException: Premature end of file.
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:720)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:313)
at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource.java:189)
... 5 more
Caused by: org.xml.sax.SAXParseException: Premature end of file.
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1231)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:637)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:708)
... 7 more

最佳答案

http://www.facebook.com 很可能不返回 HTML 文档,而是返回 HTTP 重定向或其他内容,ITextRenderer 可能不支持。

您应该通过使用一些不执行重定向的 URL 进行测试来验证这一点。

关于java - 错误 : 'Premature end of file.' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15288952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com