提示:Java使用Aspose技术将word文件转换成pdf文件
文章目录
- 一、介绍
- 二、下载依赖并引入jar包
- 三、编写功能
一、介绍
Java语言使用Aspose技术将word转换成pdf文件的功能,以及部署服务器遇到的坑。
二、下载依赖并引入jar包
下载aspose-words-15.8.0-jdk16.jar
https://download.csdn/download/qq_44753710/87454465?spm=1001.2014.3001.5503
1、解压下载的aspose-words-15.8.0-jdk16获取jar包
2、将jar包放入项目中:这里放在boot的resources目录中并将lib包添加包管理器中。
三、编写功能
1、在pom中引入包的路径
注意:只引入依赖包,部署服务器上会报找不到依赖的错误也有可能报
cn.hutool.core.io.resource.NoResourceException: Resource of path [static/license.xml] not exist!
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.3.7</version>
</dependency>
// 引入aspose-words-15.8.0-jdk16依赖包
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>15.8.0</version>
<scope>system</scope>
<!--路径是aspose-words-15.8.0-jdk16包放的路径-->
<systemPath>${project.basedir}/src/main/resources/lib/aspose-words-15.8.0-jdk16.jar</systemPath>
</dependency>
<!--需要在pom中加入一下代码服务器上才能正常使用 不然打包时候报程序包com.aspose.words不存在-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--将第三方包引入不加这个是linux上是没有的-->
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
2、创建license.xml文件 在resources目录中创建static文件夹中创建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、创建Word2PdfAsposeUtil工具类
import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.io.resource.Resource;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
public class Word2PdfAsposeUtil {
// 添加日志
private static final Logger logger = LoggerFactory.getLogger(PDF.utils.Word2PdfAsposeUtil.class);
// 读取license.xml的内容
public static boolean getLicense() {
boolean result = false;
Resource resource = new ClassPathResource("static/license.xml");
try (InputStream is = resource.getStream()) {
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
*
* @param inPath 源文件路径 .docx文件
* @param outPath 生成的文件 .pdf文件
* @return
*/
public static boolean doc2pdf(String inPath, String outPath) {
if (!getLicense()) {
logger.error("license获取失败");
return false;
}
try (FileOutputStream os = new FileOutputStream(new File(outPath))) {
long old = System.currentTimeMillis();
Document doc = new Document(inPath);
doc.save(os, SaveFormat.PDF);
long now = System.currentTimeMillis();
logger.info("pdf转换成功,共耗时:" + ((now - old) / 1000.0) + "秒");
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
// main 方法测试工具类
public static void main(String[] args) {
//调用授权方法 源文件路径
String sourceFile = "C:\\Users\\user\\Desktop\\11\\test.docx";
// 生成的路径
String targetFile = "C:\\Users\\user\\Desktop\\11\\test.pdf";
// 调用方法
doc2pdf(sourceFile,targetFile);
}
}
注意:如果生成报错就是jar报引入不成功 生成的编译文件中没有引入依赖,在pom的bulid中加入以下代码就可以成功
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--将第三方包引入不加这个是linux上是没有的-->
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
更多推荐
将word文档转换成pdf格式【使用Aspose技术实现:亲测可用】
发布评论