Bläddra i källkod

文件预览,按照客户环境调整完成

wanyuan 5 månader sedan
förälder
incheckning
eb29cd7094

+ 0 - 43
docker/CentOS-7-reg.repo

@@ -1,43 +0,0 @@
-# CentOS-Base.repo
-#
-# The mirror system uses the connecting IP address of the client and the
-# update status of each mirror to pick mirrors that are updated to and
-# geographically close to the client.  You should use this for CentOS updates
-# unless you are manually picking other mirrors.
-#
-# If the mirrorlist= does not work for you, as a fall back you can try the 
-# remarked out baseurl= line instead.
-#
-#
- 
-[base]
-name=CentOS-$releasever - Base - repo.huaweicloud.com
-baseurl=https://repo.huaweicloud.com/centos/$releasever/os/$basearch/
-#mirrorlist=https://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
-gpgcheck=1
-gpgkey=https://repo.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-7
- 
-#released updates 
-[updates]
-name=CentOS-$releasever - Updates - repo.huaweicloud.com
-baseurl=https://repo.huaweicloud.com/centos/$releasever/updates/$basearch/
-#mirrorlist=https://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
-gpgcheck=1
-gpgkey=https://repo.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-7
- 
-#additional packages that may be useful
-[extras]
-name=CentOS-$releasever - Extras - repo.huaweicloud.com
-baseurl=https://repo.huaweicloud.com/centos/$releasever/extras/$basearch/
-#mirrorlist=https://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
-gpgcheck=1
-gpgkey=https://repo.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-7
- 
-#additional packages that extend functionality of existing packages
-[centosplus]
-name=CentOS-$releasever - Plus - repo.huaweicloud.com
-baseurl=https://repo.huaweicloud.com/centos/$releasever/centosplus/$basearch/
-#mirrorlist=https://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
-gpgcheck=1
-enabled=0
-gpgkey=https://repo.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-7

+ 0 - 66
docker/Dockerfile

@@ -1,66 +0,0 @@
-# 基础镜像
-FROM centos:centos7
-
-USER root
-
-#容器内根:/mnt
-WORKDIR /mnt
-
-#安装openjdk17
-COPY ./jdk/openjdk-17+35_linux-x64_bin.tar.gz /mnt/openjdk.tar.gz
-RUN mkdir /usr/local/java && \
-    tar -zxvf /mnt/openjdk.tar.gz -C /usr/local/java/ && \
-    rm -rf /mnt/openjdk.tar.gz
-ENV JAVA_HOME=/usr/local/java/jdk-17
-ENV PATH=$JAVA_HOME/bin:$PATH
-
-# 安装 open office
-COPY ./openoffice/Apache_OpenOffice_4.1.15_Linux_x86-64_install-rpm_zh-CN.tar.gz /mnt/openoffice.tar.gz
-RUN tar -xzf /mnt/openoffice.tar.gz -C /mnt && \
-    cd /mnt/zh-CN/RPMS/ && \
-    rpm -ivh *.rpm && \
-    cd /mnt/zh-CN/RPMS/desktop-integration/ && \
-    rpm -ivh openoffice4.1.15-redhat-menus-4.1.15-9813.noarch.rpm && \
-    rm -rf /mnt/zh-CN
-
-
-# 将 CentOS7基础仓库修改为阿里云仓库,解决 yum报错
-RUN mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
-COPY ./CentOS-7-reg.repo /etc/yum.repos.d/CentOS-Base.repo
-RUN yum clean all && \
-    yum makecache && \
-    yum install deltarpm -y
-
-# 安装缺失的包
-RUN yum install -y libXext.x86_64 freetype && \
-    yum groupinstall -y "X Window System"
-# 3.将我们准备的fonts字体,拷贝到/usr/share/fonts下面
-RUN mkdir -p /usr/share/fonts
-COPY ./openoffice/fonts /usr/share/fonts
-RUN cd /usr/share/fonts/ && \
-  chmod -R 755 /usr/share/fonts && \
-# 4.安装字体,和"X Window System"
-  yum install mkfontscale fontconfig -y && \
-  mkfontscale && \
-  mkfontdir && \
-  fc-cache -fv
-
-
-# 解决中文乱码问题
-ENV LANG en_US.UTF-8
-ENV LANGUAGE en_US.UTF-8
-ENV LC_ALL en_US.UTF-8
-ENV TZ Asia/Shanghai
-
-#指定环境(prod-生产,dev-开发)
-ENV APP_ACTIVE="prod"
-#卷目录(关联日志配置log4j2-prod.xml,关联存储配置application-prod.yml)
-VOLUME ["/mnt/logs","/mnt/storage","/mnt/jar"]
-#暴露容器端口为8012
-EXPOSE 8012
-
-#容器启动时执行的命令
-#ENTRYPOINT /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"-nofirststartwizard &
-ENTRYPOINT nohup java -Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8 -Dspring.profiles.active=${APP_ACTIVE} -jar /mnt/jar/officeOnlineShow.jar
-
-

