Centos 搭建 KKFileViewer 服务

kkFileView 为文件文档在线预览解决方案,该项目使用流行的 spring boot 搭建,易上手和部署,基本支持主流办公文档的在线预览,如 doc,docx,xls,xlsx,ppt,pptx,pdf,txt,zip,rar,图片,视频,音频等等

环境要求

  1. Java: 1.8+
  2. OpenOffice 或 LiberOffice

操作步骤

  1. 下载 kkFileView 码云发行版本 下载最新版发行包

  2. 下载 LiberOffice 官网下载地址

  3. 下载完成之后上传到服务器 /opt 文件夹内

  4. 解压 KKFileViewer

    1
    tar -xzvf kkFileView-4.0.0.tar.gz
  5. 解压 LiberOffice,然后进去文件夹内、用 yum下载需要的文件包

    1
    2
    3
    4
    5
    tar -xzvf LibreOffice_7.2.7_Linux_x86-64_rpm.tar.gz

    cd LibreOffice_7.2.7_Linux_x86-64_rpm/RPMS

    yum localinstall *.rpm
  6. yum 下载完成之后,查看版本

    1
    2
    3
    cd /opt/libreoffice7.2/program

    ./soffice --version
  7. 出现版本信息,则为安装成功

  8. 如果未出现版本信息,有报错,一般都是缺少相关依赖包。报错提示缺少什麽包 就yum安装什麽包,直到出现正确的 版本信息。例如:

    • 提示找不到 libcairo

      1
      2
      3
      4
      [root@localhost uploads]# libreoffice6.0 --invisible --convert-to html test.doc
      javaldx: Could not find a Java Runtime Environment!
      Warning: failed to read path from javaldx
      /opt/libreoffice6.0/program/soffice.bin: error while loading shared libraries: libcairo.so.2: cannot open shared object file: No such file or directory

      安装 yum -y install cairo

    • 如果出现如下提示:

      1
      2
      3
      4
      5
      libreoffice6.0  d9755f55-6722-4e74-a7c7-b74f4ac09ec0.docx
      Failed to open display
      javaldx: Could not find a Java Runtime Environment!
      Warning: failed to read path from javaldx
      /opt/libreoffice6.0/program/soffice.bin: error while loading shared libraries: libSM.so.6: cannot open shared object file: No such file or directory

      安装 yum -y install libSM

    • 这里整理了一份完整的离线依赖列表,需全部安装才可启动成功。
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      avahi-libs-0.6.31-20.el7.x86_64.rpm
      cairo-1.15.12-4.el7.x86_64.rpm
      cups-libs-1.6.3-51.el7.x86_64.rpm
      fontconfig-2.13.0-4.3.el7.x86_64.rpm
      libglvnd-1.0.1-0.8.git5baa1e5.el7.x86_64.rpm
      libglvnd-egl-1.0.1-0.8.git5baa1e5.el7.x86_64.rpm
      libglvnd-glx-1.0.1-0.8.git5baa1e5.el7.x86_64.rpm
      libICE-1.0.9-9.el7.x86_64.rpm
      libSM-1.2.2-2.el7.x86_64.rpm
      libX11-1.6.7-2.el7.x86_64.rpm
      libXau-1.0.8-2.1.el7.x86_64.rpm
      libxcb-1.13-1.el7.x86_64.rpm
      libXext-1.3.3-3.el7.x86_64.rpm
      libXinerama-1.1.3-2.1.el7.x86_64.rpm
      libXrender-0.9.10-1.el7.x86_64.rpm
      libpng15-1.5.30-7.el8.x86_64.rpm

  9. 成功安装 LiberOffice 之后, 修改 kkFileView 的配置文件

    1
    2
    cd /opt/kkFileView-4.0.0/config
    vi application.properties
    更改 application.properties 中的三项配置
    1
    2
    3
    server.servlet.context-path= /file-view
    office.home = /opt/libreoffice7.2
    base.url = http://项目ip:端口/file-view
    启动
    1
    2
    3
    4
    5
    cd /opt/kkFileView-4.0.0/bin
    #执行
    ./startup.sh
    #第一次执行完毕后建议再执行查看日志脚本
    ./showlog.sh
    正常启动,如下
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    [root@localhost bin]# ./showlog.sh
    _ _ ______ _ _ __ __ _
    | | | | | ____| (_) | | \ \ / / (_)
    | | __ | | __ | |__ _ | | ___ \ \ / / _ ___ __ __
    | |/ / | |/ / | __| | | | | / _ \ \ \/ / | | / _ \ \ \ /\ / /
    | < | < | | | | | | | __/ \ / | | | __/ \ V V /
    |_|\_\ |_|\_\ |_| |_| |_| \___| \/ |_| \___| \_/\_/

    => Spring Boot :: (v2.4.2) QQ1 :: 613025121
    => kkFileView :: (v4.0.0) QQ2 :: 484680571
    => github :: https://github.com/kekingcn/kkFileView
    => gitee :: https://gitee.com/kekingcn/file-online-preview

    2021-11-20 11:44:06.661 INFO 41979 --- [ main] org.eclipse.jetty.util.log : Logging initialized @4962ms to org.eclipse.jetty.util.log.Slf4jLog
    2021-11-20 11:44:07.092 INFO 41979 --- [ main] o.s.b.w.e.j.JettyServletWebServerFactory : Server initialized with port: 8012
    2021-11-20 11:44:07.101 INFO 41979 --- [ main] org.eclipse.jetty.server.Server : jetty-9.4.35.v20201120; built: 2020-11-20T21:17:03.964Z; git: bdc54f03a5e0a7e280fab27f55c3c75ee8da89fb; jvm 1.8.0_282-b08
    2021-11-20 11:44:07.191 INFO 41979 --- [ main] o.e.j.s.h.ContextHandler.application : Initializing Spring embedded WebApplicationContext
    2021-11-20 11:44:07.192 INFO 41979 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2806 ms
    2021-11-20 11:44:07.594 INFO 41979 --- [ main] org.eclipse.jetty.server.session : DefaultSessionIdManager workerName=node0
    2021-11-20 11:44:07.595 INFO 41979 --- [ main] org.eclipse.jetty.server.session : No SessionScavenger set, using defaults
    2021-11-20 11:44:07.599 INFO 41979 --- [ main] org.eclipse.jetty.server.session : node0 Scavenging every 600000ms
    2021-11-20 11:44:07.627 INFO 41979 --- [ main] o.e.jetty.server.handler.ContextHandler : Started o.s.b.w.e.j.JettyEmbeddedWebAppContext@74e52303{application,/file-view,[file:///tmp/jetty-docbase.8012.5985362194341337627/],AVAILABLE}
    2021-11-20 11:44:07.629 INFO 41979 --- [ main] org.eclipse.jetty.server.Server : Started @5935ms
    2021-11-20 11:44:07.647 INFO 41979 --- [ main] f.a.AutowiredAnnotationBeanPostProcessor : Autowired annotation is not supported on static methods: public static void cn.keking.config.ConfigConstants.setFileUploadDisable(java.lang.Boolean)
    2021-11-20 11:44:07.901 INFO 41979 --- [ main] cn.keking.service.FileConvertQueueTask : 队列处理文件转换任务启动完成
    2021-11-20 11:44:08.097 INFO 41979 --- [ main] o.a.j.office.ProcessPoolOfficeManager : ProcessManager implementation is LinuxProcessManager
    2021-11-20 11:44:08.162 INFO 41979 --- [ProcessThread-0] o.a.jodconverter.office.OfficeProcess : starting process with acceptString 'socket,host=127.0.0.1,port=2001,tcpNoDelay=1' and profileDir '/tmp/.jodconverter_socket_host-127.0.0.1_port-2001'
    2021-11-20 11:44:08.216 INFO 41979 --- [ProcessThread-0] o.a.jodconverter.office.OfficeProcess : started process; pid = 42030
    2021-11-20 11:44:09.085 WARN 41979 --- [ProcessThread-0] o.a.j.office.ManagedOfficeProcess : office process died with exit code 81; restarting it
    2021-11-20 11:44:09.139 INFO 41979 --- [ProcessThread-0] o.a.jodconverter.office.OfficeProcess : starting process with acceptString 'socket,host=127.0.0.1,port=2001,tcpNoDelay=1' and profileDir '/tmp/.jodconverter_socket_host-127.0.0.1_port-2001'
    2021-11-20 11:44:09.191 INFO 41979 --- [ProcessThread-0] o.a.jodconverter.office.OfficeProcess : started process; pid = 42039
    2021-11-20 11:44:09.851 INFO 41979 --- [ProcessThread-0] o.a.j.office.OfficeConnection : connected: 'socket,host=127.0.0.1,port=2001,tcpNoDelay=1'
    2021-11-20 11:44:09.992 INFO 41979 --- [ProcessThread-1] o.a.jodconverter.office.OfficeProcess : starting process with acceptString 'socket,host=127.0.0.1,port=2002,tcpNoDelay=1' and profileDir '/tmp/.jodconverter_socket_host-127.0.0.1_port-2002'
    2021-11-20 11:44:10.056 INFO 41979 --- [ProcessThread-1] o.a.jodconverter.office.OfficeProcess : started process; pid = 42092
    2021-11-20 11:44:10.817 WARN 41979 --- [ProcessThread-1] o.a.j.office.ManagedOfficeProcess : office process died with exit code 81; restarting it
    2021-11-20 11:44:10.866 INFO 41979 --- [ProcessThread-1] o.a.jodconverter.office.OfficeProcess : starting process with acceptString 'socket,host=127.0.0.1,port=2002,tcpNoDelay=1' and profileDir '/tmp/.jodconverter_socket_host-127.0.0.1_port-2002'
    2021-11-20 11:44:10.918 INFO 41979 --- [ProcessThread-1] o.a.jodconverter.office.OfficeProcess : started process; pid = 42102
    2021-11-20 11:44:11.452 INFO 41979 --- [ProcessThread-1] o.a.j.office.OfficeConnection : connected: 'socket,host=127.0.0.1,port=2002,tcpNoDelay=1'
    2021-11-20 11:44:12.179 INFO 41979 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
    2021-11-20 11:44:12.436 INFO 41979 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page template: index
    2021-11-20 11:44:12.470 INFO 41979 --- [ main] cn.keking.config.WebConfig : Add resource locations: /home/daqisuo/data/file-view/kkFileView-4.0.0/file/
    2021-11-20 11:44:13.111 INFO 41979 --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 'taskScheduler'
    2021-11-20 11:44:13.169 INFO 41979 --- [ main] o.e.j.s.h.ContextHandler.application : Initializing Spring DispatcherServlet 'dispatcherServlet'
    2021-11-20 11:44:13.169 INFO 41979 --- [ main] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
    2021-11-20 11:44:13.172 INFO 41979 --- [ main] o.s.web.servlet.DispatcherServlet : Completed initialization in 2 ms
    2021-11-20 11:44:13.220 INFO 41979 --- [ main] o.e.jetty.server.AbstractConnector : Started ServerConnector@6fe7aac8{HTTP/1.1, (http/1.1)}{0.0.0.0:8012}
    2021-11-20 11:44:13.223 INFO 41979 --- [ main] o.s.b.web.embedded.jetty.JettyWebServer : Jetty started on port(s) 8012 (http/1.1) with context path '/file-view'
    2021-11-20 11:44:13.275 INFO 41979 --- [ main] cn.keking.ServerMain : kkFileView 服务启动完成,耗时:10.603732337s,演示页请访问: http://127.0.0.1:8012
  10. 配置 nginx 请求转发

    1
    2
    3
    location /file-view {
    proxy_pass http://localhost:8012;
    }
  11. 查看访 http://项目 ip:端口/file-view/index 是否能进入 index 页面

  12. 在 Vue 中使用 KKFileViewer 3.0 以上版本时候,需要用 Base64 编码

    1
    npm install js-base64 --save

    在需要使用预览功能的组件中 import { Base64 } from 'js-base64';

    1
    2
    var url = 'http://127.0.0.1:8080/file/test.txt'; //要预览文件的访问地址
    window.open('http://127.0.0.1:8012/onlinePreview?url='+encodeURIComponent(Base64.encode(previewUrl)));
  13. 成功如下图


Centos 搭建 KKFileViewer 服务
https://www.ivan.fun/posts/null/
作者
IVAn
发布于
2022年6月19日
许可协议