关于word文档转PDF
需要依赖
地址:阿里云地址 https://www.aliyundrive.com/s/fsSKWGAD21M
依赖可直接使用
使用此Maven命令安装jar包到本地仓库
mvn install:install-file -Dfile=jar包地址 -DgroupId=com.aspose -DartifactId=aspose-words -Dversion=15.8.0 -Dpackaging=jar
mvn install:install-file -Dfile=C:\Users\Administrator.m2\repository\aspose-words-15.8.0-jdk16.jar -DgroupId=com.aspose -DartifactId=aspose-words -Dversion=15.8.0 -Dpackaging=jar
license.xml文件内容(放在/src/main/resources目录下)
Aspose.Total for Java Aspose.Words for Java Enterprise 20991231 20991231 8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7 sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU= ## 依赖 com.aspose aspose-words 15.8.0license.xml文件内容(放在/src/main/resources目录下)
// An highlighted block
<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>
WordPdfUtil.java——封装工具类
package com.example.demo.test.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Word 转 Pdf 帮助类
*
* @author admin
*
*/
public class WordPdfUtil {
public static void main(String[] args) {
private static boolean license = false;
static {
try {
// license.xml放在src/main/resources文件夹下
InputStream is = WordPdfUtil.class.getClassLoader().getResourceAsStream("license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
license = true;
} catch (Exception e) {
license = false;
System.out.println("License验证失败...");
e.printStackTrace();
}
}
/**
* doc转pdf
*
* @param wordPath
* @param pdfPath
*/
@PostMapping("/")
public static void doc2pdf(String wordPath, String pdfPath) {
// 验证License 若不验证则转化出的pdf文档会有水印产生
if (!license) {
System.out.println("License验证不通过...");
return;
}
try {
long old = System.currentTimeMillis();
//新建一个pdf文档
File file = new File(pdfPath);
FileOutputStream os = new FileOutputStream(file);
//Address是将要被转化的word文档
Document doc = new Document(wordPath);
//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
doc.save(os, SaveFormat.PDF);
long now = System.currentTimeMillis();
os.close();
//转化用时
System.out.println("Word 转 Pdf 共耗时:" + ((now - old) / 1000.0) + "秒");
} catch (Exception e) {
System.out.println("Word 转 Pdf 失败...");
e.printStackTrace();
}
}
}
}
公网地址转换返回pdf
/**
* doc转pdf
*
* @param response
* @param url
*/
public static void doc2pdf(HttpServletResponse response, String url) throws Exception {
// 验证License 若不验证则转化出的pdf文档会有水印产生
if (!license) {
log.warn("License验证不通过...");
return;
}
//"https://tinyzk-pro-per-cdn.1haozc.com/587138d5-9cd7-4d85-9627-cddc65240f4f.docx"
Document doc = new Document(url);
//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
doc.save(response.getOutputStream(), SaveFormat.PDF);
}