+ 0 - 1
docker/build_docker.sh

@@ -1 +0,0 @@
-docker build -t office_online_show:v1 .

BIN
docker/jar/officeOnlineShow.jar


+ 34 - 0
docker/officeView/Dockerfile

@@ -0,0 +1,34 @@
+FROM linuxserver/libreoffice:arm64v8-latest
+
+USER root
+
+WORKDIR /mnt
+
+# 设置环境变量
+ENV LANG=zh_CN.UTF-8
+ENV LC_ALL=zh_CN.UTF-8
+ENV LANGUAGE=zh_CN.UTF-8
+ENV TZ=Asia/Shanghai
+ENV JAVA_OPTS="-Xmx1024M -Xms1024M -Xmn768M -XX:MaxMetaspaceSize=256M -XX:MetaspaceSize=256M"
+ENV APP_ACTIVE="prod"
+ENV BOOT_CONFIG=""
+
+VOLUME ["/mnt/logs","/mnt/storage","/mnt/jar"]
+
+# 复制字体文件
+COPY jar /mnt/jar
+COPY libreoffice/fonts /usr/share/fonts/libreoffice
+
+## 更改文件所有者
+#RUN chown -R root:root /usr/share/fonts/libreoffice
+#
+## 设置字体权限
+#RUN chmod -R 777 /usr/share/fonts/libreoffice
+
+# 生成字体缓存
+RUN fc-cache -fv
+
+EXPOSE 8012
+
+# 设置入口点
+ENTRYPOINT nohup java -server ${JAVA_OPTS} -Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8 -Djava.security.egd=file:/dev/./urandom -Dspring.profiles.active=${APP_ACTIVE} -jar /mnt/jar/officeOnlineShow.jar ${BOOT_CONFIG}

+ 1 - 0
docker/officeView/build_docker.sh

@@ -0,0 +1 @@
+docker build -t office-view:v1 .

+ 1 - 0
docker/officeView/run_docker.sh

@@ -0,0 +1 @@
+docker run --name office-view -d -p 8012:8012 -v /mnt/minto/docker/officeView/logs:/mnt/logs -v /mnt/minto/docker/officeView/jar:/mnt/jar -v /mnt/minto/docker/officeView/storage:/mnt/storage office-view:v1

BIN
docker/openoffice/Apache_OpenOffice_4.1.15_Linux_x86-64_install-rpm_zh-CN.tar.gz


+ 0 - 1
docker/run_docker.sh

@@ -1 +0,0 @@
-docker run --name office_online_show -d -p 8012:8012 -v /mnt/office_online_show/docker/logs:/mnt/logs -v /mnt/office_online_show/docker/jar:/mnt/jar -v /mnt/office_online_show/docker/storage:/mnt/storage office_online_show:v1

