基于WPSOffice+Pywpsrpc构建Docker镜像,实现文档转换和在线预览服务

发布时间 2023-12-07 16:13:00作者: codest

背景

产品功能需要实现标准文档的在线预览功能,由于DOC文档没办法直接通过浏览器打开预览,需要提前转换为PDF文档或者HTML页面。

经过测试发现DOC转为HTML页面后文件提交较大,而且生成的静态资源文件较多,需要额外搭建Web容器存放,所以还是考虑转换为PDF格式文档。

 

选型

需求明确后就要考虑如何实现了,这里对比了开源文档转换工具例如:kkFileViewdocuments4jpoiopenofficeasposeSpire.Office等。

选型主要考虑以下几个方面:

1、转换为PDF文档后是否会失真,也就是和原DOC文档的格式还原度;

2、是否支持大文档,有些标准文档超过40M用MSOffice打开很卡;

3、工具收费情况。

简单说下结论:

1、上面几个工具全部验证了一遍,后发现都不太合适,原因有二,免费工具会失真或者限制了文档的大小,收费的工具可以用但是贵;

2、最后选择用WPS进行文档格式转换,原因很简单,首先WPS有个人版是可以免费使用的,其实WPS转换PDF文档的还原度相当高,另外客户方已经采购了WPS的企业版本,如果后续文档转换失真也有个说法;

3、解决方案:基于CentOS安装WPS,通过Python的Pywpsrpc组件调用WPS的文档转换功能,对外提供文档转换服务。

 

镜像构建

服务镜像已构建完成,如有需要可以联系笔者V:hsky23557544

基础镜像

由于涉及安装的组件太多,编写Dockerfile的话太麻烦也不便于试错,所以决定先拉取一个基础的CentOS镜像,把软件服务都安装好,最后通过docker commit将容器打成镜像。

1、基础镜像docker pull centos:7.6.1810

2、启动容器docker run --name centos --network host -dit --privileged=true centos:7.6.1810 /usr/sbin/init

3、更新到国内的YUM源,加速后面的软件安装,网上教程很多不增加篇幅

安装CentOS桌面

pywpsrpc需要依赖系统桌面调用WPS的API,如需headless运行可以参考:https://github.com/timxx/pywpsrpc/wiki/Run-on-Server

yum -y install epel-release
yum groupinstall "GNOME Desktop"
systemctl isolate graphical.target
systemctl set-default graphical.target
yum install xrdp -y
yum install tigervnc-server -y
# 设置vnc访问密码
vncpasswd root

systemctl start xrdp
systemctl enable xrdp

WPSOffice安装

1、官网Linux版本安装包下载:https://linux.wps.cn/

2、安装yum localinstall wps-office-11.1.0.11711-1.x86_64.rpm

安装WPSOffice依赖的字体

 推荐使用开源项目安装:https://github.com/dv-anomaly/ttf-wps-fonts

git clone https://github.com/iamdh4/ttf-wps-fonts.git

cd ttf-wps-fonts

sudo sh install.sh

rm -rf /tmp/ttf-wps-fonts

安装Python3.6

开始用Python3.9版本安装pywpsrpc的时候直接报错了,查阅了一些pywpsrpc的issues发现需要使用Python3.6.x版本,另外pywpsrpc没有arm版本需要自行编译whl安装。

1、下载安装包:https://registry.npmmirror.com/-/binary/python/3.6.15/Python-3.6.15.tgz

2、安装依赖:yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl--devel

3、编译安装

# 解压压缩包
tar -zxvf Python-3.6.15.tgz  

# 进入文件夹
cd Python-3.6.15

# 配置安装位置
./configure prefix=/usr/local/python3

# 安装
make && make install

# 建立软连接
ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3.6 /usr/bin/pip3

4、安装pywpsrpc:pip3 install pywpsrpc -i https://pypi.tuna.tsinghua.edu.cn/simple 

5、安装flask:pip3 install flask -i https://pypi.tuna.tsinghua.edu.cn/simple

如果提示使用pip安装依赖包时提示SSL证书验证的问题,需要重新编译安装openssl,操作如下:

# 下载源码包
wget http://www.openssl.org/source/openssl-1.1.1l.tar.gz

# 编译安装
mkdir /usr/local/ssl
./config --prefix=/usr/local/ssl --openssldir=/usr/local/ssl no-zlib
make && make install
echo "/usr/local/ssl/lib" >> /etc/ld.so.conf

测试是否安装好,ldconfig -v 显示出相关内容,并查看/usr/local/ssl目录下确实有安装好的文件

# 回到Python源吗目录下
make clean

# 配置安装位置
./configure prefix=/usr/local/python3

修改Modules/Setup文件

make && make install

