springboot中Word转PDF技巧
生活随笔
收集整理的這篇文章主要介紹了
springboot中Word转PDF技巧
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近兩天在弄office套餐的各種轉換,剛開始用了easypoi對excel進行了各種操作,十分的方便。但是有個新需求是上傳word文檔,然后下載的時候是原word文檔,預覽的話用pdf預覽,就涉及到了word轉pdf。
一看easypoi上面沒有,這我就有點懵了,立馬想到了原生poi,網上搜了一圈自己嘗試了一下,發現因為poi升級到4.1版本的原因,遇到了各種問題,無法完成轉換。于是在stackoverflow上找到個完美答案。直接貼代碼:
pom:依賴
<dependency> <groupId>com.documents4j</groupId> <artifactId>documents4j-local</artifactId> <version>1.0.3</version> </dependency> <dependency> <groupId>com.documents4j</groupId> <artifactId>documents4j-transformer-msoffice-word</artifactId> <version>1.0.3</version> </dependency>java代碼:
public static void main(String[] args) {File inputWord = new File("D:/aa.docx");File outputFile = new File("D:/2.pdf");try {InputStream docxInputStream = new FileInputStream(inputWord);OutputStream outputStream = new FileOutputStream(outputFile);IConverter converter = LocalConverter.builder().build();converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();outputStream.close();System.out.println("success");} catch (Exception e) {e.printStackTrace();}}參考:springboot中Word轉PDF技巧 - 簡書 (jianshu.com)
?
總結
以上是生活随笔為你收集整理的springboot中Word转PDF技巧的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [Leedcode][JAVA][第98
- 下一篇: [密码学基础][每个信息安全博士生应该知