Commit 0af574fa authored by alex yao's avatar alex yao

feat(FileUtils): Word文件转换PDF工具

parent 95e34fd8
...@@ -365,6 +365,11 @@ ...@@ -365,6 +365,11 @@
<version>1.12</version> <version>1.12</version>
</dependency> </dependency>
<dependency>
<groupId>aspose-words</groupId>
<artifactId>words</artifactId>
<version>21.1</version>
</dependency>
</dependencies> </dependencies>
......
package cn.com.poc.common.utils; package cn.com.poc.common.utils;
import cn.com.yict.framemax.core.exception.BusinessException;
import com.aspose.words.SaveFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Date; import java.util.Date;
...@@ -13,6 +18,8 @@ import java.util.UUID; ...@@ -13,6 +18,8 @@ import java.util.UUID;
*/ */
public class FileUtils { public class FileUtils {
private static Logger logger = LoggerFactory.getLogger(FileUtils.class);
/** /**
* 格式化文件大小 * 格式化文件大小
*/ */
...@@ -65,4 +72,30 @@ public class FileUtils { ...@@ -65,4 +72,30 @@ public class FileUtils {
} }
return null; return null;
} }
/**
* word转pdf文件转换工具
*
* @param docFile doc文件 / docx文件
* @return
*/
public static File wordConvertPDF(File docFile) {
try {
FileInputStream fileInputStream = new FileInputStream(docFile);
com.aspose.words.Document doc = new com.aspose.words.Document(fileInputStream);
File tempFile = File.createTempFile(UUID.randomUUID().toString(), ".pdf");
FileOutputStream os = new FileOutputStream(tempFile);
doc.save(os, SaveFormat.PDF);
os.close();
fileInputStream.close();
return tempFile;
} catch (IOException e) {
logger.error("doc2pdf error:" + e.getMessage());
throw new BusinessException("文件加载失败");
} catch (Exception e) {
logger.error("doc2pdf error:" + e.getMessage());
throw new BusinessException("文件解析失败");
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment