背景:日常開發erp系統,會有一些工單或者合同之類需要填寫打印。我們就會將其word模板來通過系統自動化填寫并轉換為pdf格式(pdf文件打印可保證文件質量,是一種通用的格式。文件不易去修改,比較穩定)。所以我們將通過jacob來實現這些功能。
準備工作:
1.服務器需要安裝office2007,因為我們就是調用這個來實現轉換。
2.需要安裝插件jacob,安裝jacob-1.14.3-x86.dll到jdk\jdk1.7.0\jre\bin(你自己電腦安裝的jdk)
3.需要使用jacob-1.14.3.jar包
maven代碼如下:
1
2
3
4
5
|
<dependency> <groupid>net.sf.jacob-project</groupid> <artifactid>jacob</artifactid> <version> 1.14 . 3 </version> </dependency> |
4.假如通過以上準備工作未成功轉換,就下載一個saveaspdfandxps.exe組件(office2007里的)。我就是通過這個組件才完成轉換。
5.上面的在系統為windows7中就可以了,假如你的項目需要發布到服務器(服務器系統一般都是windows2008)。則還需要一步。在上面的基礎上再安裝安裝jacob-1.14.3-x64.dll到jdk\jdk1.7.0\jre\bin(你自己電腦安裝的jdk)中。很多人在win7下都能成功轉換,但在win2008就是出問題。我就是通過磨了一天的時間,看了各種日志才發現問題。
一、工具類(operationio.java),這里面可以不做任何修改,復制粘貼就可以了。
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
package com.repair.util.pub; import java.awt.image.bufferedimage; import java.io.bytearrayinputstream; import java.io.file; import java.io.fileinputstream; import java.io.ioexception; import java.io.inputstream; import javax.imageio.imageio; import sun.misc.base64decoder; import sun.misc.base64encoder; import com.jacob.activex.activexcomponent; import com.jacob.com.comthread; import com.jacob.com.dispatch; import com.jacob.com.variant; public class operationio { static final int wdformatpdf = 17 ; // pdf 格式 /** * word轉換pdf * @param sfilename word文件存在位置 * @param tofilename pdf文件存放位置 */ public static void wordtopdf(string sfilename,string tofilename){ system.out.println( "啟動word..." ); long start = system.currenttimemillis(); activexcomponent app = null ; dispatch doc = null ; try { //調用office word app = new activexcomponent( "word.application" ); app.setproperty( "visible" , new variant( false )); dispatch docs = app.getproperty( "documents" ).todispatch(); doc = dispatch.call(docs, "open" , sfilename).todispatch(); system.out.println( "打開文檔..." + sfilename); system.out.println( "轉換文檔到pdf..." + tofilename); file tofile = new file(tofilename); if (tofile.exists()) { tofile.delete(); } dispatch.call(doc, "saveas" , tofilename, // filename wdformatpdf); long end = system.currenttimemillis(); system.out.println( "轉換完成..用時:" + (end - start) + "ms." ); } catch (exception e) { system.out.println( "========error:文檔轉換失敗:" + e.getmessage()); } finally { dispatch.call(doc, "close" , false ); system.out.println( "關閉文檔" ); if (app != null ) app.invoke( "quit" , new variant[] {}); } //如果沒有這句話,winword.exe進程將不會關閉 comthread.release(); } /** * 遞歸刪除目錄下的所有文件及子目錄下所有文件 * @param dir 將要刪除的文件目錄 * @return boolean returns "true" if all deletions were successful. * if a deletion fails, the method stops attempting to * delete and returns "false". */ public static boolean deletedir(file dir) { if (dir.isdirectory()) { string[] children = dir.list(); for ( int i= 0 ; i<children.length; i++) { boolean success = deletedir( new file(dir, children[i])); if (!success) { return false ; } } } // 目錄此時為空,可以刪除 return dir.delete(); } /** * 將圖片文件轉化為字節數組字符串,并對其進行base64編碼處理 * @param imgfilepath 圖片地址路徑 */ public static string getimagestr(string imgfilepath) { // byte [] data = null ; // 讀取圖片字節數組 try { inputstream in = new fileinputstream(imgfilepath); data = new byte [in.available()]; in.read(data); in.close(); } catch (ioexception e) { e.printstacktrace(); } // 對字節數組base64編碼 base64encoder encoder = new base64encoder(); return encoder.encode(data); // 返回base64編碼過的字節數組字符串 } /** * 將二進制轉換為圖片 * * @param base64string */ public static void base64stringtoimage(string base64string,string imageoutpath) { try { base64decoder decoder = new sun.misc.base64decoder(); byte [] bytes1 = decoder.decodebuffer(base64string); bytearrayinputstream bais = new bytearrayinputstream(bytes1); bufferedimage bi1 = imageio.read(bais); file w2 = new file(imageoutpath); // 可以是jpg,png,gif格式 imageio.write(bi1, "jpg" , w2); // 不管輸出什么格式圖片,此處不需改動 } catch (ioexception e) { e.printstacktrace(); } } } |
二、業務類(printwordtopdf.java) ,這里
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
package com.hjm.test; import java.io.bufferedwriter; import java.io.file; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.outputstreamwriter; import java.io.unsupportedencodingexception; import java.io.writer; import java.util.hashmap; import java.util.map; import com.engineering.pojos.pub.gcrecordarchive; import com.repair.util.pub.operationio; import freemarker.template.configuration; import freemarker.template.template; import freemarker.template.templateexception; public class printwordtopdf { public static void main(string[] args) { //創建一個configuration的實例 configuration configuration = new configuration(); //設置編碼 configuration.setdefaultencoding( "utf-8" ); //創建map對象,來保存要填寫的數據 map<string, object> paramap = new hashmap<string, object>(); //下面這些是我測試的一些數據 paramap.put( "receivingparty" , "中國民航" ); paramap.put( "packinglistno" , 10087 ); paramap.put( "conno" , 10088 ); try { //調用模板的文件夾,new file("d:\\測試")是一個絕對路徑,你可以自己設置為服務器路徑。 configuration.setdirectoryfortemplateloading( new file( "d:\\測試" )); } catch (ioexception e) { e.printstacktrace(); } template t = null ; try { //獲取模板文件 t = configuration.gettemplate( "fmo-08 packing list.ftl" ); // 獲取模板文件 } catch (ioexception e) { e.printstacktrace(); } //生成一個文件保存的文件夾 file file = new file( "d:\\最終" ); //判斷文件夾是否存在,存在刪除并重創 if (!file .exists() && !file .isdirectory()) { file.mkdir(); } else { boolean b = operationio.deletedir(file); if (b){ file.mkdir(); } } //填寫數據后生成的word文件。 string outfilepath = "d:/最終\\結果" + ".doc" ; file outfile = new file(outfilepath); // 導出文件 writer out = null ; try { try { out = new bufferedwriter( new outputstreamwriter( new fileoutputstream(outfile), "utf-8" )); } catch (unsupportedencodingexception e) { // todo auto-generated catch block e.printstacktrace(); } } catch (filenotfoundexception e1) { e1.printstacktrace(); } try { t.process(paramap,out); // 將填充數據填入模板文件并輸出到目標文件 out.flush(); out.close(); //轉換pdf的文件 operationio.wordtopdf(outfilepath, "d:/最終\\結果" + ".pdf" ); } catch (templateexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } } |
總結:通過以上代碼,就可以在模板中填寫好數據,并將其生成word文件與其pdf文件。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qq_31170429/article/details/78424973