有两种,1.要求本地安装wps的。
2.下载jar包 aspose-words-16.8.0.jar
踩坑:打包的时候
代码: word转pdf的工具类
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.nio.charset.StandardCharsets;
public class WordToPdfUtil {
/**
* The constant LOG.
*/
private static final Logger LOG = LoggerFactory.getLogger(WordToPdfUtil.class);
/**
* 获取license
*
* @return
*/
private static boolean getLicense() {
boolean result = false;
try {
// 凭证
String licenseStr =
"<License>\n" +
" <Data>\n" +
" <Products>\n" +
" <Product>Aspose.Total for Java</Product>\n" +
" <Product>Aspose.Words for Java</Product>\n" +
" </Products>\n" +
" <EditionType>Enterprise</EditionType>\n" +
" <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +
" <LicenseExpiry>20991231</LicenseExpiry>\n" +
" <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +
" </Data>\n" +
" <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" +
"</License>";
InputStream license = new ByteArrayInputStream(licenseStr.getBytes(StandardCharsets.UTF_8));
License asposeLic = new License();
asposeLic.setLicense(license);
result = true;
} catch (Exception e) {
LOG.error("error:", e);
}
return result;
}
/**
* @param pdfFilePath 生成的pdf路径
* @param docPath doc文件路径
*/
public static void wordToPdf(String pdfFilePath, String docPath) {
FileOutputStream fileOS = null;
// 验证License
if (!getLicense()) {
LOG.error("验证License失败!");
return;
}
try {
InputStream in = new FileInputStream(docPath);
Document doc = new Document(in);
fileOS = new FileOutputStream(new File(pdfFilePath));
// 保存转换的pdf文件
doc.save(fileOS, SaveFormat.PDF);
} catch (Exception e) {
LOG.error("error:", e);
} finally {
try {
if (fileOS != null) {
fileOS.close();
}
} catch (IOException e) {
LOG.error("error:", e);
}
}
}
/**
* @param outputStream pdf文件流
* @param inputStream doc文件流
*/
public static void wordToPdf(OutputStream outputStream, InputStream inputStream) {
// 验证License
if (!getLicense()) {
LOG.error("验证License失败!");
return;
}
try {
Document doc = new Document(inputStream);
// 保存转换的pdf文件
doc.save(outputStream, SaveFormat.PDF);
} catch (Exception e) {
LOG.error("error:", e);
} finally {
try {
if(inputStream!=null){
inputStream.close();
}
} catch (IOException e) {
LOG.error("error:", e);
}
}
}
/**
* @param pdfFilePath pdf文件流
* @param inputStream doc文件流
*/
public static void wordToPdf(String pdfFilePath, InputStream inputStream) {
FileOutputStream fileOS = null;
// 验证License
if (!getLicense()) {
LOG.error("验证License失败!");
return;
}
try {
Document doc = new Document(inputStream);
fileOS = new FileOutputStream(new File(pdfFilePath));
// 保存转换的pdf文件
doc.save(fileOS, SaveFormat.PDF);
} catch (Exception e) {
LOG.error("error:", e);
} finally {
try {
if (fileOS != null) {
fileOS.close();
}
if(inputStream!=null){
inputStream.close();
}
} catch (IOException e) {
LOG.error("error:", e);
}
}
}
}
上传代码
public R preview(MultipartFile file) {
ByteArrayInputStream in = null;
ByteArrayInputStream inFile = null;
ByteArrayOutputStream baos = null;
Map<String, Object> resultMap = new HashMap<>();
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
//将word的输入流 转为pdf的输出流
WordToPdfUtil.wordToPdf(outputStream, file.getInputStream());
//将pdf输出流转为二进制流 然后上传这个流信息
InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
String fileName = IdUtil.simpleUUID() + StrUtil.DOT + "pdf";
resultMap = new HashMap<>(4);
resultMap.put("bucketName", ossProperties.getBucketName());
resultMap.put("fileName", fileName);
String url = String.format("/%s/%s", ossProperties.getBucketName(), fileName);
resultMap.put("url", url);
try {
minioTemplate.putObject(ossProperties.getBucketName(), fileName, inputStream, inputStream.available(), "application/pdf");
// 文件管理数据记录,收集管理追踪文件
Integer fileId = fileLog(file, fileName, url);
resultMap.put("fileId", fileId);
} catch (Exception e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return R.ok(resultMap);
}
打包注意点
<!--${project.basedir}位置不清楚可以点上去看一下,是在引入jar报的目录的根结构处-->
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>16.8.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/aspose-words-16.8.0.jar</systemPath>
</dependency>
如果不能把jar包编入多最后的jar包中,添加一下编译插件(应该可以打包成功,因为jar包在resources中)
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 打包时会将本地jar一起打包 -->
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>