gpt4 book ai didi

java - 使用 iText 将 HTML 转换为 PDF 时出错

转载 作者:行者123 更新时间:2023-11-30 05:04:05 24 4
gpt4 key购买 nike

我正在尝试从 HTML 文档生成 PDF 文件。

HTML 文件格式正确且没有错误。我使用 HtmlCleaner 来清理代码,因此它适合使用 iText 创建 PDF 文件。

这是我在 HTML 示例中使用的代码。

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.html.simpleparser.HTMLWorker;
import com.itextpdf.text.pdf.PdfWriter;


public class pdfIng {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {

com.itextpdf.text.Document document = new com.itextpdf.text.Document(PageSize.A4);
PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("D://testpdf.pdf"));
document.open();
document.addAuthor("Author of the Doc");
document.addCreator("Creator of the Doc");
document.addSubject("Subject of the Doc");
document.addCreationDate();
document.addTitle("This is the title");

//SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
//SAXmyHtmlHandler shh = new SAXmyHtmlHandler(document);

HTMLWorker htmlWorker = new HTMLWorker(document);
String str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+ " <html> <head /> <body> " +
"<h2>Text</h2> " +
" Here, you will learn how to retrieve all rows from a " +
"database table. You know that table contains the data in " +
"rows and columns format. If you want to access the data from" +
" a table then you need to use some APIs and methods. See brief " +
"descriptions for retrieving all rows from a database table as below: " +
" Description of program: Program establishes the connection " +
"between MySQL database and java file so that the we can retrieve " +
"all data from a specific database table. If any exception occurs " +
"then shows a message SQL code does not execute. " +
"<br /> <br /> <hr /> <br /> " +
"<b>Name</b> " +
"AAAAAA AAAAAAAAA <br /> <b>Date :" +
"</b> 17/04/2011 00:31:18 <br /> <b>Text:" +
"</b> <br /> gggggggggggggg <br /> <br /> " +
" <br /> " +
"<br /> <b>Name</b> " +
" BBBBBB BBBBBBBBB <br /> <b>Date " +
":</b> 17/04/2011 00:35:37 <br /> <b>Text:</b>" +
" <br /> gftgfgfgfgfgggfgf gggggg" +
" <br /> <br /> " +
" <br /> <br /> <b>Name</b> " +
"DDDDDD DDDDDDDDD <br /> <b>Date :</b> " +
" 16/04/2011 22:28:28 <br /> <b>Text:</b> " +
"<br /> w tawa! <br /> <br /> " +
" <br /> <br /> <b>Name</b> " +
"CCCCCC CCCCCCCCC <br /> <b>Date :</b> " +
"16/04/2011 22:37:08 <br /> <b>Text:</b> " +
"<br /> ched tawa!!! <br /> <br /> " +
" <br /> " +
" <br /> <b>Name</b> " +
"BBBBBB BBBBBBBBB <br /> <b>Date :</b> " +
" 16/04/2011 22:37:26 <br /> <b>Text:</b> " +
"<br /> okiiiiii! <br /> <br /> " +
" " +
" <br /> <br /> <b>Name</b> " +
" AAAAAA AAAAAAAAA <br /> <b>Date :</b> " +
" 17/04/2011 02:41:14 <br /> <b>Text:</b> " +
" <br /> cava hakka?? " +
" <br /> <br /> " +
" <br /> </body></html> ";
System.out.println(str);
htmlWorker.parse(new StringReader(str));

document.close();

} catch(DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}

和输出

Exception in thread "main" java.lang.NullPointerException
at com.itextpdf.text.html.simpleparser.HTMLWorker.createLineSeparator(HTMLWorker.java:435)
at com.itextpdf.text.html.simpleparser.HTMLTagProcessors$5.startElement(HTMLTagProcessors.java:208)
at com.itextpdf.text.html.simpleparser.HTMLWorker.startElement(HTMLWorker.java:189)
at com.itextpdf.text.xml.simpleparser.SimpleXMLParser.processTag(SimpleXMLParser.java:566)
at com.itextpdf.text.xml.simpleparser.SimpleXMLParser.go(SimpleXMLParser.java:340)
at com.itextpdf.text.xml.simpleparser.SimpleXMLParser.parse(SimpleXMLParser.java:592)
at com.itextpdf.text.html.simpleparser.HTMLWorker.parse(HTMLWorker.java:143)
at pdfIng.main(pdfIng.java:78)

我一开始以为这行"<?xml version=\"1.0\" encoding=\"utf-8\"?>"导致错误,但事实并非如此。

我在 str 字符串中搜索了是否有导致错误的字符,但在我看来所有单词都是正常且健康的,我无法消除任何一个。

预先感谢您的帮助! :)

最佳答案

我发现错误了!这是HR标签!!在 iText 网站上我发现了这个:

删除旧的类/功能;这可能会导致您的应用程序崩溃,但您不应该使用任何这些过时的类,因此这些问题很可能不会出现问题。如果您确实遇到问题,请按照以下说明操作:

  • class Graphic:如果您仍在使用它:请使用直接内容和/或 PdfTemplate。待办事项:the <hr> tag doesn't work anymore in the XML parser .

所以我必须找到其他东西来代替 HR 标签!

关于java - 使用 iText 将 HTML 转换为 PDF 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5703578/

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