react-pdf

react-pdf

React应用中集成PDF文件显示功能

React-PDF是一个用于在React应用中显示PDF文件的开源组件库。它支持通过URL、base64内容和Uint8Array等方式加载PDF,并提供Document和Page组件来渲染文档和页面。该库支持注释和文本层功能,兼容现代浏览器,并可配置以支持非拉丁字符和标准字体。React-PDF为开发者提供了一个简单而灵活的解决方案,用于在React项目中实现PDF查看功能。

React-PDFPDF渲染React组件文档显示JavaScript库Github开源项目

npm downloads CI

React-PDF

在您的React应用中像显示图片一样轻松地显示PDF。

迷路了?

这个包是用来"显示"已有的PDF。如果您想用React"创建"PDF,您可能在找@react-pdf/renderer

简要说明

  • 通过执行npm install react-pdfyarn add react-pdf安装。
  • 通过添加import { Document } from 'react-pdf'导入。
  • 通过添加<Document file="..." />使用。file可以是URL、base64内容、Uint8Array等。
  • <Document />内放置<Page />组件来渲染页面。

演示

sample目录中可以找到一个最小化的演示页面。

也可以查看在线演示

在继续之前

React-PDF正在持续开发中。本文档是为React-PDF 9.x分支编写的。如果您想查看其他版本的React-PDF文档,请使用GitHub页面顶部的下拉菜单切换到适当的标签。以下是每个分支最新文档的快速链接:

入门

兼容性

浏览器支持

React-PDF支持所有现代浏览器。它已在Chrome、Edge、Safari、Firefox和Opera的最新版本中进行了测试。

React-PDF v9默认支持以下浏览器:

  • Chrome ≥119
  • Edge ≥119
  • Safari ≥17.4
  • Firefox ≥121

您可以通过提供额外的polyfills(例如Array.prototype.atPromise.allSettledPromise.withResolvers)并配置您的打包工具以转译pdfjs-dist或使用旧版PDF.js worker来扩展支持的浏览器列表。

如果您需要支持更旧的浏览器,您需要使用React-PDF v6或更早版本。

React

要使用最新版本的React-PDF,您的项目需要使用React 16.8或更高版本。

如果您使用的是较旧版本的React,请参考下表找到合适的React-PDF版本。

React版本最新兼容的React-PDF版本
≥16.8最新版
≥16.35.x
≥15.54.x

Preact

React-PDF可以与Preact一起使用。

安装

通过执行npm install react-pdfyarn add react-pdf将React-PDF添加到您的项目中。

Next.js

如果您使用Next.js且未启用Turbopack,请在您的next.config.js中添加以下内容:

module.exports = { + webpack: (config) => { + config.resolve.alias.canvas = false; + return config; + }, }

如果您使用Next.js且启用了Turbopack,请添加empty-module.ts文件:

export default {};

并在您的next.config.js中添加以下内容:

module.exports = { + experimental: { + turbo: { + resolveAlias: { + canvas: './empty-module.ts', + }, + }, + }, };

配置PDF.js worker

为了使React-PDF正常工作,需要提供PDF.js worker。您有几个选择。

导入worker(推荐)

对于大多数情况,以下示例将起作用:

import { pdfjs } from 'react-pdf'; pdfjs.GlobalWorkerOptions.workerSrc = new URL( 'pdfjs-dist/build/pdf.worker.min.mjs', import.meta.url, ).toString();

[!注意] 在Next.js中:

  • 使用App Router时,确保在文件顶部添加'use client';
  • 使用Pages Router时,确保在导入使用此代码的组件时禁用SSR

[!注意] pnpm需要一个包含public-hoist-pattern[]=pdfjs-dist.npmrc文件才能正常工作。

<details> <summary>查看更多示例</summary>
Parcel 2

对于Parcel 2,您需要使用稍微不同的代码:

pdfjs.GlobalWorkerOptions.workerSrc = new URL( - 'pdfjs-dist/build/pdf.worker.min.mjs', + 'npm:pdfjs-dist/build/pdf.worker.min.mjs', import.meta.url, ).toString();
</details>

将worker复制到public目录

您需要自行确保pdfjs-dist/build中的pdf.worker.mjs文件被复制到项目的输出文件夹中。

例如,您可以使用自定义脚本:

