【前端】Vue中实现pdf逐页转图片,图片再逐张提取文字

给定场景:后端无法实现pdf转文字,由前端实现"pdf先转图片再转文字"。

方法:

假设我们在< template>中有一个元素存放我们处理过的canvas集合

bash 复制代码
<div id="canvasIDpdfs" />

我们给定一个按钮,编写click函数,通过点击按钮触发pdf先转图片再转文字的功能

bash 复制代码
this.pdfdata()

在< script>里编写函数

javascript 复制代码
pdfdata() {
    const url = pdf路径;

    if (url) {
        const that = this;
        const canvasID = document.getElementById('canvasIDpdfs');
        // 引入插件库PDFJS
        PDFJS.getDocument({
            url,
            httpHeaders: {
                token: that.$cookie.get('token'),
            },
        }).then((pdfDoc_) => {
            if (pdfDoc_) {
                // 清空canvas
                canvasID.innerHTML = '';
                // 清空富文本
                this.editor.txt.clear();
                // 赋值
                that.pdfDoc = pdfDoc_;
                // 别名
                const pdfDoc = pdfDoc_;
                // 逐页
                for (let i = 1; i <= pdfDoc.numPages; i++) {
                    // 传入页码与画布
                    that.renderPage(i, canvasID);
                }
            }
        }).catch((e) => {
            console.log(`获取pdf文件失败,${e} `);
        });
    }
},

renderPage(num, canvasID) {
    // 使用PDFJS能力
    this.pdfDoc.getPage(num).then((page) => {
        // 所有画布容器
        const canvasList = canvasID;
        // 新画布
        const canvas = document.createElement('canvas');
        // 存新画布
        canvasList.appendChild(canvas);
        // 绘图
        const ctx = canvas.getContext('2d');
        // 缩放
        const viewport = page.getViewport(1.5);
        // 尺寸
        canvas.height = viewport.height;
        canvas.width = viewport.width;
        // 使用PDFJS能力
        const renderContext = {
            canvasContext: ctx,
            viewport,
        };
        // 使用PDFJS能力
        const renderTask = page.render(renderContext);
        renderTask.promise.then(() => {
            // 新画布已绘制完成
            const latestCanvas =
                document.getElementById('canvasIDpdfs').getElementsByTagName('canvas')[num - 1].toDataURL("image/jpeg");
            // 对其进行图片转文字
            axios({
                url: 图片转文字的服务API,
                method: 'post',
                headers: {
                    'Content-Type': 'application/json',
                    token: Cookies.get('token'),
                },
                data: {
                    image: latestCanvas.split('data:image/jpeg;base64,')[1],
                },
            }).then((res) => {
                // 得到文字
                if (res.result
                    && res.result > 0
                ) {
                    res.result.forEach((m) => {
                        // 自定义样式
                        const style = '';
                        // 文字添加到富文本
                        this.editor.txt.append(`<p style="${style}">${m.words}</p>`);
                    });
                }
            });
        });
    });
},
相关推荐
Hello--_--World2 分钟前
Vue2的 双端 diff算法 与 Vue3 的 快速diff 算法
前端·vue.js·算法
gongzemin10 分钟前
怎么在VS Code 调试vue2 源码
前端·vue.js
烟话618 分钟前
Vue3响应式原理【通俗理解】
前端·javascript·vue.js
AscendKing25 分钟前
免费、易用、覆盖全平台的网页转 PDF 工具
pdf·html·网页保存·网页保存为pdf·保存网页位pdf
浩星27 分钟前
electron系列5:深入理解Electron打包
前端·javascript·electron
患得患失94932 分钟前
【前端WebSocket】心跳功能,心跳重置策略、双向确认(Ping-Pong) 以及 指数退避算法(Exponential Backoff)
前端·websocket·算法
英俊潇洒美少年34 分钟前
React 实现 AI 流式打字机对话:SSE 分包粘包处理 + 并发优化
前端·javascript·react.js
chQHk57BN37 分钟前
前端测试入门:Jest、Cypress等测试框架使用教程
前端
遇见你...1 小时前
前端技术知识点
前端
AC赳赳老秦1 小时前
OpenClaw image-processing技能实操:批量抠图、图片尺寸调整,适配办公需求
开发语言·前端·人工智能·python·深度学习·机器学习·openclaw