最近项目需要实现pdf下载的功能,由于没有这方面的经验,从网上花了很长时间查找了相关的资料。整理之后,发现有几个框架可以实现这个功能。
1. 开源框架支持
- itext,生成pdf文档,还支持将xml、html文件转化为pdf文件;
- apache pdfbox,生成、合并pdf文档;
- docx4j,生成docx文档,支持转换为pdf格式。
2. 实现方案
比较了一番后,采用了freemarker+docx4j+apache pdfbox的方案:
maven依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
<!-- pdfbox --> <dependency> <groupid>org.apache.pdfbox</groupid> <artifactid>pdfbox</artifactid> <version> 2.0 . 11 </version> </dependency> <!-- docx4j --> <dependency> <groupid>org.docx4j</groupid> <artifactid>docx4j</artifactid> <version> 3.3 . 7 </version> </dependency> <dependency> <groupid>org.apache.xmlgraphics</groupid> <artifactid>batik-util</artifactid> <version> 1.10 </version> </dependency> <dependency> <groupid>org.docx4j</groupid> <artifactid>docx4j-export-fo</artifactid> <version> 3.3 . 6 </version> </dependency> <!-- https: //mvnrepository.com/artifact/dom4j/dom4j --> <dependency> <groupid>dom4j</groupid> <artifactid>dom4j</artifactid> <version> 1.6 . 1 </version> </dependency> <!-- image --> <dependency> <groupid>net.coobird</groupid> <artifactid>thumbnailator</artifactid> <version> 0.4 . 8 </version> </dependency> <!-- json --> <dependency> <groupid>com.alibaba</groupid> <artifactid>fastjson</artifactid> <version> 1.2 . 47 </version> </dependency> |
步骤
1 把pdf文档对应的word(docx)制作出来
2 把word文档另存为xml文件
另存为xml
3 将xml文件制作为freemarker模版(ftl)文件
制作模版文件
4 将数据和ftl文件组装为xml文本
1
2
3
4
5
6
7
8
|
map<string, object> map = new hashmap<>(); map.put( "name" , "小明" ); map.put( "address" , "北京市朝阳区" ); map.put( "email" , "xiaoming@abc.com" ); stringwriter stringwriter = new stringwriter(); bufferedwriter writer = new bufferedwriter(stringwriter); template.process(map, writer); string xmlstr = stringwriter.tostring(); |
5 使用docx4j将xml文本加载为word文档对象
1
2
|
bytearrayinputstream in = new bytearrayinputstream(xmlstr.getbytes()); wordprocessingmlpackage wordmlpackage = wordprocessingmlpackage.load(in); |
6 使用docx4j将word文档转存为pdf文档
1
2
|
string outputfilepath = "/users/xiaoming/简历.pdf" ; docx4j.topdf(wordmlpackage, new fileoutputstream( new file(outputfilepath))); |
7 使用apache pdfbox将多个pdf文档合为一个
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
string foldername = "/users/xiaoming/pdfs" ; string destpath = "/users/xiaoming/all.pdf" ; pdfmergerutility mergepdf = new pdfmergerutility(); string[] filesinfolder = getfiles(foldername); arrays.sort(filesinfolder, new comparator<string>() { @override public int compare(string o1, string o2) { return o1.compareto(o2); } }); for ( int i = 0 ; i < filesinfolder.length; i++) { mergepdf.addsource(foldername + file.separator + filesinfolder[i ]); } mergepdf.setdestinationfilename(destpath); mergepdf.mergedocuments(memoryusagesetting.setupmainmemoryonly()); |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.jianshu.com/p/b89f6ea30585