1、引入对应jar依赖
阿里云没有包含aspose-words的依赖包
所以需要自己手动下载jar包,并引入到项目中
aspose-words-15.8.0-jdk16.jar
引入方式
1.1、安装到本地仓库的方式
1.1.1、先安装jar到本地仓库
mvn install:install-file -Dfile=/path/to/your/aspose-words-15.8.0-jdk16.jar -DgroupId=com.aspose -DartifactId=aspose-words -Dversion=15.8.0 -Dpackaging=jar
参数解释
-Dfile:本地 JAR 文件的路径。
-DgroupId:依赖项的组ID。在这里是 com.aspose。
-DartifactId:依赖项的工件ID。在这里是 aspose-words。
-Dversion:依赖项的版本号。在这里是 15.8.0。
-Dpackaging:包类型。在这里是 jar。
1.1.2、在 pom.xml 中引用
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>15.8.0</version>
</dependency>
1.2、系统范围依赖的方式
1.2.1、先将jar包放到项目resource目录下
1.2.2、配置maven指定本地jar路径依赖
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>15.8.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/aspose-words-15.8.0-jdk16.jar</systemPath>
</dependency>
通过 scope 设置为 system 并指定 systemPath 可以告诉 Maven 使用本地提供的 JAR 文件。
<scope>system</scope> 的用途
- 本地引用:
使用 system 作用范围的主要原因是该依赖项没有在公共 Maven 仓库中发布,或者需要使用本地提供的 JAR 文件。此时,依赖项必须通过 指定一个本地路径。 - 避免从远程仓库下载:
system 作用范围告诉 Maven 不要尝试从远程仓库下载这个依赖项。Maven 会直接使用指定的本地路径中的 JAR 文件。
2、在resource路径下配置license.xml文件
license.xml内容
<?xml version="1.0" encoding="UTF-8" ?>
<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>
3、转换工具类
package com.jt.util;
import cn.hutool.system.OsInfo;
import cn.hutool.system.SystemUtil;
import com.aspose.words.Document;
import com.aspose.words.FontSettings;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
/**
* @Author: zhoujinchuan
* @Description: (请添加描述)
* @CreateTime: 2024/5/27 9:31
*/
@Component
public class WordPdfUtil {
// @Value("${linux.fonts.path}")
// private String fontPath;
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;
e.printStackTrace();
}
}
public void doc2pdf(String wordPath, String pdfPath) {
// 验证License 若不验证则转化出的pdf文档会有水印产生
if (!license) {
System.out.println("License验证不通过...");
return;
}
try {
File file = new File(pdfPath);
FileOutputStream os = new FileOutputStream(file);
Document doc = new Document(wordPath);
// 支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
// OsInfo osInfo = SystemUtil.getOsInfo();
// if(osInfo.isLinux()){
// FontSettings.setFontsFolder(fontPath, true);
// }
doc.save(os, SaveFormat.PDF);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void doc2pdf(InputStream wordInputStream, OutputStream pdfOutputStream) {
// 验证License 若不验证则转化出的pdf文档会有水印产生
if (!license) {
System.out.println("License验证不通过...");
return;
}
try {
Document doc = new Document(wordInputStream);
// OsInfo osInfo = SystemUtil.getOsInfo();
// if(osInfo.isLinux()){
// FontSettings.setFontsFolder(fontPath, true);
// }
// 支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
doc.save(pdfOutputStream, SaveFormat.PDF);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Linux环境aspose插件word转pdf中文乱码解决方案
- 代码中指定字体路径的方式
//fontPath为字体路径
FontSettings.setFontsFolder(fontPath, true);
- 加载到系统字体的方式
# 创建目录,将win机器的c:\windows\fonts目录下的全部文件拷贝到此目录下
mkdir mkdir /usr/share/fonts/win
cd /usr/share/fonts
#更新和刷新字体缓存的
fc-cache -fv
# 执行命令让字体生效
source /etc/profile