如题,由于是通过jacob转的html,使用xmlworker,Flying Sauser都各种类似“'与元素类型 "link" 相关联的属性 "rel" 应有左引号。'”的不停报错,使用itext无法识别中文,而且图片路径不知道怎么设置,每次都默认在我D盘根目录找。
以下是通过itext转换的java代码,是否能修改后完成中文和图片路径的处理呢:
public static void main(String[] args) throws Exception {
Document document = new Document();
StyleSheet st = new StyleSheet();
st.loadTagStyle("body", "leading", "16,0");
PdfWriter.getInstance(document, new FileOutputStream("C:\Users\Administrator\Desktop\123.pdf"));
document.open();
List p = HTMLWorker.parseToList(new FileReader("C:\Users\Administrator\Desktop\123.html"), st);
for (int k = 0; k < p.size(); ++k)
document.add((Element) p.get(k));
System.out.println("complete");
document.close();
}
以下是jacob转的html的前面部分,有什么好的办法让xmlworker,Flying Sauser能识别jacob转的文件,或者让jacob转的html变得“规范”呢。
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns:m="http://schemas.microsoft.com/office/2004/12/omml"
xmlns="http://www.w3.org/TR/REC-html40">
<!--[if !mso]>
HTMLWorker.parseToList(new FileReader("C:\\Users\\Administrator\\Desktop\\123.html")
这是什么东西,目测你的这个html本身有问题,比如css丢失,或者它不规范。itext本身没有什么问题,也支持中文。
document.add((Element) p.get(k));
这一步得到的有乱码么
网上有itext代码创建中文字体,但不知道如何修改这段代码,我需要将已存在网页转换,不是新建html。
public void htmlCodeComeString(String htmlCode, String pdfPath) {
Document doc = new Document(PageSize.A4);
try {
PdfWriter.getInstance(doc, new FileOutputStream(pdfPath));
doc.open();
// 解决中文问题
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
Paragraph t = new Paragraph(htmlCode, FontChinese);
doc.add(t);
doc.close();
System.out.println("文档创建成功");
}catch(Exception e) {
e.printStackTrace();
}
}
...
Paragraph t = new Paragraph((Element) p.get(k), FontChinese);
doc.add(t);
...
理论上这样,但是前提,你的html本身的解析没问题,Element) p.get(k)是什么,调试下