java將word轉(zhuǎn)pdf的方法示例詳解
總結(jié)
建議使用aspose-words轉(zhuǎn)pdf,poi的容易出問(wèn)題還丑…
poi的(多行的下邊框就不對(duì)了)

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>
這個(gè)工具使用了poi,最新的2.0.3對(duì)應(yīng)poi的5.2.0,2.0.1對(duì)應(yīng)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操作 (直接寫(xiě)入響應(yīng))
PdfConverter.getInstance().convert(document, response.getOutputStream(), pdfOptions);
response.setContentType("application/pdf");
或者寫(xiě)入輸出流
/**
* 將word轉(zhuǎn)為pdf并返回一個(gè)輸出流
*
* @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;
}
問(wèn)題
poi改了word之后,生成沒(méi)問(wèn)題,word中創(chuàng)建的表格,轉(zhuǎn)pdf的時(shí)候經(jīng)常出問(wèn)題(直接報(bào)錯(cuò)或者合并無(wú)效)

研究了2天,pdf轉(zhuǎn)一直各種問(wè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一堆的依賴,這個(gè)一個(gè)就好,而且本身就支持轉(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 去除水印
* 若不驗(yàn)證則轉(zhuǎn)化出的pdf文檔會(huì)有水印產(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,用這個(gè)轉(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("報(bào)告下載失敗", 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并返回一個(gè)輸入流
*
* @param outputStream 輸出文件名(pdf格式)
*/
public static ByteArrayInputStream outputStreamToPdfInputStream(ByteArrayOutputStream outputStream) throws IOException {
//輸出的pdf輸出流轉(zhuǎn)輸入流
try {
//臨時(shí)
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java實(shí)現(xiàn)Word轉(zhuǎn)PDF的全過(guò)程
- Java調(diào)用py或者exe文件實(shí)現(xiàn)Word轉(zhuǎn)PDF
- Java實(shí)現(xiàn)一鍵將Word文檔轉(zhuǎn)為PDF
- Java實(shí)現(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實(shí)現(xiàn)word/pdf轉(zhuǎn)html并在線預(yù)覽
- Java實(shí)現(xiàn)一鍵將Word文檔轉(zhuǎn)為PDF的兩種方法
相關(guān)文章
Installij IDEA install或clean項(xiàng)目的使用
這篇文章主要介紹了Installij IDEA install或clean項(xiàng)目的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
spring boot項(xiàng)目生成docker鏡像并完成容器部署的方法步驟
這篇文章主要介紹了spring boot項(xiàng)目生成docker鏡像并完成容器部署的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Java遞歸調(diào)用如何實(shí)現(xiàn)數(shù)字的逆序輸出方式
這篇文章主要介紹了Java遞歸調(diào)用如何實(shí)現(xiàn)數(shù)字的逆序輸出方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
使用IDEA創(chuàng)建一個(gè)vert.x項(xiàng)目的方法
這篇文章主要介紹了使用IDEA創(chuàng)建一個(gè)vert.x項(xiàng)目的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
springboot無(wú)法從靜態(tài)上下文中引用非靜態(tài)變量的解決方法
這篇文章主要介紹了springboot無(wú)法從靜態(tài)上下文中引用非靜態(tài)變量的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-06-06
SpringBoot Redis實(shí)現(xiàn)接口冪等性校驗(yàn)方法詳細(xì)講解
這篇文章主要介紹了SpringBoot Redis實(shí)現(xiàn)接口冪等性校驗(yàn)方法,近期一個(gè)老項(xiàng)目出現(xiàn)了接口冪等性校驗(yàn)問(wèn)題,前端加了按鈕置灰,依然被人拉著接口參數(shù)一頓輸出,還是重復(fù)調(diào)用了接口,通過(guò)復(fù)制粘貼,完成了后端接口冪等性調(diào)用校驗(yàn)2022-11-11
SpringBoot中集成Swagger2及簡(jiǎn)單實(shí)用
使用Swagger你只需要按照它的規(guī)范去定義接口及接口相關(guān)的信息,再通過(guò)Swagger衍生出來(lái)的一系列項(xiàng)目和工具,就可以做到生成各種格式的接口文檔,以及在線接口調(diào)試頁(yè)面等等,這篇文章主要介紹了SpringBoot中集成Swagger2,需要的朋友可以參考下2023-06-06

