Springboot如何集成jodconverter做文檔轉(zhuǎn)換
公司項目開發(fā)中,早期使用docx4j
進行word轉(zhuǎn)pdf
,出現(xiàn)了很多格式紊亂、空格縮進、字體間距變大等問題。
雖然針對空格縮進等處理,采取全角
模式,進行了改善。但依舊還是會有很多解決不了的格式。
一直在找一種新的方式進行替代,主要是:保證顯示格式。
jodconverter 簡介
這是一款利用操作系統(tǒng)
中的office
庫,實現(xiàn)文檔類型轉(zhuǎn)換的工具。目前支持很多格式間的互相轉(zhuǎn)換。
- 這里不做太多的闡述,度娘、論壇等都有很多博客的說明。
- 本次只是為了基本的測試與使用。
下載安裝 libreoffice
根據(jù)電腦對應(yīng)的系統(tǒng),選擇指定系統(tǒng)版本的進行安裝即可。
本次以windows進行演示,后期會增加linux的安裝腳本。
代碼演示
1、創(chuàng)建springboot項目工程并引入依賴
本次測試代碼,結(jié)合docx
模板數(shù)據(jù)填入的思想,進行doc文件內(nèi)容填充,并將doc文件轉(zhuǎn)換pdf處理。
往期回顧:根據(jù)docx填充生成word文件,并導(dǎo)出pdf
所以需要導(dǎo)入以下依賴:
<!-- docx 模板填入與導(dǎo)出doc --> <dependency> <groupId>com.deepoove</groupId> <artifactId>poi-tl</artifactId> <version>1.5.0</version> </dependency> <!-- libreoffice 進行文件轉(zhuǎn)換 --> <dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-spring-boot-starter</artifactId> <version>4.4.4</version> </dependency> <dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-local</artifactId> <version>4.4.4</version> </dependency>
2、配置
除了增加對應(yīng)依賴文件之外,還需要增加application.properties
文件的配置。
如下所示:
server.port=80 jodconverter.local.enabled=true # libreOffice根目錄 jodconverter.local.office-home=C:/Program Files/LibreOffice # 任務(wù)執(zhí)行的超時時間 jodconverter.local.task-execution-timeout=86400000 # 任務(wù)隊列的超時時間 jodconverter.local.task-queue-timeout=86400000 # 端口(線程) jodconverter.local.port-numbers=2001,2002,2003 # 一個進程的超時時間 jodconverter.local.process-timeout=86400000
3、準備一個docx模板
并放置于resources/templates_report
下,如下所示:
4、編寫測試代碼
如下所示:
package cn.xj.controller; import com.deepoove.poi.XWPFTemplate; import lombok.extern.slf4j.Slf4j; import org.jodconverter.core.DocumentConverter; import org.jodconverter.core.document.DefaultDocumentFormatRegistry; import org.jodconverter.core.office.OfficeException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; @Slf4j @RestController @RequestMapping("/pdf") public class TestController { @Autowired private DocumentConverter documentConverter; @RequestMapping("/test") public void test() throws IOException { Map<String, Object> params = new HashMap<>(); params.put("username","xiangjiao1"); params.put("password","******"); params.put("age",22); params.put("email","專注寫bug測試中文"); Resource resource = new ClassPathResource("templates_report/001.docx"); File file = resource.getFile(); // 數(shù)據(jù)填充 XWPFTemplate template = XWPFTemplate.compile(file).render(params); String docOutPath = System.getProperty("user.dir")+File.separator+"springboot-poi"+File.separator+"pdf"+File.separator+System.currentTimeMillis()+ ".doc"; OutputStream outputStream = new FileOutputStream(docOutPath); template.write(outputStream); try { String pdfOutPath = System.getProperty("user.dir")+File.separator+"springboot-poi"+File.separator+"pdf"+File.separator+System.currentTimeMillis()+".pdf"; documentConverter.convert(new File(docOutPath)).to(new File(pdfOutPath)).as(DefaultDocumentFormatRegistry.PDF).execute(); } catch (OfficeException e) { log.error("文檔轉(zhuǎn)換異常:{}", e.getMessage()); } } }
運行后的樣式
linux 環(huán)境下安裝 libreoffice
腳本如下所示:
#!/bin/bash cd /tmp install_redhat() { wget https://kkfileview.keking.cn/LibreOffice_7.3.7_Linux_x86-64_rpm.tar.gz -cO LibreOffice_7_rpm.tar.gz && tar -zxf /tmp/LibreOffice_7_rpm.tar.gz && cd /tmp/LibreOffice_7.3.7.2_Linux_x86-64_rpm/RPMS echo $? if [ $? -eq 0 ];then yum install -y libSM.x86_64 libXrender.x86_64 libXext.x86_64 yum groupinstall -y "X Window System" yum localinstall -y *.rpm echo 'install finshed...' else echo 'download package error...' fi } install_ubuntu() { wget https://kkfileview.keking.cn/LibreOffice_7.3.7_Linux_x86-64_deb.tar.gz -cO LibreOffice_7_deb.tar.gz && tar -zxf /tmp/LibreOffice_7_deb.tar.gz && cd /tmp/LibreOffice_7.3.7.2_Linux_x86-64_deb/DEBS echo $? if [ $? -eq 0 ];then apt-get install -y libxinerama1 libcairo2 libcups2 libx11-xcb1 dpkg -i *.deb echo 'install finshed...' else echo 'download package error...' fi } if [ -f "/etc/redhat-release" ]; then yum install -y wget install_redhat else apt-get install -y wget install_ubuntu fi
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot動態(tài)生成接口實現(xiàn)流程示例講解
最近遇到一個需求,需要在程序運行過程中,可以動態(tài)新增接口,自定義接口參數(shù)名稱,基本類型,以及請求方法,請求頭等等。通過幾天的研究,找到了我需要的解決方案2023-01-01BufferedInputStream(緩沖輸入流)詳解_動力節(jié)點Java學(xué)院整理
這篇文章主要為大家詳細介紹了BufferedInputStream緩沖輸入流的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05深入了解SpringAOP中的jdk動態(tài)代理與CGlib
這篇文章主要介紹了深入了解SpringAOP中的jdk動態(tài)代理與CGlib,一般我們編寫程序的思想是縱向的,也就是一個方法代碼從該方法第一行開始往下一步一步走,直到走完最后一行代碼,也就是說很多業(yè)務(wù)都需要的比如用戶鑒權(quán),資源釋放等,需要的朋友可以參考下2023-12-12SpringBoot配置文件、多環(huán)境配置、讀取配置的4種實現(xiàn)方式
SpringBoot支持多種配置文件位置和格式,其中application.properties和application.yml是默認加載的文件,配置文件可以根據(jù)環(huán)境通過spring.profiles.active屬性進行區(qū)分,命令行參數(shù)具有最高優(yōu)先級,可覆蓋其他所有配置2024-09-09SpringCloud OpenFeign自定義結(jié)果解碼器方式
這篇文章主要介紹了SpringCloud OpenFeign自定義結(jié)果解碼器方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09