import path from 'node:path'; import fs from 'node:fs'; const pdfjsDistPath = path.dirname(require.resolve('pdfjs-dist/package.json')); const pdfWorkerPath = path.join(pdfjsDistPath, 'build', 'pdf.worker.mjs'); fs.cpSync(pdfWorkerPath, './dist/pdf.worker.mjs', { recursive: true });

使用外部CDN

import { pdfjs } from 'react-pdf'; pdfjs.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.mjs`;

旧版PDF.js worker

如果您需要支持较旧的浏览器,可以使用旧版PDF.js worker。要这样做,请按照上述说明进行操作,但在PDF.js worker导入路径中将/build/替换为legacy/build/,例如:

pdfjs.GlobalWorkerOptions.workerSrc = new URL( - 'pdfjs-dist/build/pdf.worker.min.mjs', + 'pdfjs-dist/legacy/build/pdf.worker.min.mjs', import.meta.url, ).toString();

或:

-pdfjs.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.mjs`; +pdfjs.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${pdfjs.version}/legacy/build/pdf.worker.min.mjs`;

使用

以下是基本用法的示例:

import { useState } from 'react'; import { Document, Page } from 'react-pdf'; function MyApp() { const [numPages, setNumPages] = useState<number>(); const [pageNumber, setPageNumber] = useState<number>(1); function onDocumentLoadSuccess({ numPages }: { numPages: number }): void { setNumPages(numPages); } 返回 ( <div> <Document file="somefile.pdf" onLoadSuccess={onDocumentLoadSuccess}> <Page pageNumber={pageNumber} /> </Document> <p> {pageNumber} 页,共 {numPages} </p> </div> ); }

请查看本仓库中的 sample 目录以获取完整的工作示例。更多示例和更高级的用例,请查看 React-PDF Wiki 中的 Recipes

支持注释

如果你想在 React-PDF 渲染的 PDF 中使用注释(例如链接),那么你需要包含必要的样式表以正确显示注释,如下所示:

import 'react-pdf/dist/Page/AnnotationLayer.css';

支持文本层

如果你想在 React-PDF 渲染的 PDF 中使用文本层,那么你需要包含必要的样式表以正确显示文本层,如下所示:

import 'react-pdf/dist/Page/TextLayer.css';

支持非拉丁字符

如果你想确保包含非拉丁字符的 PDF 能够完美渲染,或者你遇到了以下警告:

警告:必须指定 CMap "baseUrl" 参数,请确保提供了 "cMapUrl" 和 "cMapPacked" API 参数。

那么你还需要在你的构建中包含 cMaps,并告诉 React-PDF 它们的位置。

复制 cMaps

首先,你需要从 pdfjs-dist(React-PDF 的依赖项 - 如果你已安装 React-PDF,它应该在你的 node_modules 中)复制 cMaps。cMaps 位于 pdfjs-dist/cmaps

Vite

通过执行 npm install vite-plugin-static-copy --save-devyarn add vite-plugin-static-copy --dev 添加 vite-plugin-static-copy,并在你的 Vite 配置中添加以下内容:

+import path from 'node:path'; +import { createRequire } from 'node:module'; -import { defineConfig } from 'vite'; +import { defineConfig, normalizePath } from 'vite'; +import { viteStaticCopy } from 'vite-plugin-static-copy'; +const require = createRequire(import.meta.url); + +const pdfjsDistPath = path.dirname(require.resolve('pdfjs-dist/package.json')); +const cMapsDir = normalizePath(path.join(pdfjsDistPath, 'cmaps')); export default defineConfig({ plugins: [ + viteStaticCopy({ + targets: [ + { + src: cMapsDir, + dest: '', + }, + ], + }), ] });
Webpack

通过执行 npm install copy-webpack-plugin --save-devyarn add copy-webpack-plugin --dev 添加 copy-webpack-plugin,并在你的 Webpack 配置中添加以下内容:

+import path from 'node:path'; +import CopyWebpackPlugin from 'copy-webpack-plugin'; +const pdfjsDistPath = path.dirname(require.resolve('pdfjs-dist/package.json')); +const cMapsDir = path.join(pdfjsDistPath, 'cmaps'); module.exports = { plugins: [ + new CopyWebpackPlugin({ + patterns: [ + { + from: cMapsDir, + to: 'cmaps/' + }, + ], + }), ], };
其他工具

如果你使用其他打包工具,你需要自行确保 cMaps 被复制到你项目的输出文件夹中。

例如,你可以使用自定义脚本,如:

import path from 'node:path'; import fs from 'node:fs'; const pdfjsDistPath = path.dirname(require.resolve('pdfjs-dist/package.json')); const cMapsDir = path.join(pdfjsDistPath, 'cmaps'); fs.cpSync(cMapsDir, 'dist/cmaps/', { recursive: true });

设置 React-PDF

现在你的构建中已经有了 cMaps,通过使用 options 属性向 Document 组件传递所需的选项,如下所示:

// 在 React 组件外部 const options = { cMapUrl: '/cmaps/', }; // 在 React 组件内部 <Document options={options} />;

[!注意] 确保在你的 React 组件外部定义 options 对象,如果不能,请使用 useMemo

或者,你可以使用外部 CDN 的 cMaps:

// 在 React 组件外部 import { pdfjs } from 'react-pdf'; const options = { cMapUrl: `https://unpkg.com/pdfjs-dist@${pdfjs.version}/cmaps/`, }; // 在 React 组件内部 <Document options={options} />;

