呈现整个文档时,React和PDF.js更改页面不起作用

时间:2018-12-24 12:17:38

标签: javascript reactjs pdf pdfjs

我正在尝试使用PDF.js库在React的Web应用程序中呈现PDF文件。即使在正式的git中,也有很多不同的信息,所以我坚持尝试呈现整个pdf文档,并且还具有转到特定页面的功能。这就是我现在拥有的:

import * as pdfWorker from 'pdfjs-dist/build/pdf.worker'
import PdfJsLib from 'pdfjs-dist';
import 'pdfjs-dist/web/pdf_viewer.css';
import * as pdfjsViewer from 'pdfjs-dist/web/pdf_viewer';

...

componentDidMount() {
        let container = this.viewerContainer.current;
        let viewer = this.viewer.current;

        PdfJsLib.GlobalWorkerOptions.workerSrc = pdfWorker;
        PdfJsLib.getDocument(this.props.file).then((pdf) => {

            this.setState({numPages: pdf.numPages});

            this.pdfLinkService = new pdfjsViewer.PDFLinkService();

            this.pdfFindController = new pdfjsViewer.PDFFindController({
                linkService: this.pdfLinkService,
            });

            this.pdfViewer = new pdfjsViewer.PDFViewer({
                 container,
                 viewer,
                 linkService: this.pdfLinkService,
                 findController: this.pdfFindController
            });
            this.pdfLinkService.setViewer(this.pdfViewer);

            this.pdfViewer.setDocument(pdf);
            this.pdfLinkService.setDocument(pdf, null);

            this.pdfViewer.onePageRendered.then(() => {
                 pdf.getOutline().then((outline) => {
                     this.outline = outline || null;

                     if (!outline) {
                        return;
                     }
                     this.setState({bookmarkItems: outline});
            });
       });
   });
}



...

onNextPageClick = () => {
    if(this.state.numPages > this.state.pageNumber) {
        this.setState({pageNumber: this.state.pageNumber + 1});
        this.setState({pageNumberInput: this.state.pageNumber + 1});

        this.pdfViewer.currentPageNumber++;
    }
}
onPreviousPageClick = () => {
    if(this.state.pageNumber > 1) {
        this.setState({pageNumber: this.state.pageNumber - 1});
        this.setState({pageNumberInput: this.state.pageNumber - 1});

        this.pdfViewer.currentPageNumber--;
    }
}

...

render() {
    return(

        ...

        <div className="splitToolbarButton hiddenSmallView">
            <button className="toolbarButton pageUp" title="Previous Page" id="previous" tabIndex="13" data-l10n-id="previous" onClick={this.onPreviousPageClick}>
            <span data-l10n-id="previous_label">Previous</span>
            </button>
         <div className="splitToolbarButtonSeparator" />
            <button className="toolbarButton pageDown" title="Next Page" id="next" tabIndex="14" data-l10n-id="next" onClick={this.onNextPageClick}>
             <span data-l10n-id="next_label">Next</span>
            </button>
        </div>

         ...

        <div id="viewerContainer" ref={this.viewerContainer}>
            <div id="viewer" ref={this.viewer} className="pdfViewer" />
        </div>
    )
}

因此,使用所有这些代码,我都可以完整呈现pdf文档,但是在跳转到特定页面时遇到了问题。奇怪的是,仅当我使用new pdfjsViewer.PDFViewer渲染整个文档时,我才能跳到页面,但是当我使用new pdfjsViewer.PDFSinglePageViewer时,我只能正常渲染一页时,无法跳转到页面。跳到页面。那么有人知道我想念的东西吗,这让我头痛了好几个星期了?谢谢。

1 个答案:

答案 0 :(得分:1)

我找到了我的问题所在。我不知道ID为div的{​​{1}}元素必须具有viewerContainer才能使position: absolute实际执行某些操作。 scrollIntoView元素div也是如此。当然,这现在很有意义,但是我一直在寻找错误的地方,以为它必须与React或mainContainer之类的服务有关。