| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- package com.ruoyi.common.utils;
-
- import cn.hutool.extra.spring.SpringUtil;
-
- import com.artofsolving.jodconverter.DocumentConverter;
- import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
- import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
- import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
- import com.ruoyi.common.exception.ServiceException;
- import lombok.extern.slf4j.Slf4j;
- import org.jodconverter.document.DocumentFormat;
- import org.jodconverter.office.OfficeException;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.http.MediaType;
-
-
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
-
- /**
- * LibreOffice工具类,用于将word,excel,ppt等格式文件转为pdf预览
- *
- * @author xuyuxiang
- * @date 2020/7/6 14:55
- */
- @Slf4j
- public class LibreOfficeUtil {
- @Value("${jodconverter.local.office-home}")
- private long OpenOffice_HOME;
-
- private static DocumentConverter documentConverter;
-
- private static void init() {
- try {
- documentConverter = SpringUtil.getBean(DocumentConverter.class);
- } catch (Exception e) {
- throw new ServiceException();
- }
- }
-
-
- public static boolean doc2pdf(File docFile, File pdfFile) {
- boolean result = false;// 转换结果
- if (docFile.exists()) {
- if (!pdfFile.exists()) {
- OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
- try {
- connection.connect();
- DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
- converter.convert(docFile, pdfFile);
- // 关闭连接
- connection.disconnect();
- result = true;
-
- log.info("****pdf转换成功,PDF输出:" + pdfFile.getPath() + "****");
- } catch (java.net.ConnectException e) {
- log.error("openoffice服务未启动", e);
- } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
- log.error("读取转换文件失败", e);
- } catch (Exception e) {
- log.error("转换失败", e);
- }
- } else {
- result = true;
- log.info("****已经转换为pdf,不需要再进行转化****");
- }
- } else {
- log.info("****需要转换的文档不存在,无法转换****");
- }
- return result;
- }
- /**
- * doc转pdf(程序启动openoffice)
- *
- * @param inputFile 输入文件
- * @param outputFile 输出文件
- * @return
- */
- public static boolean doc2pdf2(File inputFile, File outputFile) {
- boolean result = false;
- // OpenOffice的安装目录
- String OpenOffice_HOME = "D:\\app\\libreOffice";
- if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '/') {
- OpenOffice_HOME += "/";
- }
- Process process = null;
- try {
- // 启动OpenOffice的服务
- String command = OpenOffice_HOME
- + "program/soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";
- process = Runtime.getRuntime().exec(command);
- // 连接 OpenOffice实例,运行在8100端口
- OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
- connection.connect();
-
- // 转换
- DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
- converter.convert(inputFile, outputFile);
-
- // 关闭连接
- connection.disconnect();
- // 销毁OpenOffice服务的进程
- process.destroy();
-
- log.info("****pdf转换成功,PDF输出:" + outputFile.getPath() + "****");
- return true;
- } catch (Exception e) {
- log.error("pdf转换失败", e);
- } finally {
- if (process != null) {
- process.destroy();
- }
- }
- return result;
- }
-
- }
|