实现使用jodconverter调用libreoffice将word转pdf

This commit is contained in:
2025-07-01 11:50:59 +08:00
parent 4483f3ef4c
commit a4c4994759
32 changed files with 636 additions and 2282 deletions

58
document-api/pom.xml Normal file
View File

@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.optima</groupId>
<artifactId>document</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>document-api</artifactId>
<name>Document API</name>
<description>文档操作api</description>
<properties>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 1. 使用 maven-jar-plugin 打包当前模块的代码(不含依赖) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<!-- 可选:指定主类(如果需要可执行) -->
<archive>
<manifest>
<mainClass>com.example.MyApp</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!-- 2. 使用 maven-source-plugin 生成源码 JAR -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,94 @@
package com.optima.document.api;
import java.util.Map;
/**
* @author yanghuanglin
* @since 2022/12/28
*/
public interface BaseDocumentService {
/**
* 格式转换
*
* @param source 源文件流仅支持docx格式
* @param sourceExtension 源文件后缀名,不包含"."
* @param targetExtension 目标文件后缀名,不包含"."
* @return 转换后的文件流
*/
default byte[] convert(byte[] source, String sourceExtension, String targetExtension) {
return convert(source, sourceExtension, targetExtension, null);
}
/**
* 格式转换
*
* @param source 源文件流仅支持docx格式
* @param sourceExtension 源文件后缀名,不包含"."
* @param targetExtension 目标文件后缀名,不包含"."
* @param targetFormat 目标文件格式,需与目标文件后缀名匹配。
* server下此参数无效。
* tl-server下参考:
* <a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.word.wdsaveformat">word格式</a>
* 或 <a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.xlfileformat">excel格式</a>
* 或 <a href="https://docs.microsoft.com/en-us/office/vba/api/powerpoint.presentation.saveas">powerpoint格式</a>
* @return 转换后的文件流
*/
default byte[] convert(byte[] source, String sourceExtension, String targetExtension, String targetFormat) {
throw new UnsupportedOperationException();
}
/**
* 通过调用poi生成word
*
* @param sourceTemplate word模版流仅支持docx格式
* @param dataModel 数据模型
* @return word文档流
*/
default byte[] generateWord(byte[] sourceTemplate, Map<String, Object> dataModel) {
throw new UnsupportedOperationException();
}
/**
* word转为pdf
*
* @param source word文件流仅支持docx格式
* @param sourceFormat 源文件后缀名,不包含"."tl-server下此参数无效
* @param clear 是否清除占位符如果为true则在tl-server下源文件只支持docx格式
* @return pdf文档流
*/
default byte[] wordToPdf(byte[] source, String sourceFormat, boolean clear) {
throw new UnsupportedOperationException();
}
/**
* 通过pdfbox将word转图片
*
* @param source word文件流仅支持docx格式
* @param sourceExtension 源文件后缀名,不包含"."server下此参数无效
* @param targetExtension 目标格式 支持jpeg, jpg, gif, tiff or png
* @return 图片流
*/
default byte[] wordToImage(byte[] source, String sourceExtension, String targetExtension) {
throw new UnsupportedOperationException();
}
/**
* doc转为docx
*
* @param source doc文档流
* @return docx文档流
*/
default byte[] docToDocx(byte[] source) {
throw new UnsupportedOperationException();
}
/**
* xls转为xlsx
*
* @param source xls文档流
* @return xlsx文档流
*/
default byte[] xlsToXlsx(byte[] source) {
throw new UnsupportedOperationException();
}
}

View File

@@ -0,0 +1,31 @@
package com.optima.document.api;
/**
* 文档接口此类中用的poi2生成word文档使用docto进行格式转换
*
* @author yanghuanglin
* @since 2022/12/28
*/
public interface DocumentService extends BaseDocumentService {
/**
* 通过调用poi将word转pdf如果clear为true则仅支持docx格式
*
* @param source word模版流仅支持docx格式
* @param clear 是否清除占位符
* @return pdf文档流
*/
default byte[] wordToPdf(byte[] source, boolean clear) {
throw new UnsupportedOperationException();
}
/**
* 通过pdfbox将word转图片仅支持docx格式
*
* @param source word文件流仅支持docx格式
* @param targetExtension 目标格式 支持jpeg, jpg, gif, tiff or png
* @return 图片流
*/
default byte[] wordToImage(byte[] source, String targetExtension) {
throw new UnsupportedOperationException();
}
}