支持标准字体

如果你想支持使用标准字体的 PDF(在 PDF 1.5 中已弃用,但仍在使用),或者你遇到了以下警告:

必须指定标准字体 "baseUrl" 参数,请确保提供了 "standardFontDataUrl" API 参数。

那么你还需要在你的构建中包含标准字体,并告诉 React-PDF 它们的位置。

复制字体

首先,你需要从 pdfjs-dist(React-PDF 的依赖项 - 如果你已安装 React-PDF,它应该在你的 node_modules 中)复制标准字体。标准字体位于 pdfjs-dist/standard_fonts

Vite

通过执行 npm install vite-plugin-static-copy --save-devyarn add vite-plugin-static-copy --dev 添加 vite-plugin-static-copy,并在你的 Vite 配置中添加以下内容:

+import path from 'node:path'; +import { createRequire } from 'node:module'; -import { defineConfig } from 'vite'; +import { defineConfig, normalizePath } from 'vite'; +import { viteStaticCopy } from 'vite-plugin-static-copy'; +const require = createRequire(import.meta.url); +const standardFontsDir = normalizePath( + path.join(path.dirname(require.resolve('pdfjs-dist/package.json')), 'standard_fonts') +); export default defineConfig({ plugins: [ + viteStaticCopy({ + targets: [ + { + src: standardFontsDir, + dest: '', + }, + ], + }), ] });
Webpack

通过执行 npm install copy-webpack-plugin --save-devyarn add copy-webpack-plugin --dev 添加 copy-webpack-plugin,并在你的 Webpack 配置中添加以下内容:

+import path from 'node:path'; +import CopyWebpackPlugin from 'copy-webpack-plugin'; +const standardFontsDir = path.join(path.dirname(require.resolve('pdfjs-dist/package.json')), 'standard_fonts'); module.exports = { plugins: [ + new CopyWebpackPlugin({ + patterns: [ + { + from: standardFontsDir, + to: 'standard_fonts/' + }, + ], + }), ], };
其他工具

如果你使用其他打包工具,你需要自行确保标准字体被复制到你项目的输出文件夹中。

例如,你可以使用自定义脚本,如:

import path from 'node:path'; import fs from 'node:fs'; const pdfjsDistPath = path.dirname(require.resolve('pdfjs-dist/package.json')); const standardFontsDir = path.join(pdfjsDistPath, 'standard_fonts'); fs.cpSync(standardFontsDir, 'dist/standard_fonts/', { recursive: true });

设置 React-PDF

现在你的构建中已经有了标准字体,通过使用 options 属性向 Document 组件传递所需的选项,如下所示:

// 在 React 组件外部 const options = { standardFontDataUrl: '/standard_fonts/', }; // 在 React 组件内部 <Document options={options} />;

[!注意] 确保在你的 React 组件外部定义 options 对象,如果不能,请使用 useMemo

或者,你可以使用外部 CDN 的标准字体:

// 在 React 组件外部 import { pdfjs } from 'react-pdf'; const options = { standardFontDataUrl: `https://unpkg.com/pdfjs-dist@${pdfjs.version}/standard_fonts`, }; // React 组件内部 <Document options={options} />;

