javascript - 在 'react-pdf' 中加载超过 1 个 pdf 页面

标签 javascript reactjs

我正在用 React 构建一个具有显示 pdf 功能的项目。 我正在使用示例(在 repo 中)本身提供的基本最少代码。 但是,代码仅显示 pdf 的第一页,其余部分将被忽略。我为此使用“react-pdf”。 我

以下是我的代码..

import React, {Component} from 'react';
import './DocumentationPage.css';
import HeaderComponent from '../../components/Header/Header.js';
import { Document, Page } from 'react-pdf/build/entry.webpack';
import data from './data.pdf';
// import { Document, Page } from 'react-pdf';

export default class DocumentationPage extends Component {
    state = {
        numPages: 12,
        pageNumber: 1,
      }

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

        return (
            <div>
                <HeaderComponent id='header' location={this.props.location} />
                <div>
                    <h1>Documentation</h1>
                </div>
                <div id='contentDiv'>
                    <Document
                        file={data}
                        onLoadSuccess={this.onDocumentLoad}>
                            <Page pageNumber={pageNumber} />
                    </Document>
                    <p>Page {pageNumber} of {numPages}</p>
                </div>
            </div>
        );
    }
}

请帮忙。 谢谢。

最佳答案

如果页面总数未知,则呈现所有页面的更通用方法...

const [numPages, setNumPages] = useState(null);
.
.
.
<Document
    file={data}
    onLoadSuccess={({ numPages })=>setNumPages(numPages)}
>
    {Array.apply(null, Array(numPages))
    .map((x, i)=>i+1)
    .map(page => <Page pageNumber={page}/>)}
</Document>

关于javascript - 在 'react-pdf' 中加载超过 1 个 pdf 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47225553/

相关文章:

javascript - 如何使用 Raphael 和 jQuery 检测 IE 上的点击或鼠标按下

javascript - 无法读取帖子响应中的正文 [express.js]

reactjs - 使用 React Shallow Rendering 设置状态

reactjs - react /TSX : child unable to infer correct generic type from parent

javascript - ImageBackground 包装器内的 React Native Text 未显示(带图像)

javascript - div 上 Const 的 ReactJS 值

javascript - HTML Javascript - 防止从 dom 树的子节点执行脚本

reactjs - Firestore get() 和 onSnapshot 函数在 useEffect 中不起作用

javascript - 为什么当 props 改变时我的页面没有重新渲染? React新手用头撞墙两天了

javascript - 在对可观察数组中的项目进行更改后,如何立即看到更新?