JAVA常用API或编程工具001---ITEXT把html转换成pdf的jar包,使用Java将HTML转换为PDF
目标:
- 实现如何利用iText Java库将HTML文件转换成PDF文档?
Environment & Tools
- Eclipse (or any other IDE)
- Maven (optional)
Library:
- iText 5.4.2
- List of jar files: .classpath
- List of Maven dependencies: pom.xml
( 1 ) HTML File
- index.html
01
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
02
<
html
>
03
<
head
>
04
<
title
>HTML to PDF</
title
>
05
<
link
href
=
"style.css"
rel
=
"stylesheet"
type
=
"text/css"
/>
06
</
head
>
07
<
body
>
08
<
h1
>HTML to PDF</
h1
>
09
<
p
>
10
<
span
class
=
"itext"
>itext</
span
> 5.4.2 <
span
class
=
"description"
> converting HTML to PDF</
span
>
11
</
p
>
12
<
table
>
13
<
tr
>
14
<
th
class
=
"label"
>Title</
th
>
15
<
td
>iText - Java HTML to PDF</
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
>
- style.css
01
h
1
{
02
color
:
#ccc
;
03
}
04
table tr td{
05
text-align
:
center
;
06
border
:
1px
solid
gray
;
07
padding
:
4px
;
08
}
09
table tr th{
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 ) Java App
- App.java
01
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( String[] args )
throws
DocumentException, IOException
14
{
15
// step 1
16
Document document =
new
Document();
17
// step 2
18
PdfWriter writer = PdfWriter.getInstance(document,
new
FileOutputStream(
"pdf.pdf"
));
19
// step 3
20
document.open();
21
// step 4
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”
Source Code @ GitHub
JAVA常用API或编程工具001---ITEXT把html转换成pdf的jar包,使用Java将HTML转换为PDF相关推荐
- JAVA常用API或编程工具003--实现pdf在线阅读功能之pdf.js
pdf.js简介 PDF.js 是基于开放的 HTML5 及 JavaScript 技术实现的开源产品.简单说就是一个 PDF 解析器.运用HTML5JavaScript(即pdf.js仅使用安全的w ...
- JAVA常用API或编程工具002---SpringSource Tool Suite:基于Eclipse的Spring应用开发环境
Spring Tool Suite(STS) 是一个基于Eclipse针对Spring应用量身定制的开发环境.提供了开发 Spring 应用必须的编码.调试.运行和部署功能.STS 在最新的 Ecli ...
- Java | Java常用API方法总结
Java常用API方法总结 常用API JavaAPI 1 java.lang String StringBuilder Integer parseXxx Math Object System Thr ...
- Java 常用API
Java 常用API JFC(Java类库) 是系统提供的已实现的标准类的集合,是Java编程的API(Application Program Interface) 可帮助开发者方便.快捷地进行软件开 ...
- Java --- 常用API
常用API 方法重载: 方法名相同,方法接收的参数不同 static: 修饰的类,可以直接使用类名进行调用 方法名 说明 public static abs(int a) 返回参数的绝对值 publi ...
- [转]一款可以反编译jar包的java反编译工具-JD-GUI(绿色软件)
[转]一款可以反编译jar包的java反编译工具-JD-GUI(绿色软件) 文章分类:Java编程 该软件无需安装,界面清爽. 转自:http://www.devdoc.org/index.php/a ...
- Java 常用API的运用,效率及技巧
1. Java面向对象基本概念 2. System 3. String, StringBuffer 4. 数值,字符,布尔对象与简单类型的操作 5. Class ...
- (day9) 自学Java——常用API
AIP就是Java已经写好的各种功能的java类 目录 1.Math 2.System 3.Runtime 4.Object 5.对象工具类Objects 6.BIgInteger 7.BigDeci ...
- Java工作笔记/Java面试题/Java八股文/Java常用API
码农工具包 hutool工具 hutool工具类判断各种类型数据 <!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all --& ...
最新文章
- IntelliJ IDEA导入JDK出现The selected directory is not a valid home for JDK问题的解决方法
- shiro的QuickStart
- 《吃土》全书笔记整理
- 《Spring2.0 技术手册》读书笔记五-与Spring容器的交互(2)
- 在ie中关于ajax请求获得数据缓存问题的解决办法
- 数组指定位置添加元素_访问数组的任意位置元素的性能真的一样?
- Hadoop各组件详解
- 求职之测试开发技能准备
- Eureka的自我保护机制
- EVA改进(点锐度算法)图像清晰度评价方法C++实现
- python基础之if嵌套与循环
- CSS浮动-5.1浮动比较重要-使用folat属性来设置元素的浮动-left-right-none
- 找不到启动设备按任意键重新启动计算机,[已解决]硬盘3f0 /找不到启动设备错误...
- 爬虫-使用代理ip,使用session
- 智能优化算法之遗传算法python实现细节,GA库函数调用方法
- 什么是“真AI相机”
- 玉米社:竞价推广关键词出价原则,注意这几点!
- 修改linux中@后的名字
- 网络攻防-20169213-刘晶-第五周作业
- 关于AD前面的电压跟随器