使用 Angular 中的 @types/pdfjs-dist 获取对 pdfjsLib 的引用

问题描述

我正在尝试在预编译的 Angular 9 项目中使用 pdf.js 库。

我使用 npm i --save pdfjs-distnpm i --save @types/pdfjs-dist

安装

然后从我在网上看到的,我应该能够使用 import * as pdfjsLib from 'pdfjs-dist'; 在我的组件中访问它。问题是我在编译器中遇到了这个错误

error message

我尝试了很多其他方法获取引用,但它们要么无法获取引用,要么抛出相同的错误消息。

我也尝试过这里的建议:Importing namespace and interface with methods in TypeScript (TSX) in pdfjs-dist (PDFJS) 但是,我收到错误消息,告诉我 PDFJsstatic 不是导出成员。

error 2

任何帮助都将不胜感激!

我的目标是能够使用 getDocument() 函数

解决方法

试试这个,这是目前最新的版本。

npm i @libusoftcicom/pdfjs-dist

import * as pdfjsLib from '@libusoftcicom/pdfjs-dist';
import { PDFPageProxy,PDFJSStatic } from '@libusoftcicom/pdfjs-dist/types/display/api';

把这个放在你的加载函数中。

const pdfjsWorker = await import('@libusoftcicom/pdfjs-dist/build/pdf.worker.entry');
pdfjsLib.GlobalWorkerOptions.workerSrc = pdfjsWorker;