+ 7 - 16
pom.xml

@@ -5,13 +5,13 @@
     <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
-        <version>3.1.0</version>
+        <version>2.7.18</version>
     </parent>
     <groupId>com.kingtom.office</groupId>
     <artifactId>office-online-show</artifactId>
     <version>1.0</version>
     <properties>
-        <java.version>17</java.version>
+        <java.version>8</java.version>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <jodconverter.version>4.4.7</jodconverter.version>
         <hutool.version>5.8.29</hutool.version>
@@ -30,20 +30,6 @@
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-quartz</artifactId>
         </dependency>
-        <!-- jodconverter-->
-        <dependency>
-            <groupId>org.jodconverter</groupId>
-            <artifactId>jodconverter-core</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>org.jodconverter</groupId>
-            <artifactId>jodconverter-local</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>org.jodconverter</groupId>
-            <artifactId>jodconverter-spring-boot-starter</artifactId>
-        </dependency>
-        <!--lombok-->
         <dependency>
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>
@@ -58,6 +44,11 @@
             <version>4.0.1</version>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-exec</artifactId>
+            <version>1.4.0</version>
+        </dependency>
     </dependencies>
 
     <build>

+ 8 - 0
src/main/java/com/kingtom/office/config/AppConfig.java

@@ -18,5 +18,13 @@ import org.springframework.context.annotation.Configuration;
 @ConfigurationProperties(prefix = "app")
 @Data
 public class AppConfig{
+    /**
+     * 文件保存地址
+     */
     private String fileHome;
+
+    /**
+     * 转换等待时间
+     */
+    private Integer waitTime;
 }

+ 76 - 16
src/main/java/com/kingtom/office/controller/OfficeController.java

@@ -1,14 +1,19 @@
 package com.kingtom.office.controller;
 
 import java.io.*;
+import java.text.MessageFormat;
 import java.util.UUID;
 
+import javax.servlet.http.HttpServletResponse;
+
 import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.io.FileUtil;
 import com.kingtom.office.config.AppConfig;
-import jakarta.servlet.http.HttpServletResponse;
-import org.jodconverter.core.DocumentConverter;
-import org.jodconverter.core.office.OfficeException;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.exec.CommandLine;
+import org.apache.commons.exec.DefaultExecutor;
+import org.apache.commons.exec.ExecuteException;
+import org.apache.commons.exec.ExecuteResultHandler;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.util.StringUtils;
@@ -29,11 +34,9 @@ import org.springframework.web.multipart.MultipartFile;
  */
 @Controller
 @RequestMapping("office")
