MsOffice转PDF开源方案大致如下,如有错误请指正补充
- docx4j(可远程/本地)
- documents4j(本地且支持windows)
- jodconverter+libreoffice(windows+linux)
- jodconverter+openoffice(windows+linux)
- XDocReport(暂未试验)
如需完美转换,最好的方式仍然是使用windows,利用安装的office组件转换
一、docx4j相关
- 官方:https://www.docx4java.org/trac/docx4j
- github:https://www.docx4java.org/trac/docx4j
- 开源版与企业版比较:https://www.plutext.com/index.html?_ga=2.60354084.806085802.1650340826-1443275412.1650340826
- docx4j 8.2.3 提供了 3 种不同的方法来将 Microsoft Word docx 文档转换为 PDF。也可以将pptx或xlsx转换为PDF:https://www.docx4java.org/blog/2020/09/office-pptxxlsxdocx-to-pdf-to-in-docx4j-8-2-3/
二、环境说明
- java版本:1.8
- springboot:2.6.6
- 相关依赖:
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-Internal</artifactId>
<version>8.3.4</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
<version>8.3.4</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-MOXy</artifactId>
<version>8.3.4</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.docx4j</groupId>-->
<!-- <artifactId>docx4j-export-fo</artifactId>-->
<!-- <version>8.3.4</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-documents4j-local</artifactId>
<version>8.3.4</version>
</dependency>
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-transformer-msoffice-excel</artifactId>
<version>1.1.7</version>
</dependency>
三、代码实现
import com.documents4j.api.DocumentType;
import org.docx4j.documents4j.local.Documents4jLocalServices;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import static com.jc.officefileconvert.util.Itext7PdfUtils.getCtm;
import static com.jc.officefileconvert.util.Itext7PdfUtils.getServerBasePath;
public class Docx4jOfficeFileToPDF {
public static File transferToFile(MultipartFile multipartFile) {
// 选择用缓冲区来实现这个转换即使用java 创建的临时文件 使用 MultipartFile.transferto()方法 。
File file = null;
try {
String originalFilename = multipartFile.getOriginalFilename();
String[] filename = originalFilename.split("\\.");
file=File.createTempFile(filename[0], filename[1]);
multipartFile.transferTo(file);
file.deleteOnExit();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
public static String docx2Pdf(MultipartFile file) {
String relativelyPath = String.format("pdf/%s.pdf", getCtm());
String path = CommonConfig.getFileSavePath() + relativelyPath;
File output = new File(path);
if (!output.exists()){
output.getParentFile().mkdirs();
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(output);
Documents4jLocalServices exporter = new Documents4jLocalServices ();
exporter.export(transferToFile(file), fos, DocumentType.MS_WORD);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return getServerBasePath() + relativelyPath;
}
public static String pptx2Pdf(MultipartFile file) {
String relativelyPath = String.format("pdf/%s.pdf", getCtm());
String path = CommonConfig.getFileSavePath() + relativelyPath;
File output = new File(path);
if (!output.exists()){
output.getParentFile().mkdirs();
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(output);
Documents4jLocalServices exporter = new Documents4jLocalServices ();
exporter.export(transferToFile(file), fos, DocumentType.MS_POWERPOINT);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return getServerBasePath() + relativelyPath;
}
public static String xlsx2Pdf(MultipartFile file) {
String relativelyPath = String.format("pdf/%s.pdf", getCtm());
String path = CommonConfig.getFileSavePath() + relativelyPath;
File output = new File(path);
if (!output.exists()){
output.getParentFile().mkdirs();
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(output);
Documents4jLocalServices exporter = new Documents4jLocalServices ();
exporter.export(transferToFile(file), fos, DocumentType.MS_EXCEL);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return getServerBasePath() + relativelyPath;
}
public static void main(String[] args) {
}
}