java將word轉(zhuǎn)pdf的方法示例詳解
總結(jié)
建議使用aspose-words轉(zhuǎn)pdf,poi的容易出問題還丑…
poi的(多行的下邊框就不對了)
aspose-words的(基本和word一樣)
poi工具轉(zhuǎn)換
<!-- 處理PDF --> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId> <version>2.0.3</version> </dependency>
這個工具使用了poi,最新的2.0.3對應poi的5.2.0,2.0.1對應poi的3.15
使用
//拿到word流 InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("word/muban3.docx"); if (inputStream == null) { throw new MsgException("讀取模板失敗"); } XWPFDocument document = new XWPFDocument(inputStream); //.....word處理 PdfOptions pdfOptions = PdfOptions.create();//.fontEncoding( BaseFont.CP1250 ); //轉(zhuǎn)pdf操作 (直接寫入響應) PdfConverter.getInstance().convert(document, response.getOutputStream(), pdfOptions); response.setContentType("application/pdf");
或者寫入輸出流
/** * 將word轉(zhuǎn)為pdf并返回一個輸出流 * * @param document 輸出文件名(pdf格式) */ public static ByteArrayOutputStream wordToPdfOutputStream(XWPFDocument document) throws IOException { //word轉(zhuǎn)pdf ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PdfOptions pdfOptions = PdfOptions.create();//.fontEncoding( BaseFont.CP1250 ); //轉(zhuǎn)pdf操作 PdfConverter.getInstance().convert(document, outputStream, pdfOptions); return outputStream; }
問題
poi改了word之后,生成沒問題,word中創(chuàng)建的表格,轉(zhuǎn)pdf的時候經(jīng)常出問題(直接報錯或者合并無效)
研究了2天,pdf轉(zhuǎn)一直各種問題,一起之下?lián)Q技術(shù)
aspose-words
https://blog.csdn.net/Wang_Pink/article/details/141898210
<dependency> <groupId>com.luhuiguo</groupId> <artifactId>aspose-words</artifactId> <version>23.1</version> </dependency>
poi處理word一堆的依賴,這個一個就好,而且本身就支持轉(zhuǎn)pdf!!!
使用
- 在resources創(chuàng)建
word-license.xml
<License> <Data> <Products> <Product>Aspose.Total for Java</Product> <Product>Aspose.Words for Java</Product> </Products> <EditionType>Enterprise</EditionType> <SubscriptionExpiry>20991231</SubscriptionExpiry> <LicenseExpiry>20991231</LicenseExpiry> <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber> </Data> <Signature> sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU= </Signature> </License>
- 工具類
import com.aspose.words.Document; import com.aspose.words.License; import com.aspose.words.SaveFormat; import lombok.extern.slf4j.Slf4j; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.Objects; @Slf4j public class Doc2PdfUtil { /** * 獲取 license 去除水印 * 若不驗證則轉(zhuǎn)化出的pdf文檔會有水印產(chǎn)生 */ private static void getLicense() { String licenseFilePath = "word-license.xml"; try { InputStream is = Doc2PdfUtil.class.getClassLoader().getResourceAsStream(licenseFilePath); License license = new License(); license.setLicense(Objects.requireNonNull(is)); } catch (Exception e) { log.error("license verify failed"); e.printStackTrace(); } } /** * word 轉(zhuǎn) pdf * * @param wordFile word 文件路徑 * @param pdfFile 生成的 pdf 文件路徑 */ public static void word2Pdf(String wordFile, String pdfFile) { File file = new File(pdfFile); if (!file.getParentFile().exists()) { file.getParentFile().mkdir(); } getLicense(); try (FileOutputStream os = new FileOutputStream(new File(pdfFile))) { Document doc = new Document(wordFile); doc.save(os, SaveFormat.PDF); } catch (Exception e) { log.error("word轉(zhuǎn)pdf失敗", e); } } /** * word 轉(zhuǎn) pdf * * @param wordFile word 文件流 * @param pdfFile 生成的 pdf 文件流 */ public static void word2Pdf(InputStream wordFile, OutputStream pdfFile) { getLicense(); try { Document doc = new Document(wordFile); doc.save(pdfFile, SaveFormat.PDF); } catch (Exception e) { log.error("word轉(zhuǎn)pdf失敗", e); } } }
使用
Doc2PdfUtil.word2Pdf("aa.docx","bb.pdf");
我是依舊使用poi處理word,用這個轉(zhuǎn)pdf
//拿到word流 InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("word/muban3.docx"); if (inputStream == null) { throw new MsgException("讀取模板失敗"); } XWPFDocument document = new XWPFDocument(inputStream); //.....word處理 ByteArrayInputStream in = null; try { //由于使用的poi的document,需要現(xiàn)將poi的document轉(zhuǎn)為普通的輸入流 in = WordUtil.getInputStream(document); Doc2PdfUtil.word2Pdf(in,response.getOutputStream()); response.setContentType("application/pdf"); } catch (Exception e) { log.error("報告下載失敗", e); } finally { try { document.close(); } catch (Exception e1) { log.error("document 流關(guān)閉失敗", e1); } if (in != null) { try { in.close(); } catch (Exception e1) { log.error("in 流關(guān)閉失敗", e1); } } }
public static ByteArrayInputStream getInputStream(XWPFDocument document) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { document.write(outputStream); return outputStreamToPdfInputStream(outputStream); } catch (IOException e) { throw new RuntimeException(e); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } } } } /** * 將word轉(zhuǎn)為pdf并返回一個輸入流 * * @param outputStream 輸出文件名(pdf格式) */ public static ByteArrayInputStream outputStreamToPdfInputStream(ByteArrayOutputStream outputStream) throws IOException { //輸出的pdf輸出流轉(zhuǎn)輸入流 try { //臨時 byte[] bookByteAry = outputStream.toByteArray(); return new ByteArrayInputStream(bookByteAry); } catch (Exception e) { e.printStackTrace(); return null; } }
完美轉(zhuǎn)換
到此這篇關(guān)于java將word轉(zhuǎn)pdf的文章就介紹到這了,更多相關(guān)java將word轉(zhuǎn)pdf內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java實現(xiàn)Word轉(zhuǎn)PDF的全過程
- Java調(diào)用py或者exe文件實現(xiàn)Word轉(zhuǎn)PDF
- Java實現(xiàn)一鍵將Word文檔轉(zhuǎn)為PDF
- Java實現(xiàn)WORD和PDF互相轉(zhuǎn)換以及數(shù)據(jù)填充示例
- Java將Word文檔轉(zhuǎn)換為PDF文件的幾種常用方法總結(jié)
- Java中Word與PDF轉(zhuǎn)換為圖片的方法詳解
- Java將Word轉(zhuǎn)換成PDF的常用用法
- 探討Java 將Markdown文件轉(zhuǎn)換為Word和PDF文檔
- Java將word文件轉(zhuǎn)成pdf文件的操作方法
- Java實現(xiàn)word/pdf轉(zhuǎn)html并在線預覽
- Java實現(xiàn)一鍵將Word文檔轉(zhuǎn)為PDF的兩種方法
相關(guān)文章
Installij IDEA install或clean項目的使用
這篇文章主要介紹了Installij IDEA install或clean項目的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08spring boot項目生成docker鏡像并完成容器部署的方法步驟
這篇文章主要介紹了spring boot項目生成docker鏡像并完成容器部署的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10Java遞歸調(diào)用如何實現(xiàn)數(shù)字的逆序輸出方式
這篇文章主要介紹了Java遞歸調(diào)用如何實現(xiàn)數(shù)字的逆序輸出方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04springboot無法從靜態(tài)上下文中引用非靜態(tài)變量的解決方法
這篇文章主要介紹了springboot無法從靜態(tài)上下文中引用非靜態(tài)變量的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-06-06SpringBoot Redis實現(xiàn)接口冪等性校驗方法詳細講解
這篇文章主要介紹了SpringBoot Redis實現(xiàn)接口冪等性校驗方法,近期一個老項目出現(xiàn)了接口冪等性校驗問題,前端加了按鈕置灰,依然被人拉著接口參數(shù)一頓輸出,還是重復調(diào)用了接口,通過復制粘貼,完成了后端接口冪等性調(diào)用校驗2022-11-11