+@Slf4j
 public class OfficeController{
 
-    @Autowired
-    private DocumentConverter converter;
-
     @Autowired
     private AppConfig appConfig;
 
@@ -42,16 +45,18 @@ public class OfficeController{
 
     @PostMapping("toPdf")
     public void toPdf(@RequestParam("file") MultipartFile file, @RequestParam("fileType") String fileType,
-            HttpServletResponse response) throws IOException, OfficeException{
-        String uuid = StringUtils.replace(UUID.randomUUID().toString(), "-", "");
-
-        File srcFile = new File(getFilePath() + uuid + fileType);
-        FileUtil.copyFile(file.getInputStream(), srcFile);
-
-        String fileName = uuid + ".pdf";
-        File targetFile = new File(getFilePath() + fileName);
-        converter.convert(srcFile).to(targetFile).execute();
-        output(response, fileName, targetFile);
+            HttpServletResponse response) throws Exception{
+        try{
+            String uuid = StringUtils.replace(UUID.randomUUID().toString(), "-", "");
+            File srcFile = new File(getFilePath() + uuid + "." + fileType);
+            FileUtil.copyFile(file.getInputStream(), srcFile);
+            File targetFile = targetFile = libreOfficeConvert(srcFile);
+            //File targetFile = asposeConvert(srcFile);
+            output(response, targetFile.getName(), targetFile);
+        } catch(Exception e){
+            log.error(e.getMessage(), e);
+            throw new Exception("转换失败!");
+        }
     }
 
     private static void output(HttpServletResponse response, String fileName, File file) throws IOException{
@@ -81,4 +86,59 @@ public class OfficeController{
     private String getFilePath(){
         return appConfig.getFileHome() + DateUtil.format(DateUtil.date(), FORMAT);
     }
+
+    private synchronized File libreOfficeConvert(File officeFile) throws IOException, InterruptedException{
+        DefaultExecutor exec = new DefaultExecutor();
+        ExecuteResultHandler erh = new ExecuteResultHandler(){
+            @Override
+            public void onProcessComplete(int exitValue){
+                log.info(" 转换完成! Exit value: {}", exitValue);
+            }
+
+            @Override
+            public void onProcessFailed(ExecuteException e){
+                log.error(" 转换失败!", e);
+            }
+        };
+        File tempFolder = new File(getFilePath());
+        String osName = System.getProperty("os.name");
+        String command = null;
+        if(osName.contains("Windows")){
+            command = "cmd /c start soffice --headless --invisible --convert-to pdf:writer_pdf_Export \"{0}\" "
+                    + "--outdir \"{1}\"";
+        } else{
+            String fileType = officeFile.getName().substring(officeFile.getName().lastIndexOf("."));
+            String cmd;
+            switch(fileType){
+                case ".docx":
+                case ".doc":
+                    cmd = "writer";
+                    break;
+                case ".ppt":
+                case ".pptx":
+                    cmd = "impress";
+                    break;
+                case ".xls":
+                case ".xlsx":
+                    cmd = "calc";
+                    break;
+                default:
+                    throw new RuntimeException("不支持的文件类型");
+            }
+            command = "libreoffice --headless --invisible --convert-to "
+                    + "pdf:writer_pdf_Export \"{0}\" --outdir \"{1}\"";
+        }
+        command = MessageFormat.format(command, officeFile.getAbsolutePath(), tempFolder.getAbsolutePath());
+        log.info("执行office文件转换任务,命令为{}", command);
+        exec.execute(CommandLine.parse(command), erh);
+        File outputFile = new File(tempFolder.getAbsolutePath() + File.separator + officeFile.getName()
+                .substring(0, officeFile.getName().indexOf(".")) + ".pdf");
+        //CountDownLatch latch = new CountDownLatch(1);不生效,所以换成了循环睡眠
+        int time = 0;
+        while((!outputFile.exists()) && time < appConfig.getWaitTime()){
+            Thread.sleep(1000);
+            time++;
+        }
+        return outputFile;
+    }
 }

+ 1 - 7
src/main/resources/application-local.yml

@@ -9,13 +9,7 @@ spring:
       max-request-size: 200MB
 app:
   file-home: D:/pdf
-jodconverter:
-  local:
-    enabled: true
-    office-home: C:/Program Files (x86)/OpenOffice 4
-    port-numbers:
-      - 10000
-      - 10001
+  wait-time: 30
 #logging:
 logging:
   level:

+ 1 - 26
src/main/resources/application-prod.yml

@@ -9,32 +9,7 @@ spring:
       max-request-size: 300MB
 app:
   file-home: /mnt/storage
-jodconverter:
-  local:
-    enabled: true
-    office-home: /opt/openoffice4
-#    office-home: /opt/openoffice4/program
-    port-numbers:
-      - 10000
-      - 10001
-      - 10002
-      - 10003
-      - 10004
-      - 10005
-      - 10006
-      - 10007
-      - 10008
-      - 10009
-      - 10010
-      - 10011
-      - 10012
-      - 10013
-      - 10014
-      - 10015
-      - 10016
-      - 10017
-      - 10018
-      - 10019
+  wait-time: 60
 logging:
   level:
     root: info