JAVA常用API或编程工具001---ITEXT把html转换成pdf的jar包,使用Java将HTML转换为PDF
2017-03-31 18:28
876 查看
iText“XMLWorker”允许开发人员以一种程序员友好的方式将XML文件转换成PDF文件。iText还可以将包含CSS样式的HTML转换为PDF格式的文档。
实现如何利用iTextJava库将HTML文件转换成PDF文档?
Eclipse(oranyotherIDE)
Maven(optional)
iText5.4.2
Listofjarfiles:.classpath
ListofMavendependencies:pom.xml
index.html
style.css
SourceCode@GitHub
目标:
实现如何利用iTextJava库将HTML文件转换成PDF文档?
Environment&Tools
Eclipse(oranyotherIDE)Maven(optional)
Library:
iText5.4.2Listofjarfiles:.
ListofMavendependencies:
(1)HTMLFile
index.html01 | <!DOCTYPE htmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> |
02 | < html > |
03 | < head > |
04 | < title >HTML toPDF</ title > |
05 | < link href = "style.css" rel = "stylesheet" type = "text/css" /> |
06 | </ head > |
07 | < body > |
08 | < h1 >HTML toPDF</ h1 > |
09 | < p > |
10 | < span class = "itext" >itext</ span > 5.4.2< span class = "description" > convertingHTMLtoPDF</ span > |
11 | </ p > |
12 | < table > |
13 | < tr > |
14 | < th class = "label" >Title</ th > |
15 | < td >iText -JavaHTMLtoPDF</ td > |
16 | </ tr > |
17 | < tr > |
18 | < th >URL</ th > |
19 | < td >http://hmkcode.com/itext-html-to-pdf-using-java</ td > |
20 | </ tr > |
21 | </ table > |
22 | </ body > |
23 | </ html > |
01 | h 1 { |
02 | color : #ccc ; |
03 | } |
04 | table trtd{ |
05 | text-align : center ; |
06 | border : 1px solid gray ; |
07 | padding : 4px ; |
08 | } |
09 | table trth{ |
10 | background-color : #84C7FD ; |
11 | color : #fff ; |
12 | width : 100px ; |
13 | } |
14 | .itext{ |
15 | color : #84C7FD ; |
16 | font-weight : bold ; |
17 | } |
18 | .description{ |
19 | color : gray ; |
20 | } |
(2)JavaApp
App.java01 | package com.hmkcode; |
02 |
03 | import java.io.FileInputStream; |
04 | import java.io.FileOutputStream; |
05 | import java.io.IOException; |
06 | import com.itextpdf.text.Document; |
07 | import com.itextpdf.text.DocumentException; |
08 | import com.itextpdf.text.pdf.PdfWriter; |
09 | import com.itextpdf.tool.xml.XMLWorkerHelper; |
10 |
11 | public class App |
12 | { |
13 | public static void main( throws DocumentException, IOException |
14 | { |
15 | // |
16 | Document new Document(); |
17 | // |
18 | PdfWriter new FileOutputStream( "pdf.pdf" )); |
19 | // |
20 | document.open(); |
21 | // |
22 | XMLWorkerHelper.getInstance().parseXHtml(writer, document, |
23 | new FileInputStream( "index.html" )); |
24 | //step 5 |
25 | document.close(); |
26 |
27 | System.out.println( "PDF Created!" ); |
28 | } |
29 | } |
(3)Output“PDF”
SourceCode@
相关文章推荐
- iText – 使用Java将HTML转换为PDF
- JAVA常用API或编程工具003--实现pdf在线阅读功能之pdf.js
- JAVA常用API或编程工具002---SpringSource Tool Suite:基于Eclipse的Spring应用开发环境
- 【JAVA】使用 iText XMLWorker实现HTML转PDF
- java 使用itextpdf.jar和itext-asian.jar根据pdf模板填写数据后生成一个新的pdf文件
- 使用Jsoup.jar写的工具类(java提取Html中的文本)
- java使用pd4ml将html转换成pdf
- 使用Itext把html转换成pdf文件
- java - 使用itext把txt转换pdf
- 一个非常不错的纯源码“PDF转换swf”完全脱离安装第三方插件,直接使用java调用Flash api将PDF转换成图片,在将图片转换成swf。
- PDF转换成HTML工具的使用
- java使用itextpdf将图片转换成pdf的简单实例
- java使用itextpdf将图片转换成pdf时DPI的问题
- java使用itext操作pdf
- Java使用正则表达式将UBB的img表达方法转换成html样式
- [转]java平台,使用openoffice将word转换为html
- java使用itext操作pdf
- 使用Java组件itext 生成pdf介绍
- Java中使用iText组件制作pdf报表
- 利用Flying Saucer 和 iText 实现HTMl转PDF(java)