简体   繁体   English

在带有打字稿的 create-react-app 中使用 mozilla pdf.js

[英]Use mozilla pdf.js in create-react-app with typescript

I try to find some documentation for how to include the mozilla pdf.js into a create-react-app with typescript configured.我试图找到一些关于如何将 mozilla pdf.js 包含到配置了打字稿的 create-react-app 的文档。 Unfortunately I only find a lot of people who are asking the same quetion but no working solutions.不幸的是,我只发现很多人都在问同样的问题,但没有可行的解决方案。
At the moment I use the following approach and it seems to work.目前我使用以下方法,它似乎有效。 But I'm not sure if that is a good way of doing it.但我不确定这是否是一个好方法。

I installed the dependencies with npm as shown below.我使用 npm 安装了依赖项,如下所示。

npm install --save @types/pdfjs-dist
npm install --save pdfjs-dist

And importing / configure it this way.并以这种方式导入/配置它。

// Import pdf.js    
import * as pdfjsLib from 'pdfjs-dist';
pdfjsLib.GlobalWorkerOptions.workerSrc = process.env.PUBLIC_URL + '/assets/js/pdfjs/pdf.worker.min.js';

// Load pdf
pdfjsLib.getDocument('pdf_url').promise.then((promise: pdfjsLib.PDFDocumentProxy) => {
    console.log(promise.fingerprint);
});

Therefore I have to make sure that the files pdf.worker.js.map and pdf.worker.min.js were present inside the folder public/assets/js/pdfjs .因此,我必须确保文件pdf.worker.js.mappdf.worker.min.js存在于文件夹public/assets/js/pdfjs Is there a more elegant way of solving the import of pdf.js?有没有更优雅的方式解决pdf.js的导入问题?

Attached the react and pdfjs-dist version:附上react和pdfjs-dist版本:

"pdfjs-dist": "^2.5.207",
"react": "^16.13.1",
"react-scripts": "3.4.3",
"typescript": "^3.7.5"

I faced the similar issue.我遇到了类似的问题。 There are some solutions provided in github issue eg Here and Here but nothing worked for me.在 github 问题中提供了一些解决方案,例如这里这里,但对我来说没有任何效果。 what worked for me in create-react-app with javascript is using es5 version of the build files from pdfjs-dist which are available at pdfjs-dist/es5/build/在使用 javascript 的 create-react-app 中对我有用的是使用来自 pdfjs-dist 的 es5 版本的构建文件,这些文件可在 pdfjs-dist/es5/build/

import * as pdfjs from 'pdfjs-dist/es5/build/pdf';
import pdfjsWorker from 'pdfjs-dist/es5/build/pdf.worker.entry';

pdfjs.GlobalWorkerOptions.workerSrc = pdfjsWorker;

I'm not sure about typescript but should work the same.我不确定打字稿,但应该一样。

versions:版本:

"pdfjs-dist": "^2.7.570",
"react": "^17.0.1",

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM