我正在尝试将 PDF.js (由 Mozilla 提供)引用到我的 React 项目中。但是,它抛出“意外标识符”错误。
我已将 PDF.js 放在公共文件夹中并在我的 index.html 中引用它。
文件结构:
public
- index.html
- pdftojs
- parsejs.js // < parseFile method in this file will be called
- pdf-parse.js
- misc..
src
- pdftotext
- parsepdf.js // < page to parse PDF
pdf-parse.js
var PDFJS = null
function render_page(pageData) { ... } // Untouched
async function PDF(...) { ... } // Untouched
exports.pdf = PDF; // Changed this line
来自原始库的 parsejs.js:
8 import pdf from 'pdf-parse.js';
9 const pdfjsLib = require('pdfjs-dist'); // 'require' is undefined too so I don't know what is the correct way
10
11 function parseFile(file) {
...
45 }
此文件抛出 Unexpected identifier on Line 8
解析 PDF 页面 (parsepdf.js)
process(file) {
parseFile(file); // calling method in parsejs.js
...
}
这给出了 'parseFile' is not defined
原文由 xtotheawn 发布,翻译遵循 CC BY-SA 4.0 许可协议
我今天花了太多时间来拼凑这个问题的其他答案的片段。所以这是一个完整的答案。
首先安装 pdfjs-dist:
下面是如何在实际的查看器组件中使用它: