【问题标题】:TypeError: Cannot read property 'GlobalWorkerOptions' of undefined in PDFJS.GlobalWorkerOptions.workerSrc when using React jsTypeError:使用 React js 时无法读取 PDFJS.GlobalWorkerOptions.workerSrc 中未定义的属性“GlobalWorkerOptions”
【发布时间】:2020-07-25 18:54:22
【问题描述】:

我在 PDFJS.GlobalWorkerOptions.workerSrc 中收到“TypeError:无法读取未定义的属性 'GlobalWorkerOptions'”。我的代码在这里,

import { pdfjs } from 'react-pdf';
pdfjs.GlobalWorkerOptions.workerSrc = `https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.0.87/pdf.js`;

我用它从给定的 pdf 中提取文本。我用

npm i react-pdf

npm 为此。帮帮我..

【问题讨论】:

    标签: reactjs pdf npm pdfjs react-pdf


    【解决方案1】:

    试试这个代码。用您的 PDF 文档更改 somefile.pdf。

    import React, { Component } from 'react';
    import { Document, Page } from 'react-pdf';
    
    class MyApp extends Component {
      state = {
        numPages: null,
        pageNumber: 1,
      }
    
      onDocumentLoadSuccess = ({ numPages }) => {
        this.setState({ numPages });
      }
    
      render() {
        const { pageNumber, numPages } = this.state;
    
        return (
          <div>
            <Document
              file="somefile.pdf"
              onLoadSuccess={this.onDocumentLoadSuccess}
            >
              <Page pageNumber={pageNumber} />
            </Document>
            <p>Page {pageNumber} of {numPages}</p>
          </div>
        );
      }
    }
    
    

    【讨论】:

    • 这不是我所期望的答案。我必须能够知道,在 react 中使用 pdfjs.GlobalWorkerOptions.workerSrc 的正确方法是什么。
    • 答案直接来自文档。 github.com/wojtekmaj/react-pdf
    猜你喜欢
    • 2020-05-03
    • 1970-01-01
    • 2022-11-17
    • 2020-12-24
    • 2022-01-01
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 2021-07-25
    相关资源
    最近更新 更多