IText实现Html转PDF itextpdf-5.5.5.jar
2015-04-21 19:41
519 查看
环境:itextpdf-5.5.5.jar xmlworker-5.5.5.jar
尝试做个csdn博文下载器,首要解决的任务是html转pdf,百度到IText,查了很多教程Itext版本都比较老,我下的5.5.5的。
代码不复杂,
public static void main(String[] args) throws FileNotFoundException,
Exception {
String htmlFile = "C:\\Users\\Administrator\\Desktop\\test.htm";
String pdfFile = "C:\\Users\\Administrator\\Desktop\\test.pdf";
// PdfUtils.parseHTML2PDFFile(pdfFile, new FileInputStream(htmlFile));
String ss = "";
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(htmlFile), "UTF-8"));
String t = "";
while ((t = br.readLine()) != null) {
// System.out.println(t);
ss += t;
}
PdfUtils.parseHTML2PDFFile2(pdfFile, ss);
}
public static void parseHTML2PDFFile2(String pdfFile, String html)
throws DocumentException, IOException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream(pdfFile));
document.open();
XMLWorkerHelper.getInstance().parseXHtml(writer, document,
new ByteArrayInputStream(html.getBytes("Utf-8")),
Charset.forName("UTF-8"));
document.close();
}
遇到的问题:中文显示不了,百度了好久没解决,后来尝试写了一个简单的html页面,能显示中文,查了下原因,html要设置字体,在body加<body style="font-size:12.0pt; font-family:微软雅黑"> 字体可以换,然后输出就能显示中文了。
复制去Google翻译翻译结果
尝试做个csdn博文下载器,首要解决的任务是html转pdf,百度到IText,查了很多教程Itext版本都比较老,我下的5.5.5的。
代码不复杂,
public static void main(String[] args) throws FileNotFoundException,
Exception {
String htmlFile = "C:\\Users\\Administrator\\Desktop\\test.htm";
String pdfFile = "C:\\Users\\Administrator\\Desktop\\test.pdf";
// PdfUtils.parseHTML2PDFFile(pdfFile, new FileInputStream(htmlFile));
String ss = "";
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(htmlFile), "UTF-8"));
String t = "";
while ((t = br.readLine()) != null) {
// System.out.println(t);
ss += t;
}
PdfUtils.parseHTML2PDFFile2(pdfFile, ss);
}
public static void parseHTML2PDFFile2(String pdfFile, String html)
throws DocumentException, IOException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream(pdfFile));
document.open();
XMLWorkerHelper.getInstance().parseXHtml(writer, document,
new ByteArrayInputStream(html.getBytes("Utf-8")),
Charset.forName("UTF-8"));
document.close();
}
遇到的问题:中文显示不了,百度了好久没解决,后来尝试写了一个简单的html页面,能显示中文,查了下原因,html要设置字体,在body加<body style="font-size:12.0pt; font-family:微软雅黑"> 字体可以换,然后输出就能显示中文了。
复制去Google翻译翻译结果
相关文章推荐
- 使用 itext、flying-saucer 实现html转PDF(转)
- iText 实现 html 转换 pdf
- 使用 itext、flying-saucer 实现html转PDF
- 利用Flying Saucer 和 java iText 实现HTMl转PDF
- iText实现html转pdf
- 利用Flying Saucer 和 iText 实现HTMl转PDF(java)
- itext实现HTML转换为PDF
- 【JAVA】使用 iText XMLWorker实现HTML转PDF
- Itext 实现 html转换成pdf
- java使用wkhtmltopdf实现Html转pdf,并在centos服务器部署步骤。
- itext和jxl实现PDF,CSV和Excel格式的文件的下载
- js实现html下载到本地并生成pdf文件
- lecene.net实现pdf,doc,xls,ppt,htm,html等格式文件的检索
- (Java实现)HTML页面转PDF解决方案
- Itext 导出PDF(中文解决,HTML转PDF)
- java实现html转pdf功能
- 使用iText 将html页面转PDF文件(itext+freemarker)
- lecene.net实现pdf,doc,xls,ppt,htm,html等格式文件的检索
- asp.net导出excel-一行代码实现excel、xml、pdf、word、html、csv等7种格式文件导出功能而且美观-SNF快速开发平台
- 使用freemaker和itext从html模板生成pdf文件