kkFilePreview realizes online preview of word/excel/pdf and other files

demand background

There are article attachments in the project that need to be previewed online. Common files include doc, docx, xls, xlsx, pdf, zip, rar, etc. The previous project experience is to achieve word and excel generation through easypoi, then use libreoffice to convert to pdf, and then use pdfjs to achieve online preview. This time, through research, it was found that there is an open source project kkfileview that can meet such requirements. Once again, I feel that open source has great power and can make our lives better. I hope that capable people and organizations can participate in open source projects and donate to open source projects, so that they can survive and live better. Let's talk about the whole integration process, I hope to help everyone.

Install 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
 
 

复制代码

Download 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

复制代码

Check if the program starts successfully

image.png

Open port 8012

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

Visit the page: http://112.116.10.161:8012/kkfileview/index

kkfileview 首页

Upload a word file, the test found that there are squares, the reason is the lack of Chinese fontsimage.png

Install Chinese fonts

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
复制代码

It is found that the font parsing returned by fc-match is incorrect, so you need to configure font parsing rules

Configure font parsing rules: Add the following match tags between configuration sections to add Chinese font parsing rules.

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>
复制代码

After configuration, check the parsing again

[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"
复制代码

Re-upload a new file to test

Re-upload the file to test, the font is normal重新上传文件测试,字体正常了

The preview effect is as followsimage.png

problems encountered

run libreoffice7.3 --version, prompt error while loading shared libraries: libcups.so.2, solution foryum install cups-libs

References

Guess you like

Origin juejin.im/post/7086313504822525983