java根據(jù)模板實(shí)現(xiàn)填充word內(nèi)容并轉(zhuǎn)換為pdf
1.word模板填充內(nèi)容
使用EasyPoi寫入Word文檔。
import cn.afterturn.easypoi.word.WordExportUtil; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.poi.xwpf.usermodel.XWPFDocument; import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class WriteWordtemplate { public static void main(String[] args) throws Exception { Map<String, Object> params = createDate(); String sourceFile = "d:/temp/模版word3.docx"; String targetFile = "d:/temp/輸出結(jié)果3.docx"; FileOutputStream fos = null; try { //獲取模板文檔 File templateFile = new File(sourceFile); System.out.println(templateFile.getName()); // 寫入word XWPFDocument doc = WordExportUtil.exportWord07(templateFile.getPath(), params); fos = FileUtils.openOutputStream(new File(targetFile)); doc.write(fos); } catch (Exception e) { System.out.println(e); } finally { IOUtils.closeQuietly(fos); } } private static Map<String, Object> createDate() { //填充數(shù)據(jù) List<WordExportBatch> resultList = new ArrayList<>(); WordExportBatch wordExport = new WordExportBatch(); WordExportBatch wordExport1 = new WordExportBatch(); wordExport.setCreateDate("2022/9/30"); wordExport1.setCreateDate("2022/9/28"); wordExport.setNumber("11"); wordExport.setExMoneny("11"); wordExport.setExFunc("支付寶"); wordExport1.setNumber("15"); wordExport.setAmount("1234.5"); wordExport1.setAmount("2345.77"); wordExport.setEndDate("2022/12/31"); wordExport1.setEndDate("2022/11/30"); wordExport.setType("支付寶"); wordExport1.setType("微信"); wordExport1.setExMoneny("22"); wordExport1.setExFunc("微信"); resultList.add(wordExport); resultList.add(wordExport1); //準(zhǔn)備數(shù)據(jù) Map<String, Object> params = new HashMap<>(); params.put("number", "112"); params.put("amount", "1234.5"); params.put("endDate", "2022/11/30"); params.put("resultList", resultList); return params; } }
pom依賴可以參考:使用EasyPoi實(shí)現(xiàn)word文檔生成和段落循環(huán)
2.把word轉(zhuǎn)換為pdf
word生成pdf的方法比較多,調(diào)研了常用的方式
一種方式是部署一臺(tái)windowserver,對(duì)外提供接口來(lái)進(jìn)行生成,能最大程度還原格式。
第二種方式就是利用jodconverter,會(huì)有少許的失真。
現(xiàn)在介紹的是基于jodconverter把word轉(zhuǎn)換為PDF。首先引入jar包
<dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-local</artifactId> <version>4.2.2</version> </dependency> <dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-spring-boot-starter</artifactId> <version>4.2.2</version> </dependency>
在配置文件中加入如下配置
jodconverter: local: enabled: true # 啟用本地模式 office-home: /opt/libreoffice7.5 # LibreOffice 的安裝路徑 max-tasks-per-process: 10 # 每個(gè) Office 進(jìn)程處理的最大任務(wù)數(shù) port-numbers: 8202 # LibreOffice 后臺(tái)服務(wù)使用的端口號(hào),可修改
在處理類中注入,之所以使用@Lazy是因?yàn)榧虞d的使用會(huì)報(bào)錯(cuò),讓它延遲加載。
@Lazy @Autowired private DocumentConverter converter; public void wordConvertToPdf(File wordFile, File pdfFile) { try { logger.info("word轉(zhuǎn)換pdf開(kāi)始"); converter.convert(wordFile).to(pdfFile).execute(); logger.info("word轉(zhuǎn)換pdf結(jié)束"); } catch (Exception e) { logger.error("word轉(zhuǎn)pdf異常", e); } }
3.后續(xù)使用
之前寫過(guò)word模板生成段落和生成表格的段落。
結(jié)合起來(lái)就能生成相對(duì)完整的word了。后續(xù)如果需要蓋章什么的直接拿生成的pdf文件就可以了。
到此這篇關(guān)于java根據(jù)模板實(shí)現(xiàn)填充word內(nèi)容并轉(zhuǎn)換為pdf的文章就介紹到這了,更多相關(guān)java模板填充word內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)的properties文件動(dòng)態(tài)修改并自動(dòng)保存工具類
這篇文章主要介紹了Java實(shí)現(xiàn)的properties文件動(dòng)態(tài)修改并自動(dòng)保存工具類,可實(shí)現(xiàn)針對(duì)properties配置文件的相關(guān)修改與保存功能,需要的朋友可以參考下2017-11-11簡(jiǎn)單了解JAVA內(nèi)存區(qū)域效果知識(shí)
這篇文章主要介紹了簡(jiǎn)單了解JAVA內(nèi)存區(qū)域效果知識(shí),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10解決表單post,get到springMVC后臺(tái)亂碼的問(wèn)題
下面小編就為大家分享一篇解決表單post,get到springMVC后臺(tái)亂碼的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01淺談Java8 的foreach跳出循環(huán)break/return
這篇文章主要介紹了Java8 的foreach跳出循環(huán)break/return,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07IntelliJ IDEA Project窗口的一些設(shè)置詳解
這篇文章主要介紹了IntelliJ IDEA Project窗口的一些設(shè)置詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08SpringBoot之HandlerInterceptor攔截器的使用詳解
這篇文章主要介紹了SpringBoot之HandlerInterceptor攔截器的使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10springboot的maven多模塊混淆jar包的實(shí)現(xiàn)方法
springboot可以使用proguard-maven-plugin 這個(gè)插件 在 pom.xml 中自定義proguard 的指令,本文基于 springboot + maven + proguard 的maven多模塊架構(gòu)進(jìn)行代碼混淆,需要的朋友可以參考下2024-03-03