繁体   English   中英

如何在 Typescript 中使用 react-pdf 库

[英]How to use react-pdf library with Typescript

我正在尝试使用一个 npm 库来帮助我呈现 pdf 文档。 我找到了 react-pdf 但它还没有 TypeScript 的类型,所以我正在使用它,如下所示:

let Document = require('react-pdf');
let Page = require('react-pdf');

class PdfViewer extends React.Component<PdfViewer.Props, PdfViewer.State> {

  constructor(props: PdfViewer.Props) {
    super(props);
    this.state = {
      numPages: null,
      pageNumber: 1,
    };

  }
  onDocumentLoadSuccess = ( numPages: number ) => {
    this.setState({ numPages });

  }

  componentWillReceiveProps(nextProps: PdfViewer.Props) {
    // this.setState(CsvViewer.parse(nextProps.data));
  }

  render() {
      const {url} = this.props;
      const { pageNumber, numPages } = this.state;

      const buffer = data as ArrayBuffer;

      return ( 
      <div>
        <Document
          file={url}
          onLoadSuccess={this.onDocumentLoadSuccess}
        >
          <Page pageNumber={pageNumber} />
        </Document>
        <p>Page {pageNumber} of {numPages}</p>
      </div>
      );

  }
}

但它抛出了这个错误:

元素类型无效:应为字符串(用于内置组件)或类/函数(用于复合组件)但得到:对象。

我在 GitHub 上找到了这个,但对我帮助不大。 我去了图书馆的文档,我在文件参数中传递了一个对象,就像他们展示的那样,但同样的错误。 我认为是另外一回事。 我去了react-pdf库的源代码,我认为这是他们用来从参数中获取文件的函数:

findDocumentSource = async () => {
    const { file } = this.props;

    if (!file) {
      return null;
    }

    // File is a string
    if (typeof file === 'string') {
      if (isDataURI(file)) {
        const fileUint8Array = dataURItoUint8Array(file);
        return { data: fileUint8Array };
      }

      displayCORSWarning();
      return { url: file };
    }

    // File is PDFDataRangeTransport
    if (file instanceof PDFDataRangeTransport) {
      return { range: file };
    }

    // File is an ArrayBuffer
    if (isArrayBuffer(file)) {
      return { data: file };
    }

    /**
     * The cases below are browser-only.
     * If you're running on a non-browser environment, these cases will be of no use.
     */
    if (isBrowser) {
      // File is a Blob
      if (isBlob(file) || isFile(file)) {
        return { data: await loadFromFile(file) };
      }
    }

    // At this point, file must be an object
    if (typeof file !== 'object') {
      throw new Error('Invalid parameter in file, need either Uint8Array, string or a parameter object');
    }

    if (!file.url && !file.data && !file.range) {
      throw new Error('Invalid parameter object: need either .data, .range or .url');
    }

    // File .url is a string
    if (typeof file.url === 'string') {
      if (isDataURI(file.url)) {
        const { url, ...otherParams } = file;
        const fileUint8Array = dataURItoUint8Array(url);
        return { data: fileUint8Array, ...otherParams };
      }

      displayCORSWarning();
    }

    return file;
  };

但我怀疑错误就在这里,但我不知道可能是什么。 我使用的react版本是 16.8.2 和 4.0.2 for react-pdf

这是不正确的:

let Document = require('react-pdf');
let Page = require('react-pdf');

执行此操作时,您正在导入整个模块。 这意味着DocumentPage都包含react-pdf的所有模块。 相反,您想要做的是导入 ReactPDF 并引用这些模块:

const ReactPDF = require('react-pdf');

<ReactPDF.Document />
<ReactPDF.Page />

或者你可以使用解构:

import { Document, Page } from 'react-pdf';

//or

const { Document, Page } = require('react-pdf'); 

如果 TypeScript 抱怨缺少声明,只需将@types文件夹添加到您的项目中,在其中创建一个名为react-pdf的子文件夹,并在该文件夹中创建一个名为index.d.ts的文件,其中包含一行:

declare module "react-pdf"
  • 首先添加类型
  • 如果您使用的是创建反应应用程序,您应该添加:
import { Document, Page, pdfjs } from "react-pdf/dist/esm/entry.webpack";
pdfjs.GlobalWorkerOptions.workerSrc =`//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`;

暂无
暂无

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

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