react方面的pdf_react-pdf

本文档介绍了如何使用React库`react-pdf`来简单方便地在React应用中展示PDF文件,包括安装、导入、基本用法以及一些高级配置选项,如启用PDF.js工作线程、支持注释和非拉丁字符。还提供了兼容性指南和示例代码。
摘要由CSDN通过智能技术生成

Display PDFs in your React app as easily as if they were images.

React-PDF

Display PDFs in your React app as easily as if they were images.

tl;dr

Install by executing npm install react-pdf or yarn add react-pdf.

Import by adding import { Document } from 'react-pdf'.

Use by adding . file can be a URL, base64 content, Uint8Array, and more.

Put components inside to render pages.

Demo

Minimal demo page is included in sample directory.

Online demo is also available!

Before you continue

React-PDF is under constant development. This documentation is written for React-PDF 4.x branch. If you want to see documentation for other versions of React-PDF, use dropdown on top of GitHub page to switch to an appropriate tag. Here are quick links to the newest docs from each branch:

Getting started

Compatibility

To use the latest version of React-PDF, your project needs to use React 16.3 or later.

If you use older version of React, please refer to the table below to find suitable React-PDF version. Don't worry - as long as you're running React 15.5 or later, you won't be missing out a lot!

React version

Newest compatible React-PDF version

≥16.3

4.x

≥15.5

3.x

≥0.13

0.0.10

≥0.11

0.0.4

Installation

Add React-PDF to your project by executing npm install react-pdf or yarn add react-pdf.

Usage

Here's an example of basic usage:

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 (

file="somefile.pdf"

onLoadSuccess={this.onDocumentLoadSuccess}

>

Page {pageNumber} of {numPages}

);

}

}

Check the sample directory in this repository for a full working example. For more examples and more advanced use cases, check Recipes in React-PDF Wiki.

Enable PDF.js worker

It is crucial for performance to use PDF.js worker whenever possible. This ensures that PDF files will be rendered in a separate thread without affecting page performance. To make things a little easier, we've prepared several entry points you can use.

Webpack

Instead of directly importing/requiring 'react-pdf', import it like so:

import { Document } from 'react-pdf/dist/entry.webpack';

Parcel

Instead of directly importing/requiring 'react-pdf', import it like so:

import { Document } from 'react-pdf/dist/entry.parcel';

Create React App

Create React App uses Webpack under the hood, but instructions for Webpack will not work. Standard instructions apply.

Standard (Browserify and others)

If you use Browserify or other bundling tools, you will have to make sur

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值