react方面的pdf_react-pdf

Display PDFs in your React app as easily as if they were images. React-PDFDisplay PDFs in your React app as easily as if they were images.tl;drInstall by executing npm install react-pdf or yarn ad...
摘要由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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React中使用react-pdf插件来实现弹窗预览PDF的功能是可行的。根据你提供的引用内容,你已经选择了react-pdf插件来实现这个需求。为了解决react-pdf插件的一些问题,你可以在引入react-pdf的下一行代码中添加以下代码来解决问题:pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`。这段代码可以确保react-pdf插件正常工作。此外,你还提到了react-file-viewer插件可以用来预览docx和xlsx格式的文件。所以,你可以使用react-pdf插件来预览PDF文件,而使用react-file-viewer插件来预览docx和xlsx文件。这样可以满足你的需求。 #### 引用[.reference_title] - *1* *2* [react中使用react-pdf弹窗预览pdf](https://blog.csdn.net/michen1127/article/details/131416422)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [React文件预览,React实现在线预览docx,xslx,pdf格式文件](https://blog.csdn.net/a82048195/article/details/107681577)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值