ln -s /usr/local/python3/bin/python3 /usr/local/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/local/bin/pip3

 安装QT5

1、下载安装包:https://download.qt.io/archive/qt/5.12/5.12.12/

2、安装依赖包:yum -y install mesa-libGL-devel mesa-libGLU-devel freeglut-devel gdb

3、安装包授权:chmod +x qt-opensource-linux-x64-5.12.12.run

4、使用mstsc远程登录CentOS桌面,执行./qt-opensource-linux-x64-5.12.12.run通过图形化界面安装,安装时会提示注册按提示信息操作即可

文档转换服务

import traceback, uuid, os
from flask import Flask, request, send_from_directory
from pywpsrpc.rpcwpsapi import (createWpsRpcInstance, wpsapi)
from pywpsrpc.common import (S_OK, QtApp)


app = Flask(__name__)
# 设置文件上传保存路径
app.config['UPLOAD_FOLDER'] = '/tmp'
# MAX_CONTENT_LENGTH设置上传文件的大小,单位字节
app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024 * 1024


formats = {
    "doc": wpsapi.wdFormatDocument,
    "docx": wpsapi.wdFormatXMLDocument,
    "rtf": wpsapi.wdFormatRTF,
    "html": wpsapi.wdFormatHTML,
    "pdf": wpsapi.wdFormatPDF,
    "xml": wpsapi.wdFormatXML,
}


class ConvertException(Exception):

    def __init__(self, text, hr):
        self.text = text
        self.hr = hr

    def __str__(self):
        return """Convert failed:
Details: {}
ErrCode: {}
""".format(self.text, hex(self.hr & 0xFFFFFFFF))


def convert_to(path, format, abort_on_fails=False):
    hr, rpc = createWpsRpcInstance()
    if hr != S_OK:
        raise ConvertException("Can't create the rpc instance", hr)

    hr, app = rpc.getWpsApplication()
    if hr != S_OK:
        raise ConvertException("Can't get the application", hr)

    try:
        # we don't need the gui
        app.Visible = False

        docs = app.Documents

        def _handle_result(hr):
            if abort_on_fails and hr != S_OK:
                raise ConvertException("convert_file failed", hr)

        hr = convert_file(path, docs, format)
        _handle_result(hr)
    except Exception as e:
        print(traceback.format_exc())

    app.Quit(wpsapi.wdDoNotSaveChanges)


def convert_file(file, docs, format):
    hr, doc = docs.Open(file, ReadOnly=True)
    if hr != S_OK:
        return hr

    out_dir = os.path.dirname(os.path.realpath(file)) + "/out"
    os.makedirs(out_dir, exist_ok=True)

    # you have to handle if the new_file already exists
    new_file = out_dir + "/" + os.path.splitext(os.path.basename(file))[0] + "." + format
    ret = doc.SaveAs2(new_file, FileFormat=formats[format])

    # always close the doc
    doc.Close(wpsapi.wdDoNotSaveChanges)

    return ret


@app.route("/convert", methods=["POST"])
def do_convert():
    try:

        # 转换格式参数
        convert_format = request.form['format']

        # 获取文件参数
        file = request.files['file']
        # 文件后缀
        file_ext = file.filename.rsplit('.')[-1]
        file_name_uuid = str(uuid.uuid4())
        # UUID文件名
        file_name = file_name_uuid + "." + file_ext
        # 保存文件
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], file_name))

        # 文件转换
        print("开始转换文档[%s]" % file_name)
        convert_to(os.path.join(app.config['UPLOAD_FOLDER'], file_name), convert_format, True)
        print("文档转换完成[%s]" % file_name)

        # 返回转换后的文件
        convert_file_name = file_name_uuid + "." + convert_format
        return send_from_directory(app.config['UPLOAD_FOLDER'] + '/out', convert_file_name, as_attachment=True)

    except Exception as e:
        print('文档转换时发生错误')
        print(traceback.format_exc())
        return {"success": False}


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=9000)

使用mstsc远程登录CentOS桌面,执行启动脚本:

#!/bin/bash
set -e

export PATH=$PATH:/opt/kingsoft/wps-office/office6
export LD_LIBRARY_PATH=/opt/Qt5.12.12/5.12.12/gcc_64/lib
python3 /root/converter.py

服务验证

1、远程桌面登录CentOS桌面后能够正常打开WPS软件,没有提示字体依赖错误;

2、文档转换接口测试:

curl --location --request POST 'http://10.32.x.x:9000/convert' \
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)' \
--header 'Accept: */*' \
--header 'Host: 10.32.x.x:9000' \
--header 'Content-Type: multipart/form-data; boundary=--------------------------851319197095122366611715' \
--form 'format="pdf"' \
--form 'file=@"需要转换的DOC文档路径"'