用户指南

Document

加载通过 file 属性传递的文档。

属性

属性名描述默认值示例值
className将添加到渲染元素的类名,与默认的 react-pdf__Document 一起使用。不适用<ul><li>字符串:<br />"custom-class-name-1 custom-class-name-2"</li><li>字符串数组:<br />["custom-class-name-1", "custom-class-name-2"]</li></ul>
error组件在发生错误时应显示的内容。"Failed to load PDF file."<ul><li>字符串:<br />"An error occurred!"</li><li>React 元素:<br /><p>An error occurred!</p></li><li>函数:<br />this.renderError</li></ul>
externalLinkRel注释中渲染的链接的 rel 属性。"noopener noreferrer nofollow"rel 属性的有效值之一:<ul><li>"noopener"</li><li>"noreferrer"</li><li>"nofollow"</li><li>"noopener noreferrer"</li></ul>
externalLinkTarget注释中渲染的外部链接的 target 属性。未设置,将使用默认行为target 属性的有效值之一:<ul><li>"_self"</li><li>"_blank"</li><li>"_parent"</li><li>"_top"</li></ul>
file要显示的 PDF。<br />可以是 URL、文件(通过 import … from … 导入或从表单元素的文件输入中获取),或带参数的对象(url - URL;data - 数据,最好是 Uint8Array;range - PDFDataRangeTransport)。<br />警告:由于使用全等检查(===)来确定 file 对象是否已更改,必须通过将其设置在组件的状态、使用 useMemo 或其他类似技术来进行记忆化。不适用<ul><li>URL:<br />"https://example.com/sample.pdf"</li><li>文件:<br />import importedPdf from '../static/sample.pdf' 然后<br />sample</li><li>参数对象:<br />{ url: 'https://example.com/sample.pdf' }</ul>
imageResourcesPath用于为注释 SVG 的 src 属性添加前缀的路径。不适用(pdf.js 将回退到空字符串)"/public/images/"
inputRef类似于 ref 的属性,但它会传递给 <Document> 组件渲染的主 <div>不适用<ul><li>函数:<br />(ref) => { this.myDocument = ref; }</li><li>使用 createRef 创建的 Ref:<br />this.ref = createRef();<br /><br />inputRef={this.ref}</li><li>使用 useRef 创建的 Ref:<br />const ref = useRef();<br /><br />inputRef={ref}</li></ul>
loading组件在加载时应显示的内容。"Loading PDF…"<ul><li>字符串:<br />"Please wait!"</li><li>React 元素:<br /><p>Please wait!</p></li><li>函数:<br />this.renderLoader</li></ul>
noData组件在没有数据时应显示的内容。"No PDF file specified."<ul><li>字符串:<br />"Please select a file."</li><li>React 元素:<br /><p>Please select a file.</p></li><li>函数:<br />this.renderNoData</li></ul>
onItemClick点击大纲项目或缩略图时调用的函数。通常,你会使用这个回调来将用户移动到他们请求的位置。不适用({ dest, pageIndex, pageNumber }) => alert('Clicked an item from page ' + pageNumber + '!')
onLoadError加载文档时发生错误时调用的函数。不适用(error) => alert('Error while loading document! ' + error.message)
onLoadProgress加载过程中可能多次调用的函数。不适用({ loaded, total }) => alert('Loading a document: ' + (loaded / total) * 100 + '%')
onLoadSuccess文档成功加载时调用的函数。不适用(pdf) => alert('Loaded a file with ' + pdf.numPages + ' pages!')
onPassword加载受密码保护的 PDF 时调用的函数。提示用户输入密码的函数。(callback) => callback('s3cr3t_p4ssw0rd')
onSourceErrorfile 属性检索文档源时发生错误时调用的函数。不适用(error) => alert('Error while retrieving document source! ' + error.message)
onSourceSuccessfile 属性成功检索文档源时调用的函数。不适用() => alert('Document source retrieved!')
options一个对象,可以在其中定义要传递给 PDF.js 的其他参数。最值得注意的有:<ul><li>cMapUrl;</li><li>httpHeaders - 自定义请求头,例如用于授权);</li><li>withCredentials - 一个布尔值,指示是否在请求中包含 cookie(默认为 false</li></ul>有关可能参数的完整列表,请查看 PDF.js 文档中的 DocumentInitParameters<br /><br />注意:确保在 React 组件外部定义 options 对象,如果不行,请使用 useMemo不适用{ cMapUrl: '/cmaps/' }
renderMode文档的渲染模式。可以是 "canvas""custom""none"。如果设置为 "custom",还必须提供 customRenderer"canvas""custom"
rotate文档的旋转角度。如果提供,将全局更改旋转,即使对于已给定自己的 rotate 属性的页面也是如此。90 = 向右旋转,180 = 上下颠倒,270 = 向左旋转。不适用90

Page

显示一个页面。应放置在 <Document /> 内部。或者,也可以传递 pdf 属性,该属性可以从 <Document />onLoadSuccess 回调函数中获取,但一些高级功能(如渲染注释和文档内页面之间的链接)可能无法正常工作。

属性

属性名描述默认值示例值
canvasBackground画布背景颜色。可使用任何有效的 canvas.fillStyle"transparent"
canvasRef类似于 ref 的属性,但它传递给 <Canvas> 组件渲染的 <canvas><ul><li>函数:<br />(ref) => { this.myCanvas = ref; }</li><li>使用 createRef 创建的 Ref:<br />this.ref = createRef();<br /><br />inputRef={this.ref}</li><li>使用 useRef 创建的 Ref:<br />const ref = useRef();<br /><br />inputRef={ref}</li></ul>
className将添加到渲染元素的类名,与默认的 react-pdf__Page 一起使用。<ul><li>字符串:<br />"custom-class-name-1 custom-class-name-2"</li><li>字符串数组:<br />["custom-class-name-1", "custom-class-name-2"]</li></ul>
customRenderer自定义页面渲染方式的函数。使用此属性时必须将 renderMode 设置为 "custom"MyCustomRenderer
customTextRenderer自定义文本层渲染方式的函数。({ str, itemIndex }) => str.replace(/ipsum/g, value => `<mark>${value}</mark>`)
devicePixelRatio当前设备上物理像素与设备独立像素(DIPs)之间的比率。window.devicePixelRatio1
error发生错误时组件应显示的内容。"Failed to load the page."<ul><li>字符串:<br />"An error occurred!"</li><li>React 元素:<br /><p>An error occurred!</p></li><li>函数:<br />this.renderError</li></ul>
height页面高度。如果未定义 heightwidth,页面将以 PDF 中定义的大小渲染。如果同时定义 widthheight,将忽略 height。如果同时定义 heightscale,高度将乘以给定的系数。页面默认高度300
imageResourcesPath用于为注释 SVG 的 src 属性添加前缀的路径。无(pdf.js 将默认使用空字符串)"/public/images/"
inputRef类似于 ref 的属性,但它传递给 <Page> 组件渲染的主 <div><ul><li>函数:<br />(ref) => { this.myPage = ref; }</li><li>使用 createRef 创建的 Ref:<br />this.ref = createRef();<br /><br />inputRef={this.ref}</li><li>使用 useRef 创建的 Ref:<br />const ref = useRef();<br /><br />inputRef={ref}</li></ul>
loading加载时组件应显示的内容。"Loading page…"<ul><li>字符串:<br />"Please wait!"</li><li>React 元素:<br /><p>Please wait!</p></li><li>函数:<br />this.renderLoader</li></ul>
noData无数据时组件应显示的内容。"No page specified."<ul><li>字符串:<br />"Please select a page."</li><li>React 元素:<br /><p>Please select a page.</p></li><li>函数:<br />this.renderNoData</li></ul>
onGetAnnotationsError加载注释时发生错误时调用的函数。(error) => alert('Error while loading annotations! ' + error.message)
onGetAnnotationsSuccess注释成功加载时调用的函数。(annotations) => alert('Now displaying ' + annotations.length + ' annotations!')
onGetStructTreeError加载结构树时发生错误时调用的函数。(error) => alert('Error while loading structure tree! ' + error.message)
onGetStructTreeSuccess结构树成功加载时调用的函数。(structTree) => alert(JSON.stringify(structTree))
onGetTextError加载文本层项目时发生错误时调用的函数。(error) => alert('Error while loading text layer items! ' + error.message)
onGetTextSuccess文本层项目成功加载时调用的函数。({ items, styles }) => alert('Now displaying ' + items.length + ' text layer items!')
onLoadError加载页面时发生错误时调用的函数。(error) => alert('Error while loading page! ' + error.message)
onLoadSuccess页面成功加载时调用的函数。(page) => alert('Now displaying a page number ' + page.pageNumber + '!')
onRenderAnnotationLayerError渲染注释层时发生错误时调用的函数。(error) => alert('Error while loading annotation layer! ' + error.message)
onRenderAnnotationLayerSuccess注释成功渲染到屏幕上时调用的函数。() => alert('Rendered the annotation layer!')
onRenderError渲染页面时发生错误时调用的函数。(error) => alert('Error while loading page! ' + error.message)
onRenderSuccess页面成功渲染到屏幕上时调用的函数。() => alert('Rendered the page!')
onRenderTextLayerError渲染文本层时发生错误时调用的函数。(error) => alert('Error while rendering text layer! ' + error.message)
onRenderTextLayerSuccess文本层成功渲染到屏幕上时调用的函数。() => alert('Rendered the text layer!')
pageIndex应显示 PDF 文件中的哪一页,按页面索引。如果提供了 pageNumber 属性,则忽略此属性。01
pageNumber应显示 PDF 文件中的哪一页,按页码。如果提供了此属性,将忽略 pageIndex 属性。12
pdf<Document />onLoadSuccess 回调函数中获取的 pdf 对象。(自动从父级 <Document /> 获取)pdf
renderAnnotationLayer是否应渲染注释(如链接)。truefalse
renderForms是否应渲染表单。renderAnnotationLayer 属性必须设置为 truefalsetrue
renderMode文档的渲染模式。可以是 "canvas""custom""none"。如果设置为 "custom",还必须提供 customRenderer"canvas""custom"
renderTextLayer是否应该渲染文本层。truefalse
rotate页面旋转的角度。90 = 向右旋转,180 = 上下颠倒,270 = 向左旋转。页面的默认设置,通常为 090
scale页面缩放比例。10.5
width页面宽度。如果未定义 heightwidth,页面将以 PDF 中定义的大小进行渲染。如果同时定义了 widthheightheight 将被忽略。如果同时定义了 widthscale,宽度将乘以给定的因子。页面的默认宽度300

大纲

显示大纲(目录)。应放置在 <Document /> 内部。或者,可以传递 pdf 属性,该属性可以从 <Document />onLoadSuccess 回调函数中获得。

属性

属性名描述默认值示例值
className将添加到渲染元素的类名,与默认的 react-pdf__Outline 一起使用。<ul><li>字符串:<br />"custom-class-name-1 custom-class-name-2"</li><li>字符串数组:<br />["custom-class-name-1", "custom-class-name-2"]</li></ul>
inputRef行为类似于 ref,但传递给 <Outline> 组件渲染的主 <div><ul><li>函数:<br />(ref) => { this.myOutline = ref; }</li><li>使用 createRef 创建的 Ref:<br />this.ref = createRef();<br /><br />inputRef={this.ref}</li><li>使用 useRef 创建的 Ref:<br />const ref = useRef();<br /><br />inputRef={ref}</li></ul>
onItemClick点击大纲项时调用的函数。通常,你会使用此回调将用户移动到他们请求的位置。({ dest, pageIndex, pageNumber }) => alert('Clicked an item from page ' + pageNumber + '!')
onLoadError检索大纲时发生错误时调用的函数。(error) => alert('Error while retrieving the outline! ' + error.message)
onLoadSuccess成功检索大纲时调用的函数。(outline) => alert('The outline has been successfully retrieved.')

缩略图

显示页面的缩略图。不渲染注释层或文本层。不将自身注册为链接目标,因此用户点击内部链接(例如目录中的链接)时不会滚动到缩略图组件。点击时,尝试导航到被点击的页面(类似于大纲中的链接)。应放置在 <Document /> 内部。或者,可以传递 pdf 属性,该属性可以从 <Document />onLoadSuccess 回调函数中获得。

属性

属性与 <Page /> 组件相同,但某些与注释层和文本层相关的属性不可用:

  • customTextRenderer
  • onGetAnnotationsError
  • onGetAnnotationsSuccess
  • onGetTextError
  • onGetTextSuccess
  • onRenderAnnotationLayerError
  • onRenderAnnotationLayerSuccess
  • onRenderTextLayerError
  • onRenderTextLayerSuccess
  • renderAnnotationLayer
  • renderForms
  • renderTextLayer

此外,还提供了额外的属性:

属性名描述默认值示例值
className将添加到渲染元素的类名,与默认的 react-pdf__Thumbnail 一起使用。<ul><li>字符串:<br />"custom-class-name-1 custom-class-name-2"</li><li>字符串数组:<br />["custom-class-name-1", "custom-class-name-2"]</li></ul>
onItemClick点击缩略图时调用的函数。通常,你会使用此回调将用户移动到他们请求的位置。({ dest, pageIndex, pageNumber }) => alert('Clicked an item from page ' + pageNumber + '!')

有用链接

许可证

MIT 许可证。

作者

<table> <tr> <td > <img src="https://avatars.githubusercontent.com/u/5426427?v=4&s=128" width="64" height="64" alt="Wojciech Maj"> </td> <td> <a href="https://github.com/wojtekmaj">Wojciech Maj</a> </td> </tr> </table>

致谢

如果没有 Niklas Närhinen 创建的原始版本以及 Mozilla 开发的 pdf.js,这个项目是不可能实现的。感谢你们!

赞助商

感谢所有赞助商成为赞助商,您的图片将出现在我们的 GitHub README 上。

<a href="https://opencollective.com/react-pdf-wojtekmaj#sponsors" target="_blank"><img src="https://yellow-cdn.veclightyear.com/ab5030c0/c3d92cfe-7dff-4036-85ad-e5d4e4f666ca.svg?width=890"></a>

支持者

感谢所有支持者成为支持者,您的图片将出现在我们的 GitHub README 上。

<a href="https://opencollective.com/react-pdf-wojtekmaj#backers" target="_blank"><img src="https://yellow-cdn.veclightyear.com/ab5030c0/c78e9cfd-0232-4116-a9b1-b8a80a7a344f.svg?width=890"></a>

主要贡献者

感谢所有为这个项目做出贡献的人!

主要贡献者

编辑推荐精选

TRAE编程

TRAE编程

AI辅助编程,代码自动修复

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
蛙蛙写作

蛙蛙写作

AI小说写作助手,一站式润色、改写、扩写

蛙蛙写作—国内先进的AI写作平台,涵盖小说、学术、社交媒体等多场景。提供续写、改写、润色等功能,助力创作者高效优化写作流程。界面简洁,功能全面,适合各类写作者提升内容品质和工作效率。

AI辅助写作AI工具蛙蛙写作AI写作工具学术助手办公助手营销助手AI助手
问小白

问小白

全能AI智能助手,随时解答生活与工作的多样问题

问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。

热门AI助手AI对话AI工具聊天机器人
Transly

Transly

实时语音翻译/同声传译工具

Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。

讯飞智文

讯飞智文

一键生成PPT和Word,让学习生活更轻松

讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。

AI办公办公工具AI工具讯飞智文AI在线生成PPTAI撰写助手多语种文档生成AI自动配图热门
讯飞星火

讯飞星火

深度推理能力全新升级,全面对标OpenAI o1

科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。

热门AI开发模型训练AI工具讯飞星火大模型智能问答内容创作多语种支持智慧生活
Spark-TTS

Spark-TTS

一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型

Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。

咔片PPT

咔片PPT

AI助力,做PPT更简单!

咔片是一款轻量化在线演示设计工具,借助 AI 技术,实现从内容生成到智能设计的一站式 PPT 制作服务。支持多种文档格式导入生成 PPT,提供海量模板、智能美化、素材替换等功能,适用于销售、教师、学生等各类人群,能高效制作出高品质 PPT,满足不同场景演示需求。

讯飞绘文

讯飞绘文

选题、配图、成文,一站式创作,让内容运营更高效

讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。

热门AI辅助写作AI工具讯飞绘文内容运营AI创作个性化文章多平台分发AI助手
材料星

材料星

专业的AI公文写作平台,公文写作神器

AI 材料星,专业的 AI 公文写作辅助平台,为体制内工作人员提供高效的公文写作解决方案。拥有海量公文文库、9 大核心 AI 功能,支持 30 + 文稿类型生成,助力快速完成领导讲话、工作总结、述职报告等材料,提升办公效率,是体制打工人的得力写作神器。

下拉加载更多