去除BaseDocumentService,方法都放到DocumentService
This commit is contained in:
@@ -28,14 +28,6 @@
|
|||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-jar-plugin</artifactId>
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
<version>3.3.0</version>
|
<version>3.3.0</version>
|
||||||
<configuration>
|
|
||||||
<!-- 可选:指定主类(如果需要可执行) -->
|
|
||||||
<archive>
|
|
||||||
<manifest>
|
|
||||||
<mainClass>com.example.MyApp</mainClass>
|
|
||||||
</manifest>
|
|
||||||
</archive>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
<!-- 2. 使用 maven-source-plugin 生成源码 JAR -->
|
<!-- 2. 使用 maven-source-plugin 生成源码 JAR -->
|
||||||
|
|||||||
@@ -1,94 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,12 +1,43 @@
|
|||||||
package com.optima.document.api;
|
package com.optima.document.api;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文档接口,此类中用的poi2生成word文档,使用docto进行格式转换
|
* 文档操作接口
|
||||||
*
|
*
|
||||||
* @author yanghuanglin
|
* @author yanghuanglin
|
||||||
* @since 2022/12/28
|
* @since 2022/12/28
|
||||||
*/
|
*/
|
||||||
public interface DocumentService extends BaseDocumentService {
|
public interface DocumentService {
|
||||||
|
/**
|
||||||
|
* 格式转换
|
||||||
|
*
|
||||||
|
* @param source 源文件流,仅支持docx格式
|
||||||
|
* @param sourceExtension 源文件后缀名,不包含"."
|
||||||
|
* @param targetExtension 目标文件后缀名,不包含"."
|
||||||
|
* @param targetFormat 目标文件格式,需与目标文件后缀名匹配。
|
||||||
|
* jodconverter-document-server 下此参数无效。
|
||||||
|
* docto-document-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();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过调用poi将word转pdf,如果clear为true,则仅支持docx格式
|
* 通过调用poi将word转pdf,如果clear为true,则仅支持docx格式
|
||||||
*
|
*
|
||||||
@@ -28,4 +59,24 @@ public interface DocumentService extends BaseDocumentService {
|
|||||||
default byte[] wordToImage(byte[] source, String targetExtension) {
|
default byte[] wordToImage(byte[] source, String targetExtension) {
|
||||||
throw new UnsupportedOperationException();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -135,10 +135,6 @@ public class DocumentServiceImpl implements DocumentService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] wordToPdf(byte[] source, String sourceFormat, boolean clear) {
|
|
||||||
return wordToPdf(source, clear);
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] wordToPdf(byte[] source, boolean clear) {
|
public byte[] wordToPdf(byte[] source, boolean clear) {
|
||||||
try {
|
try {
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
@@ -185,10 +181,6 @@ public class DocumentServiceImpl implements DocumentService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] wordToImage(byte[] source, String sourceExtension, String targetExtension) {
|
|
||||||
return wordToImage(source, targetExtension);
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] docToDocx(byte[] source) {
|
public byte[] docToDocx(byte[] source) {
|
||||||
return convert(source, "doc", "docx", "wdFormatDocumentDefault");
|
return convert(source, "doc", "docx", "wdFormatDocumentDefault");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -91,7 +91,7 @@ public class DocumentServiceImpl implements DocumentService {
|
|||||||
* @param targetExtension 目标文件后缀名
|
* @param targetExtension 目标文件后缀名
|
||||||
* @return 转换后的文件流
|
* @return 转换后的文件流
|
||||||
*/
|
*/
|
||||||
public byte[] convert(byte[] source, String sourceExtension, String targetExtension) {
|
private byte[] convert(byte[] source, String sourceExtension, String targetExtension) {
|
||||||
try {
|
try {
|
||||||
sourceExtension = sourceExtension.replace(".", "");
|
sourceExtension = sourceExtension.replace(".", "");
|
||||||
targetExtension = targetExtension.replace(".", "");
|
targetExtension = targetExtension.replace(".", "");
|
||||||
@@ -131,10 +131,6 @@ public class DocumentServiceImpl implements DocumentService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] wordToPdf(byte[] source, String sourceFormat, boolean clear) {
|
|
||||||
return wordToPdf(source, clear);
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] wordToPdf(byte[] source, boolean clear) {
|
public byte[] wordToPdf(byte[] source, boolean clear) {
|
||||||
try {
|
try {
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
@@ -181,10 +177,6 @@ public class DocumentServiceImpl implements DocumentService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] wordToImage(byte[] source, String sourceExtension, String targetExtension) {
|
|
||||||
return wordToImage(source, targetExtension);
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] docToDocx(byte[] source) {
|
public byte[] docToDocx(byte[] source) {
|
||||||
return convert(source, "doc", "docx");
|
return convert(source, "doc", "docx");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,33 +45,28 @@
|
|||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<!-- 1. 使用 maven-jar-plugin 打包当前模块的代码(不含依赖) -->
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-jar-plugin</artifactId>
|
<artifactId>maven-resources-plugin</artifactId>
|
||||||
<version>3.3.0</version>
|
<version>3.3.1</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>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<id>attach-sources</id>
|
<id>copy-resources</id>
|
||||||
<phase>package</phase>
|
<phase>process-resources</phase>
|
||||||
<goals>
|
<goals>
|
||||||
<goal>jar-no-fork</goal>
|
<goal>copy-resources</goal>
|
||||||
</goals>
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<outputDirectory>${project.build.directory}</outputDirectory>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
<includes>
|
||||||
|
<include>application.yml</include>
|
||||||
|
</includes>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
</configuration>
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|||||||
@@ -9,6 +9,9 @@ import javax.annotation.Resource;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class DocumentConverter {
|
public class DocumentConverter {
|
||||||
@@ -29,10 +32,19 @@ public class DocumentConverter {
|
|||||||
targetFile.delete();
|
targetFile.delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
String sourceExtension = sourceFile.getName().substring(sourceFile.getName().lastIndexOf("."));
|
Map<String, Object> params = new HashMap<>();
|
||||||
String targetExtension = targetFile.getName().substring(targetFile.getName().lastIndexOf("."));
|
params.put("callerName", "张三");
|
||||||
|
|
||||||
byte[] converted = documentService.convert(Files.readAllBytes(sourceFile.toPath()), sourceExtension, targetExtension);
|
// 生成word,并转为pdf
|
||||||
Files.write(targetFile.toPath(), converted);
|
byte[] generatedWord = documentService.generateWord(Files.readAllBytes(sourceFile.toPath()), params);
|
||||||
|
byte[] wordedToPdf = documentService.wordToPdf(generatedWord, true);
|
||||||
|
Path pdfPath = targetFile.toPath();
|
||||||
|
Files.write(pdfPath, wordedToPdf);
|
||||||
|
|
||||||
|
// 生成的word转为图片
|
||||||
|
byte[] wordedToImage = documentService.wordToImage(generatedWord, "jpg");
|
||||||
|
File imageFile = new File(targetFile.getAbsolutePath().replace(".pdf", ".jpg"));
|
||||||
|
Path imagePath = imageFile.toPath();
|
||||||
|
Files.write(imagePath, wordedToImage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user