gpt4 book ai didi

java - 不从 http 网站 url 生成 pdf

转载 作者:行者123 更新时间:2023-12-01 05:32:19 25 4
gpt4 key购买 nike

我想使用servlt从url生成pdf我正在使用 Flying Saucer 从网站 url 生成 pdf我的代码是

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package abc;

import com.lowagie.text.DocumentException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.xhtmlrenderer.pdf.ITextRenderer;

public class TestPdf {
public static void main(String args[]) throws MalformedURLException
{
String File_To_Convert = "test.htm";
//String url = new File(File_To_Convert).toURI().toURL().toString();
URL testurl=new URL("http://www.ipillion.com/");
String url=testurl.toString();
System.out.println(""+url);
String HTML_TO_PDF = "d:\\ConvertedFile.pdf";
OutputStream os;
try {
os = new FileOutputStream(HTML_TO_PDF);


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

renderer.createPDF(os);

os.close();

}catch(Exception e)
{
e.printStackTrace();
}
}}

我尝试过使用不同的网站,但每次都会抛出异常例如

run:
http://www.ipillion.com/
ERROR: 'The element type "SCRIPT" must be terminated by the matching end-tag "</SCRIPT>".'
org.xhtmlrenderer.util.XRRuntimeException: Can't load the XML resource (using TRaX transformer). org.xml.sax.SAXParseException; lineNumber: 12; columnNumber: 71; The element type "SCRIPT" must be terminated by the matching end-tag "</SCRIPT>".
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:211)
at org.xhtmlrenderer.pdf.ITextRenderer.loadDocument(ITextRenderer.java:134)
at org.xhtmlrenderer.pdf.ITextRenderer.setDocument(ITextRenderer.java:138)
at abc.TestPdf.main(TestPdf.java:33)
Caused by: javax.xml.transform.TransformerException: org.xml.sax.SAXParseException; lineNumber: 12; columnNumber: 71; The element type "SCRIPT" must be terminated by the matching end-tag "</SCRIPT>".
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:723)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:317)
at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource.java:189)
... 5 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 12; columnNumber: 71; The element type "SCRIPT" must be terminated by the matching end-tag "</SCRIPT>".
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1236)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:640)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:711)
... 7 more
BUILD SUCCESSFUL (total time: 9 seconds)

最佳答案

您正在使用 XHTML 呈现器 (org.xhtmlrenderer.pdf.ITextRenderer),但该网站采用 HTML 格式。

关于java - 不从 http 网站 url 生成 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8778701/

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