基于Docker和kkFileView实现office在线预览

1 设置kkFileView

kkFileView开源的office在线预览项目,真的很不错!官网地址如下:

https://kkfileview.keking.cn/zh-cn/index.html

# 下载镜像
sudo docker pull keking/kkfileview:v4.0.0

# 启动kkFileView
sudo docker run -itd --name kkfileview -p 8012:8012 keking/kkfileview:v4.0.0

2 设置Nginx文件代理

设置容器

# 创建nginx容器
sudo docker run -itd --name kkfileview_nginx -v /home/yyp/file:/home/file -p 80:80 nginx:1.20.0

# 将容器中default.conf复制到本地
sudo docker cp kkfileview_nginx:/etc/nginx/conf.d/default.conf .

# 将default.conf复制到容器中
sudo docker cp default.conf kkfileview_nginx:/etc/nginx/conf.d

nginx配置文件

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    
    location /file/ {
        alias   /home/file/;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

3 设置HTML文件

!!!注意!!!

如果使用容器文件中的url = "http://172.17.0.3/file/my_data.docx";的ip地址不能是127.0.0.1。ip地址必须是一个与kkFileView容器在同一网段,并且能够下载文件的ip地址。

我的kkfileview容器的ip地址是:172.17.0.2;nginx容器的ip地址是:172.17.0.3。

注意:如果是接口调用,一定要在地址后面添加“&fullfilename=你的文件名.后缀名称”,否则会报错。

<!DOCTYPE html>
<html>
        <head>
                <title>测试</title>
        </head>
        <script src="https://cdn.jsdelivr.net/npm/js-base64@3.6.0/base64.min.js"></script>
        <script type="text/javascript">
                // npm在线安装
                // npm install --save js-base64@3.6.0
                function jump(){
                        //要预览文件的访问地址
                        var url = "http://172.17.0.3/file/my_data.docx";
                        window.open('http://127.0.0.1:8012/onlinePreview?url='+encodeURIComponent(Base64.encode(url)));
                }
        </script>

        <body>
                <input type="button" value="查看文件" onclick="jump()" />
        </body>
</html>

4 截图

### 解决kkFileView文件在线预览失败的问题 当遇到kkFileView文件在线预览失败的情况时,可能的原因及对应的解决方案如下: #### 1. LibreOffice 版本问题 如果使用的Apache OpenOffice已经停止更新,而最新版本为4.1+,则建议更换至LibreOffice以获得更好的兼容性持续的支持。官方Docker镜像集成的是OpenOffice,在这种情况下应考虑手动安装并配置最新的LibreOffice版本来替代之[^2]。 #### 2. 配置路径错误 对于CentOS7环境下部署的kkFileView服务而言,`application.properties`文件内的`office.home`属性需指向实际安装有LibreOffice的位置。若此设置不正确,则可能导致无法正常调用转换命令从而引发预览失败。因此应当确认该参数已被修改成正确的LibreOffice安装目录[^4]。 ```properties # application.properties 中 office.home 的示例配置 office.home=/usr/lib/libreoffice/program/ ``` #### 3. 转换指令执行异常 为了验证是否能够成功地通过Soffice组件完成Word到JPG格式之间的转换操作,可以在命令行下尝试运行类似的测试命令。这有助于排查是否存在权限不足或其他环境因素干扰了正常的处理流程[^3]。 ```bash soffice --headless --convert-to jpg ./example.docx ``` 上述措施可以帮助定位并修复kkFileView文件在线预览过程中可能出现的各种问题。确保所使用的软件均为稳定版,并保持良好的维护习惯,定期检查日志记录以便及时发现潜在隐患。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值