kkFilePreview实现word/excel/pdf等文件在线预览

需求背景

项目中有文章附件需要实现在线预览,常见的文件有doc,docx, xls, xlsx, pdf, zip, rar等。以前的项目经验是通过easypoi实现word, excel生成,然后使用libreoffice转换为pdf,然后再使用pdfjs来实现在线预览。本次,通过调研发现有一个开源项目kkfileview能实现此类需求。再次感觉开源力量巨大,能够让我们的生活更美好,希望有能力的人和组织能够多多参与开源项目,捐款给开源项目,让他们活下来,活的更好。下面就来讲一下整个集成过程,希望对大家有所帮助。

安装libreoffice7.3

cd /opt/packages
 wget --no-check-certificate  https://download.documentfoundation.org/libreoffice/stable/7.3.1/rpm/x86_64/LibreOffice_7.3.1_Linux_x86-64_rpm.tar.gz
 
 tar zxvf  LibreOffice_7.3.1_Linux_x86-64_rpm.tar.gz
 
 sudo yum localinstall ./LibreOffice_7.3.1.3_Linux_x86-64_rpm/RPMS/*.rpm
 
 # 验证安装是否成功
 libreoffice7.3 --version
 ----> LibreOffice 7.3.1.3 a69ca51ded25f3eefd52d7bf9a5fad8c90b87951
 
 which  libreoffice7.3
 ---> /usr/bin/libreoffice7.3
 
 ll /usr/bin/libreoffice7.3
 ----> lrwxrwxrwx. 1 root root 35 3月  24 15:07 /usr/bin/libreoffice7.3 -> /opt/libreoffice7.3/program/soffice
# 可以发现libroffice7.3所在目录是/opt/libreoffice7.3
 
 

复制代码

下载kkFileView 4.0.0

cd /opt/apps
wget https://kkfileview.keking.cn/kkFileView-4.0.0.tar.gz
tar zxvf kkFileView-4.0.0.tar.gz

### 修改配置文件/opt/apps/kkFileView-4.0.0/config/application.properties
server.servlet.context-path= ${KK_CONTEXT_PATH:/kkfileview}
office.home = ${KK_OFFICE_HOME:/opt/libreoffice7.3/}

### 启动
./bin/start.sh

复制代码

检查程序启动是否成功

image.png

开通8012端口

firewall-cmd --zone=public --add-port=8012/tcp --permanent
firewall-cmd --reload
firewall-cmd --zone=public --query-port=8012/tcp
复制代码

访问页面: http://112.116.10.161:8012/kkfileview/index

kkfileview 首页

上传一个word文件,测试发现出现方块,原因是缺少中文字体 image.png

安装中文字体

yum -y install fontconfig  mkfontscale

 mkdir -p /usr/shared/fonts/chinese
 chmod -R 755 /usr/share/fonts/chinese
 cd /usr/shared/fonts/chinese
 # 上传字体文件后,刷新缓存
 fc-cache -fv
 # 检测linux上安装的中文字体
 fc-list :lang=zh-cn
 
 # 检查能否解析中文字体名
for family in 微软雅黑 宋体 黑体 仿宋 仿宋_GB2312 楷体 楷体_GB2312 隶书 幼圆; do echo -n "$family: "; fc-match "$family"; done
复制代码

发现fc-match返回的字体解析不对,因此需要配置字体解析规则

配置字体解析规则:在配置节之间增加以下match标签,增加中文字体解析规则。

vim /etc/fonts/fonts.conf

  <!-- 为微软雅黑 宋体 黑体 仿宋 仿宋_GB2312 楷体 楷体_GB2312 隶书 幼圆;配置别名 -->
  <match>
    <test name="family"><string>微软雅黑</string></test>
    <edit name="family" mode="assign" binding="strong">
      <string>Microsoft YaHei</string>
    </edit>
  </match>
  <match>
    <test name="family"><string>宋体</string></test>
    <edit name="family" mode="assign" binding="strong">
      <string>SimSun</string>
    </edit>
  </match>
  <match>
    <test name="family"><string>黑体</string></test>
    <edit name="family" mode="assign" binding="strong">
      <string>SimHei</string>
    </edit>
  </match>
  <match>
    <test name="family"><string>仿宋</string></test>
    <edit name="family" mode="assign" binding="strong">
      <string>FangSong</string>
    </edit>
  </match>
   <match>
    <test name="family"><string>仿宋_GB2312</string></test>
    <edit name="family" mode="assign" binding="strong">
      <string>FangSong_GB2312</string>
    </edit>
  </match>
   <match>
    <test name="family"><string>楷体</string></test>
    <edit name="family" mode="assign" binding="strong">
      <string>KaiTi</string>
    </edit>
  </match>
  <match>
    <test name="family"><string>楷体_GB2312</string></test>
    <edit name="family" mode="assign" binding="strong">
      <string>KaiTi_GB2312</string>
    </edit>
  </match>
  <match>
    <test name="family"><string>隶书</string></test>
    <edit name="family" mode="assign" binding="strong">
      <string>LiSu</string>
    </edit>
  </match>
  <match>
    <test name="family"><string>幼圆</string></test>
    <edit name="family" mode="assign" binding="strong">
      <string>YouYuan</string>
    </edit>
  </match>
复制代码

配置后,再次检查解析情况

[root@webadmin01 chinese]# for family in 微软雅黑 宋体 黑体 仿宋 仿宋_GB2312 楷体 楷体_GB2312 隶书 幼圆; do   echo -n "$family: ";   fc-match "$family"; done
微软雅黑: MSYH.TTC: "Microsoft YaHei" "Normal"
宋体: SIMSUN.TTC: "SimSun" "Regular"
黑体: SIMHEI.TTF: "SimHei" "Normal"
仿宋: SIMFANG.TTF: "FangSong" "Regular"
仿宋_GB2312: 仿宋_GB2312.ttf: "FangSong_GB2312" "Regular"
楷体: SIMKAI.TTF: "KaiTi" "Regular"
楷体_GB2312: 楷体_GB2312.ttf: "KaiTi_GB2312" "Regular"
隶书: 隶书.ttf: "LiSu" "Regular"
幼圆: youyuan.TTF: "YouYuan" "Regular"
复制代码

重新上传一个新的文件进行测试

重新上传文件测试,字体正常了 重新上传文件测试,字体正常了

预览效果如下 image.png

遇到的问题

运行libreoffice7.3 --version, 提示error while loading shared libraries: libcups.so.2, 解决方案为yum install cups-libs

参考资料

猜你喜欢

转载自juejin.im/post/7086313504822525983
今日推荐