我想从给定的PDF文件中提取所有文本(使用node和pdfjs),所以我安装了pdfjs-dist并try 使用以下代码:

import pdfjs from 'pdfjs-dist/build/pdf.js';
import pdfjsWorker from 'pdfjs-dist/build/pdf.worker.entry.js';

pdfjs.GlobalWorkerOptions.workerSrc = pdfjsWorker;

const pdf = await pdfjs.getDocument('testdoc.pdf');
const page = await pdf.getPage(1);

然而,这给了我

const page = await pdf.getPage(1);
                       ^

TypeError: pdf.getPage is not a function

为什么会出现这种情况,如何解决呢?

推荐答案

您需要在.getDocument()调用的末尾加上.promise才能公开Promises API.

const pdf = await pdfjs.getDocument('testdoc.pdf').promise;
const page = await pdf.getPage(1);

来源:https://mozilla.github.io/pdf.js/examples/

Node.js相关问答推荐

无法验证叶签名|无法验证第一个证书

使用Vite和ReactJS时,在哪里设置NODE_OPTIONS?

GraphQL MongoDB Mongoose填充字段未获取多个类别

在Android Studio中react 本机构建失败:未正确检测到Node.js版本

Puppeteer 在本地运行良好,但在 Heroku 中运行不佳

NodeJS:zlib.gzipSync 在不同平台上给出不同的明文输出

在快速路由中使用 axios 会在数据字段中返回特殊字符而不是 json

Zod 模式中的self 数组

使用 Forms API 进行批量更新时生成 itemId

在新创建的 Angular 工作区上运行 ng lint 时出错

Mongodb聚合传递一个匹配的数组和一个不匹配的数组

TypeError:changeChecked 不是函数

在 `DataFrame` 上使用用户定义的可链接函数抽象出 Polars 表达式

配额超出了每分钟的 Sheets API 写入请求数. node .js

使用服务帐户将 Firebase 应用程序部署到 Heroku(使用 dotenv 的环境变量)

Handlebars:访问已被拒绝解析来自的属性,因为它不是其父级的自己的属性

如何获取在 NodeJS 中执行的脚本的文件名?

Node.js + Express:应用程序不会开始监听端口 80

使用 gzip/deflate 压缩的简单 HTTP 请求

AWS Lambda 函数写入 S3