React-pdf: 未使用 Uint8Array 数据触发 onLoadSuccess

创建于 2018-02-21  ·  4评论  ·  资料来源: wojtekmaj/react-pdf

我在特定环境(电子)中使用 react-pdf,所以我无法通过 http/https 加载文件,而是从 fs 加载文件。
好像没有触发 onLoadSuccess ,正常吗?
我需要在呈现第一页之前获取页数。

question

所有4条评论

绝对不正常! 否则文件是否完美加载? 你能在这里分享你的 React-PDF 实现吗?

是的,文件已完美加载。

尝试了稳定版本和 3 alpha。

我错过了什么吗?

export default class PDFPlayer extends PureComponent {

  state = {
    fileLoaded: 0,
    numPages: null,
    pageNumber: 1,
  }

  componentWillMount() {
    const { filePath } = this.props;
    if (fs.existsSync(filePath)) {
      fs.readFile(filePath, (err, file) => {
        if (err) { console.log(err); }
        this.file = new Uint8Array(file);
        this.setState({ fileLoaded: 1 });
      });
    }
  }

  onLoadSuccess = (pdf) => {
    console.log('onDocumentLoad', pdf);
    this.setState({ numPages: pdf.numPages });
  }

  onRenderSuccess = (page) => {
    console.log(page.originalHeight);
  }

  render() {
    const { fileLoaded } = this.state;
    if (!fileLoaded) return null;
    return (
      <div style={{ ...styles.container, height: document.body.offsetHeight }}>
        <Document
          ref={(doc) => { this.document = doc; }}
          file={{ data: this.file }}
          onLoadSuccess={this.onLoadSuccess}
        >
          <Page onRenderSuccess={this.onRenderSuccess} pageNumber={1} />
        </Document>
      </div>
    );
  }
}

看起来完全没问题! 想知道您是否至少获得了 onSourceSuccess 回调...!
顺便说一句,我认为你不需要做这么多工作。

  1. 我相信你从fs.readFile得到的任何东西也会被接受。
  2. file={this.file}也应该没问题。

还有一个提示,componentWillMount 很快就会被弃用,请尝试在 componentDidMount 上加载文档:)

感谢您的提示,我已更改为componentDidMount


    1. 是的,它可以在没有Uint8Array


    1. 不,当我尝试直接加载文件而不传递对象时出现错误。

我发现了一个 hack ,我只需要知道加载第一页后的页数,所以我正在等待第一页的 renderSuccess 以获取文档状态中的页数。 我的项目有点忙,所以我没有时间调试它。 我不确定这是因为我在电子上运行还是因为我正在加载本地文件。 无论如何,当我有时间调试它时,我会及时通知您。

此页面是否有帮助?
0 / 5 - 0 等级