gotenberg+ chromiumly + pdf.js 进行office 文档转换以及预览处理

日常中office 预览是一个比较常见的问题,基于微软的office online 是一个选择,但是移动端效果不是很好
就有pdf 以及一些基于生成图片的方案也是不错的,以下是基于gotenberg+ chromiumly 的一个尝试

简单说明

gotenberg 是基于golang 开发的包装了Chromium 以及 LibreOffice 的基于api 的无状态pdf 转换服务,可以支持多种格式文件的
pdf 转换生成,比较灵活而且比较强大,包装下可以实现一个基本符合大部分场景的office 预览服务

环境准备

  • docker-compose 文件
 
version: "3"
services:
   convert:
      image: gotenberg/gotenberg:7
      ports:
        - "3000:3000"

代码集成使用

基于了chromiumly 包装的服务

  • demo.js
const { PDFEngine } = require("chromiumly");
const fs = require("fs");
const pdf = require('pdf-parse');
(async function () {
    const buffer = await PDFEngine.convert({
        files: ["app.docx"],
    });
    fs.writeFile("mydemo.pdf", buffer, "binary", function (err) {
        if (err) {
            console.log(err);
        } else {
            let dataBuffer = fs.readFileSync('mydemo.pdf');
            pdf(dataBuffer).then(function (data) {
                // number of pages
                console.log(data.numpages);
                // number of rendered pages
                console.log(data.numrender);
                // PDF info
                console.log(data.info);
                // PDF.js version
                // check https://mozilla.github.io/pdf.js/getting_started/
                console.log(data.version);
            })
        }
    });
})()

备注: 以上只是简单的pdf 生成以及pdf 解析,对于实际预览没有直接包含,但是我们可以基于openresty proxy + pdf.js 提供的预览可以方便的处理pdf预览
具体可以参考我以前写的文章

说明

gotenberg 同时支持基于webhook的异步处理也比较强大,比较适合较大文件,而且需要消息通知的,以上只是一个简单的集成说明,实际上对于转换的文件
我们可以直接存放到minio 中,基于s3 实现预览的权限了控制,同时chromiumly client 有点不好的是目前只支持文件,因为包装了文件流,不是很方便,可以
自己开发一个rest api 基于form-data 进行处理,完整运行demo 参考github

参考资料

https://www.cnblogs.com/rongfengliang/p/13693067.html
https://www.cnblogs.com/rongfengliang/p/13913100.html
https://gotenberg.dev/
https://github.com/gotenberg/gotenberg
https://www.npmjs.com/package/chromiumly
https://github.com/rongfengliang/gotenberg_chromiumly_learning
https://github.com/unoconv/unoconv

posted on   荣锋亮  阅读(248)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2021-04-12 cube.js playground 使用的一些问题
2020-04-12 easy-rules yaml 组合模式rule 配置
2020-04-12 easy-rules 组合rule说明
2020-04-12 easy-rules mvel yaml 格式规则配置&&试用二——rule 数据返回结果
2020-04-12 easy-rules mvel yaml 格式规则配置&&试用
2019-04-12 Introducing Outflux: a smart way out of InfluxDB
2019-04-12 使用outflux 导入influxdb 的数据到timescaledb

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示