@react-pdf/renderer 和 react-pdf 都是 React 生态中处理 PDF 的核心库,但它们的职责截然不同。@react-pdf/renderer 是一个渲染引擎,允许开发者使用 React 组件和样式来从头构建 PDF 文件,它不依赖浏览器原生能力,可直接在 Node.js 或服务端生成二进制 PDF 流。而 react-pdf 是一个基于 Mozilla pdf.js 的查看器组件,专为在浏览器中渲染和展示现有的 PDF 文件而设计,提供缩放、翻页等交互功能。简而言之,前者用于“写”PDF,后者用于“读”PDF。
在 React 开发生态中,处理 PDF 通常涉及两个完全不同的阶段:创建文档和展示文档。@react-pdf/renderer 和 react-pdf 分别占据了这两个生态位。很多开发者容易混淆二者,试图用查看器去生成文件,或用生成器去展示复杂交互,这会导致架构上的严重失误。让我们深入技术细节,看看它们如何工作以及如何正确选型。
@react-pdf/renderer 是一个纯粹的生成引擎。它实现了一套类似 React 的虚拟 DOM 系统,但目标不是浏览器的 HTML,而是 PDF 规范。它不依赖浏览器环境,可以在 Node.js 服务器、Serverless 函数或客户端直接运行,输出二进制的 PDF 文件流。
// @react-pdf/renderer: 定义文档结构并生成文件
import { Document, Page, Text, StyleSheet, pdf } from '@react-pdf/renderer';
const MyDocument = () => (
<Document>
<Page size="A4">
<Text>发票编号:#1024</Text>
<Text>金额:$500.00</Text>
</Page>
</Document>
);
// 在浏览器或 Node.js 中获取 blob 或 buffer
const blob = await pdf(<MyDocument />).toBlob();
// 触发下载或发送给后端
react-pdf 是一个渲染组件。它底层依赖 Mozilla 的 pdf.js 库,将现有的 PDF 二进制数据解析并在 HTML5 Canvas 上绘制出来。它的核心任务是“展示”,提供缩放、翻页、文本选择等查看器功能,但它无法修改或创建 PDF 内容。
// react-pdf: 加载并展示现有 PDF 文件
import { Document, Page, pdfjs } from 'react-pdf';
import 'react-pdf/dist/Page/AnnotationLayer.css';
import 'react-pdf/dist/Page/TextLayer.css';
// 设置 worker 源(必须步骤)
pdfjs.GlobalWorkerOptions.workerSrc = '//cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.177/pdf.worker.min.js';
function Viewer({ fileUrl }) {
const [numPages, setNumPages] = useState(null);
function onDocumentLoadSuccess({ numPages }) {
setNumPages(numPages);
}
return (
<div>
<Document file={fileUrl} onLoadSuccess={onDocumentLoadSuccess}>
{Array.from(new Array(numPages), (el, index) => (
<Page key={`page_${index + 1}`} pageNumber={index + 1} />
))}
</Document>
</div>
);
}
@react-pdf/renderer 使用一套受限的 CSS 子集进行样式定义。它不支持所有 CSS 属性(例如没有 position: fixed 的复杂交互,也没有复杂的 flex 嵌套),因为它需要将样式转换为 PDF 规范指令。布局引擎是自定义的,专注于分页逻辑(例如内容超出页面自动换页)。
// @react-pdf/renderer: 样式定义类似 CSS-in-JS
const styles = StyleSheet.create({
page: {
flexDirection: 'row',
backgroundColor: '#E4E4E4',
padding: 30
},
section: {
margin: 10,
padding: 10,
flexGrow: 1,
fontSize: 12,
fontFamily: 'Helvetica' // 必须指定字体族
},
});
// 使用时
<View style={styles.page}>
<Text style={styles.section}>内容区域</Text>
</View>
react-pdf 不涉及样式定义(针对 PDF 内容),因为它只是忠实地渲染 PDF 文件内部已有的样式。你只能控制查看器容器的 CSS(如 canvas 的大小、边框、背景),而无法改变 PDF 内部的字体或布局。如果 PDF 本身字体缺失,它通常回退到默认字体,这取决于 pdf.js 的配置。
// react-pdf: 只能控制容器样式,无法干预 PDF 内部样式
<div style={{ border: '1px solid #ccc', maxWidth: '800px' }}>
<Page
pageNumber={1}
width={600} // 控制渲染宽度
renderTextLayer={true} // 开启文本层以支持复制
renderAnnotationLayer={true} // 开启注释层
/>
</div>
@react-pdf/renderer 的最大优势是同构能力。由于它不依赖 DOM 或 Canvas,它可以在 Node.js 服务端直接运行。这意味着你可以在 API 接口中接收请求,动态生成 PDF 并直接返回流,无需启动无头浏览器(如 Puppeteer)。
// Node.js 服务端示例 (Express)
import express from 'express';
import { pdf } from '@react-pdf/renderer';
import Invoice from './InvoiceTemplate';
const app = express();
app.get('/invoice/:id', async (req, res) => {
const doc = <Invoice id={req.params.id} />;
const buffer = await pdf(doc).toBuffer();
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-Disposition', 'attachment; filename=invoice.pdf');
res.send(buffer);
});
react-pdf 强依赖浏览器 API(如 canvas, worker)。虽然它有 SSR 的支持策略(通常通过动态导入避免服务端报错),但其核心功能必须在客户端浏览器中才能生效。它不适合在服务端生成内容,仅适合在服务端渲染(SSR)时输出占位符,然后在客户端注水(Hydration)加载真实视图。
// React 客户端组件 (需处理 SSR)
import dynamic from 'next/dynamic';
// 动态导入以避免 SSR 错误
const PDFViewer = dynamic(() => import('react-pdf').then(mod => mod.Document), {
ssr: false,
loading: () => <p>正在加载文档...</p>
});
// 仅在客户端渲染
export default function Page() {
return <PDFViewer file="/manual.pdf" />;
}
@react-pdf/renderer 要求开发者显式注册字体文件(TTF/OTF),特别是使用中文字体时。默认字体通常只支持西欧字符。如果不注册中文字体,生成的 PDF 中的中文会显示为方框或乱码。这需要额外的构建步骤来加载字体文件。
// @react-pdf/renderer: 必须注册字体
import { Font, StyleSheet } from '@react-pdf/renderer';
// 注册本地或远程字体文件
Font.register({
family: 'Noto Sans SC',
src: 'https://fonts.gstatic.com/s/notosanssc/v25/.../NotoSansSC-Regular.ttf'
});
const styles = StyleSheet.create({
text: { fontFamily: 'Noto Sans SC' }
});
react-pdf 自动处理字体。因为 PDF 文件在生成时已经嵌入了字体子集(或者依赖系统字体),pdf.js 负责解析这些嵌入数据并在 Canvas 上绘制。开发者通常不需要关心字体文件,除非 PDF 使用了非常特殊的编码且未正确嵌入,这时可能需要配置 cMapUrl 来支持 CJK(中日韩)字符的正确渲染。
// react-pdf: 配置 CMap 以支持特殊字符集
import { pdfjs } from 'react-pdf';
pdfjs.GlobalWorkerOptions.workerSrc = '...';
pdfjs.GlobalWorkerOptions.cMapUrl = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.177/cmaps/';
pdfjs.GlobalWorkerOptions.cMapPacked = true;
// 之后正常渲染即可,库会自动处理字符映射
@react-pdf/renderer 的性能瓶颈在于生成过程。对于几百页的复杂文档,构建虚拟 DOM 树并转换为 PDF 指令需要 CPU 计算时间。在客户端生成大文件可能会阻塞主线程,导致界面卡顿,建议在 Web Worker 中运行或使用服务端生成。
react-pdf 的性能瓶颈在于解析和绘制。大型 PDF 文件(如扫描版书籍)可能高达几十 MB,pdf.js 需要解析二进制流并逐页绘制到 Canvas。为了优化体验,通常采用“按需加载”策略,只渲染当前可视区域的页面,而不是一次性渲染所有页。
// react-pdf: 优化策略 - 仅渲染可见页
function OptimizedViewer({ file }) {
const [page, setPage] = useState(1);
return (
<div>
<button onClick={() => setPage(p => p - 1)}>上一页</button>
<Document file={file}>
{/* 只渲染当前页,而不是全部 */}
<Page pageNumber={page} />
</Document>
<button onClick={() => setPage(p => p + 1)}>下一页</button>
</div>
);
}
在实际企业级应用中,这两个库经常配合使用,而不是二选一。典型的场景是:用户使用 @react-pdf/renderer 在客户端或服务端生成一份报告,生成完成后,立即使用 react-pdf 在模态框中预览该文件,确认无误后再下载或发送。
@react-pdf/renderer 根据用户数据动态创建 PDF Blob。react-pdf 进行展示。import { pdf } from '@react-pdf/renderer';
import { Document, Page } from 'react-pdf';
import ReportTemplate from './ReportTemplate';
function ReportWorkflow() {
const [pdfUrl, setPdfUrl] = useState(null);
const generateAndPreview = async () => {
// 1. 生成 PDF
const blob = await pdf(<ReportTemplate data={...} />).toBlob();
// 2. 创建临时 URL
const url = URL.createObjectURL(blob);
setPdfUrl(url);
};
return (
<div>
<button onClick={generateAndPreview}>生成并预览报告</button>
{pdfUrl && (
<div className="preview-container">
<Document file={pdfUrl}>
<Page pageNumber={1} width={400} />
</Document>
</div>
)}
</div>
);
}
| 特性 | @react-pdf/renderer | react-pdf |
|---|---|---|
| 核心用途 | 创建 PDF 文件 | 查看 PDF 文件 |
| 底层依赖 | 自研渲染引擎 (无依赖) | Mozilla pdf.js |
| 运行环境 | Node.js, 浏览器, Serverless | 主要是浏览器 (需 Canvas) |
| 样式控制 | 使用 CSS 子集定义样式 | 无法修改 PDF 内部样式 |
| 字体支持 | 需手动注册 TTF/OTF | 自动解析嵌入字体 |
| 输出产物 | Blob, Buffer, Stream | React 组件 (Canvas DOM) |
| 典型场景 | 发票、报表导出、证书生成 | 文档阅读器、合同预览 |
不要试图用一个库解决所有问题。
如果你的需求是**“让用户下载一个文件”,或者“在服务器上自动化生成文档”**,请坚定选择 @react-pdf/renderer。它是目前 React 生态中生成 PDF 的事实标准,避免了引入沉重的无头浏览器方案。
如果你的需求是**“让用户在网页上看文件”**,请坚定选择 react-pdf。不要尝试用 HTML/CSS 去模拟 PDF 布局,也不要用 @react-pdf/renderer 生成后再转图片展示,那样会丢失文本可选择性和缩放清晰度。
高级用法:在复杂的 SaaS 应用中,通常两者并存。后端或边缘函数使用 @react-pdf/renderer 生成归档文件,前端使用 react-pdf 提供流畅的预览体验。理解它们的边界,能让你的文档处理架构更加清晰、高效。
选择 react-pdf 如果你的目标是让用户在网页应用中预览、查看或交互已有的 PDF 文件(如用户上传的合同、产品手册)。它适合构建文档阅读器、在线预览功能或需要自定义 PDF 查看器 UI(如工具栏、缩略图)的场景。注意,它不能用来创建新的 PDF 文件,仅用于渲染现有文件。
选择 @react-pdf/renderer 如果你的需求是动态生成 PDF 文件,例如发票、报告、简历或导出功能。它适合需要在服务端(Node.js)无头生成 PDF,或者需要在客户端将 React 界面直接转换为 PDF 下载的场景。如果你需要完全控制文档的布局、字体嵌入和内容结构,且不希望依赖浏览器的打印对话框,这是唯一的选择。
Display PDFs in your React app as easily as if they were images.
This package is used to display existing PDFs. If you wish to create PDFs using React, you may be looking for @react-pdf/renderer.
npm install react-pdf or yarn add react-pdf.import { Document } from 'react-pdf'.<Document file="..." />. file can be a URL, base64 content, Uint8Array, and more.<Page /> components inside <Document /> to render pages.A minimal demo page can be found in sample directory.
Online demo is also available!
React-PDF is under constant development. This documentation is written for React-PDF 10.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:
React-PDF supports the latest versions of all major modern browsers.
Browser compatibility for React-PDF primarily depends on PDF.js support. For details, refer to the PDF.js documentation.
You may extend the list of supported browsers by providing additional polyfills (e.g. Array.prototype.at, Promise.allSettled or Promise.withResolvers) and configuring your bundler to transpile pdfjs-dist.
To use the latest version of React-PDF, your project needs to use React 16.8 or later.
React-PDF may be used with Preact.
Add React-PDF to your project by executing npm install react-pdf or yarn add react-pdf.
If you use Next.js prior to v15 (v15.0.0-canary.53, specifically), you may need to add the following to your next.config.js:
module.exports = {
+ swcMinify: false,
}
For React-PDF to work, PDF.js worker needs to be provided. You have several options.
For most cases, the following example will work:
import { pdfjs } from 'react-pdf';
pdfjs.GlobalWorkerOptions.workerSrc = new URL(
'pdfjs-dist/build/pdf.worker.min.mjs',
import.meta.url,
).toString();
[!WARNING] The
workerSrcmust be set in the same module where you use React-PDF components (e.g.,<Document>,<Page>). Setting it in a separate file likemain.tsxand then importing React-PDF in another component may cause the default value to overwrite your custom setting due to module execution order. Always configure the worker in the file where you render the PDF components.
[!NOTE] In Next.js, make sure to skip SSR when importing the module you're using this code in. Here's how to do this in Pages Router and App Router.
[!NOTE] pnpm requires an
.npmrcfile withpublic-hoist-pattern[]=pdfjs-distfor this to work.
For Parcel 2, you need to use a slightly different code:
pdfjs.GlobalWorkerOptions.workerSrc = new URL(
- 'pdfjs-dist/build/pdf.worker.min.mjs',
+ 'npm:pdfjs-dist/build/pdf.worker.min.mjs',
import.meta.url,
).toString();
You will have to make sure on your own that pdf.worker.mjs file from pdfjs-dist/build is copied to your project's output folder.
For example, you could use a custom script like:
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 });
import { pdfjs } from 'react-pdf';
pdfjs.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.mjs`;
[!WARNING] The
workerSrcmust be set in the same module where you use React-PDF components (e.g.,<Document>,<Page>). Setting it in a separate file likemain.tsxand then importing React-PDF in another component may cause the default value to overwrite your custom setting due to module execution order. Always configure the worker in the file where you render the PDF components.
Here's an example of basic usage:
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);
}
return (
<div>
<Document file="somefile.pdf" onLoadSuccess={onDocumentLoadSuccess}>
<Page pageNumber={pageNumber} />
</Document>
<p>
Page {pageNumber} of {numPages}
</p>
</div>
);
}
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.
If you want to use annotations (e.g. links) in PDFs rendered by React-PDF, then you would need to include stylesheet necessary for annotations to be correctly displayed like so:
import 'react-pdf/dist/Page/AnnotationLayer.css';
If you want to use text layer in PDFs rendered by React-PDF, then you would need to include stylesheet necessary for text layer to be correctly displayed like so:
import 'react-pdf/dist/Page/TextLayer.css';
If you want to ensure that PDFs with non-latin characters will render perfectly, or you have encountered the following warning:
Warning: The CMap "baseUrl" parameter must be specified, ensure that the "cMapUrl" and "cMapPacked" API parameters are provided.
then you would also need to include cMaps in your build and tell React-PDF where they are.
First, you need to copy cMaps from pdfjs-dist (React-PDF's dependency - it should be in your node_modules if you have React-PDF installed). cMaps are located in pdfjs-dist/cmaps.
Add vite-plugin-static-copy by executing npm install vite-plugin-static-copy --save-dev or yarn add vite-plugin-static-copy --dev and add the following to your Vite config:
+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: '',
+ },
+ ],
+ }),
]
});
Add copy-webpack-plugin by executing npm install copy-webpack-plugin --save-dev or yarn add copy-webpack-plugin --dev and add the following to your Webpack config:
+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/'
+ },
+ ],
+ }),
],
};
If you use other bundlers, you will have to make sure on your own that cMaps are copied to your project's output folder.
For example, you could use a custom script like:
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 });
Now that you have cMaps in your build, pass required options to Document component by using options prop, like so:
// Outside of React component
const options = {
cMapUrl: '/cmaps/',
};
// Inside of React component
<Document options={options} />;
[!NOTE] Make sure to define
optionsobject outside of your React component or useuseMemoif you can't.
Alternatively, you could use cMaps from external CDN:
// Outside of React component
import { pdfjs } from 'react-pdf';
const options = {
cMapUrl: `https://unpkg.com/pdfjs-dist@${pdfjs.version}/cmaps/`,
};
// Inside of React component
<Document options={options} />;
If you want to ensure that JPEG 2000 images in PDFs will render, or you have encountered the following warning:
Warning: Unable to decode image "img_p0_1": "JpxError: OpenJPEG failed to initialize".
then you would also need to include wasm directory in your build and tell React-PDF where it is.
First, you need to copy wasm from pdfjs-dist (React-PDF's dependency - it should be in your node_modules if you have React-PDF installed). cMaps are located in pdfjs-dist/wasm.
Add vite-plugin-static-copy by executing npm install vite-plugin-static-copy --save-dev or yarn add vite-plugin-static-copy --dev and add the following to your Vite config:
+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 wasmDir = normalizePath(path.join(pdfjsDistPath, 'wasm'));
export default defineConfig({
plugins: [
+ viteStaticCopy({
+ targets: [
+ {
+ src: wasmDir,
+ dest: '',
+ },
+ ],
+ }),
]
});
Add copy-webpack-plugin by executing npm install copy-webpack-plugin --save-dev or yarn add copy-webpack-plugin --dev and add the following to your Webpack config:
+import path from 'node:path';
+import CopyWebpackPlugin from 'copy-webpack-plugin';
+const pdfjsDistPath = path.dirname(require.resolve('pdfjs-dist/package.json'));
+const wasmDir = path.join(pdfjsDistPath, 'wasm');
module.exports = {
plugins: [
+ new CopyWebpackPlugin({
+ patterns: [
+ {
+ from: wasmDir,
+ to: 'wasm/'
+ },
+ ],
+ }),
],
};
If you use other bundlers, you will have to make sure on your own that wasm directory is copied to your project's output folder.
For example, you could use a custom script like:
import path from 'node:path';
import fs from 'node:fs';
const pdfjsDistPath = path.dirname(require.resolve('pdfjs-dist/package.json'));
const wasmDir = path.join(pdfjsDistPath, 'wasm');
fs.cpSync(wasmDir, 'dist/wasm/', { recursive: true });
Now that you have wasm directory in your build, pass required options to Document component by using options prop, like so:
// Outside of React component
const options = {
wasmUrl: '/wasm/',
};
// Inside of React component
<Document options={options} />;
[!NOTE] Make sure to define
optionsobject outside of your React component or useuseMemoif you can't.
Alternatively, you could use wasm directory from external CDN:
// Outside of React component
import { pdfjs } from 'react-pdf';
const options = {
wasmUrl: `https://unpkg.com/pdfjs-dist@${pdfjs.version}/wasm/`,
};
// Inside of React component
<Document options={options} />;
If you want to support PDFs using standard fonts (deprecated in PDF 1.5, but still around), or you have encountered the following warning:
The standard font "baseUrl" parameter must be specified, ensure that the "standardFontDataUrl" API parameter is provided.
then you would also need to include standard fonts in your build and tell React-PDF where they are.
First, you need to copy standard fonts from pdfjs-dist (React-PDF's dependency - it should be in your node_modules if you have React-PDF installed). Standard fonts are located in pdfjs-dist/standard_fonts.
Add vite-plugin-static-copy by executing npm install vite-plugin-static-copy --save-dev or yarn add vite-plugin-static-copy --dev and add the following to your Vite config:
+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: '',
+ },
+ ],
+ }),
]
});
Add copy-webpack-plugin by executing npm install copy-webpack-plugin --save-dev or yarn add copy-webpack-plugin --dev and add the following to your Webpack config:
+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/'
+ },
+ ],
+ }),
],
};
If you use other bundlers, you will have to make sure on your own that standard fonts are copied to your project's output folder.
For example, you could use a custom script like:
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 });
Now that you have standard fonts in your build, pass required options to Document component by using options prop, like so:
// Outside of React component
const options = {
standardFontDataUrl: '/standard_fonts/',
};
// Inside of React component
<Document options={options} />;
[!NOTE] Make sure to define
optionsobject outside of your React component or useuseMemoif you can't.
Alternatively, you could use standard fonts from external CDN:
// Outside of React component
import { pdfjs } from 'react-pdf';
const options = {
standardFontDataUrl: `https://unpkg.com/pdfjs-dist@${pdfjs.version}/standard_fonts/`,
};
// Inside of React component
<Document options={options} />;
Loads a document passed using file prop.
| Prop name | Description | Default value | Example values |
|---|---|---|---|
| className | Class name(s) that will be added to rendered element along with the default react-pdf__Document. | n/a |
|
| error | What the component should display in case of an error. | "Failed to load PDF file." |
|
| externalLinkRel | Link rel for links rendered in annotations. | "noopener noreferrer nofollow" | One of valid values for rel attribute.
|
| externalLinkTarget | Link target for external links rendered in annotations. | unset, which means that default behavior will be used | One of valid values for target attribute.
|
| file | What PDF should be displayed. Its value can be an URL, a file (imported using import … from … or from file input form element), or an object with parameters (url - URL; data - data, preferably Uint8Array; range - PDFDataRangeTransport.Warning: Since equality check ( ===) is used to determine if file object has changed, it must be memoized by setting it in component's state, useMemo or other similar technique. | n/a |
|
| imageResourcesPath | The path used to prefix the src attributes of annotation SVGs. | n/a (pdf.js will fallback to an empty string) | "/public/images/" |
| inputRef | A prop that behaves like ref, but it's passed to main <div> rendered by <Document> component. | n/a |
|
| loading | What the component should display while loading. | "Loading PDF…" |
|
| noData | What the component should display in case of no data. | "No PDF file specified." |
|
| onItemClick | Function called when an outline item or a thumbnail has been clicked. Usually, you would like to use this callback to move the user wherever they requested to. | n/a | ({ dest, pageIndex, pageNumber }) => alert('Clicked an item from page ' + pageNumber + '!') |
| onLoadError | Function called in case of an error while loading a document. | n/a | (error) => alert('Error while loading document! ' + error.message) |
| onLoadProgress | Function called, potentially multiple times, as the loading progresses. | n/a | ({ loaded, total }) => alert('Loading a document: ' + (loaded / total) * 100 + '%') |
| onLoadSuccess | Function called when the document is successfully loaded. | n/a | (pdf) => alert('Loaded a file with ' + pdf.numPages + ' pages!') |
| onPassword | Function called when a password-protected PDF is loaded. | Function that prompts the user for password. | (callback) => callback('s3cr3t_p4ssw0rd') |
| onSourceError | Function called in case of an error while retrieving document source from file prop. | n/a | (error) => alert('Error while retrieving document source! ' + error.message) |
| onSourceSuccess | Function called when document source is successfully retrieved from file prop. | n/a | () => alert('Document source retrieved!') |
| options | An object in which additional parameters to be passed to PDF.js can be defined. Most notably:
Note: Make sure to define options object outside of your React component or use useMemo if you can't. | n/a | { cMapUrl: '/cmaps/' } |
| renderMode | Rendering mode of the document. Can be "canvas", "custom" or "none". If set to "custom", customRenderer must also be provided. | "canvas" | "custom" |
| rotate | Rotation of the document in degrees. If provided, will change rotation globally, even for the pages which were given rotate prop of their own. 90 = rotated to the right, 180 = upside down, 270 = rotated to the left. | n/a | 90 |
| scale | Document scale. | 1 | 0.5 |
Displays a page. Should be placed inside <Document />. Alternatively, it can have pdf prop passed, which can be obtained from <Document />'s onLoadSuccess callback function, however some advanced functions like rendering annotations and linking between pages inside a document may not be working correctly.
| Prop name | Description | Default value | Example values |
|---|---|---|---|
| canvasBackground | Canvas background color. Any valid canvas.fillStyle can be used. | n/a | "transparent" |
| canvasRef | A prop that behaves like ref, but it's passed to <canvas> rendered by <Canvas> component. | n/a |
|
| className | Class name(s) that will be added to rendered element along with the default react-pdf__Page. | n/a |
|
| customRenderer | Function that customizes how a page is rendered. You must set renderMode to "custom" to use this prop. | n/a | MyCustomRenderer |
| customTextRenderer | Function that customizes how a text layer is rendered. | n/a | ({ str, itemIndex }) => str.replace(/ipsum/g, value => `<mark>${value}</mark>`) |
| devicePixelRatio | The ratio between physical pixels and device-independent pixels (DIPs) on the current device. | window.devicePixelRatio | 1 |
| error | What the component should display in case of an error. | "Failed to load the page." |
|
| filterAnnotations | Function to filter annotations before they are rendered. | n/a | ({ annotations }) => annotations.filter(annotation => annotation.subtype === 'Text') |
| height | Page height. If neither height nor width are defined, page will be rendered at the size defined in PDF. If you define width and height at the same time, height will be ignored. If you define height and scale at the same time, the height will be multiplied by a given factor. | Page's default height | 300 |
| imageResourcesPath | The path used to prefix the src attributes of annotation SVGs. | n/a (pdf.js will fallback to an empty string) | "/public/images/" |
| inputRef | A prop that behaves like ref, but it's passed to main <div> rendered by <Page> component. | n/a |
|
| loading | What the component should display while loading. | "Loading page…" |
|
| noData | What the component should display in case of no data. | "No page specified." |
|
| onGetAnnotationsError | Function called in case of an error while loading annotations. | n/a | (error) => alert('Error while loading annotations! ' + error.message) |
| onGetAnnotationsSuccess | Function called when annotations are successfully loaded. | n/a | (annotations) => alert('Now displaying ' + annotations.length + ' annotations!') |
| onGetStructTreeError | Function called in case of an error while loading structure tree. | n/a | (error) => alert('Error while loading structure tree! ' + error.message) |
| onGetStructTreeSuccess | Function called when structure tree is successfully loaded. | n/a | (structTree) => alert(JSON.stringify(structTree)) |
| onGetTextError | Function called in case of an error while loading text layer items. | n/a | (error) => alert('Error while loading text layer items! ' + error.message) |
| onGetTextSuccess | Function called when text layer items are successfully loaded. | n/a | ({ items, styles }) => alert('Now displaying ' + items.length + ' text layer items!') |
| onLoadError | Function called in case of an error while loading the page. | n/a | (error) => alert('Error while loading page! ' + error.message) |
| onLoadSuccess | Function called when the page is successfully loaded. | n/a | (page) => alert('Now displaying a page number ' + page.pageNumber + '!') |
| onRenderAnnotationLayerError | Function called in case of an error while rendering the annotation layer. | n/a | (error) => alert('Error while loading annotation layer! ' + error.message) |
| onRenderAnnotationLayerSuccess | Function called when annotations are successfully rendered on the screen. | n/a | () => alert('Rendered the annotation layer!') |
| onRenderError | Function called in case of an error while rendering the page. | n/a | (error) => alert('Error while loading page! ' + error.message) |
| onRenderSuccess | Function called when the page is successfully rendered on the screen. | n/a | () => alert('Rendered the page!') |
| onRenderTextLayerError | Function called in case of an error while rendering the text layer. | n/a | (error) => alert('Error while rendering text layer! ' + error.message) |
| onRenderTextLayerSuccess | Function called when the text layer is successfully rendered on the screen. | n/a | () => alert('Rendered the text layer!') |
| pageColors | Colors used to render the page. If not provided, the default colors from PDF will be used. | n/a | { background: 'black', foreground: '#ffff00' } |
| pageIndex | Which page from PDF file should be displayed, by page index. Ignored if pageNumber prop is provided. | 0 | 1 |
| pageNumber | Which page from PDF file should be displayed, by page number. If provided, pageIndex prop will be ignored. | 1 | 2 |
pdf object obtained from <Document />'s onLoadSuccess callback function. | (automatically obtained from parent <Document />) | pdf | |
| renderAnnotationLayer | Whether annotations (e.g. links) should be rendered. | true | false |
| renderForms | Whether forms should be rendered. renderAnnotationLayer prop must be set to true. | false | true |
| renderMode | Rendering mode of the document. Can be "canvas", "custom" or "none". If set to "custom", customRenderer must also be provided. | "canvas" | "custom" |
| renderTextLayer | Whether a text layer should be rendered. | true | false |
| rotate | Rotation of the page in degrees. 90 = rotated to the right, 180 = upside down, 270 = rotated to the left. | Page's default setting, usually 0 | 90 |
| scale | Page scale. | 1 | 0.5 |
| width | Page width. If neither height nor width are defined, page will be rendered at the size defined in PDF. If you define width and height at the same time, height will be ignored. If you define width and scale at the same time, the width will be multiplied by a given factor. | Page's default width | 300 |
Displays an outline (table of contents). Should be placed inside <Document />. Alternatively, it can have pdf prop passed, which can be obtained from <Document />'s onLoadSuccess callback function.
| Prop name | Description | Default value | Example values |
|---|---|---|---|
| className | Class name(s) that will be added to rendered element along with the default react-pdf__Outline. | n/a |
|
| inputRef | A prop that behaves like ref, but it's passed to main <div> rendered by <Outline> component. | n/a |
|
| onItemClick | Function called when an outline item has been clicked. Usually, you would like to use this callback to move the user wherever they requested to. | n/a | ({ dest, pageIndex, pageNumber }) => alert('Clicked an item from page ' + pageNumber + '!') |
| onLoadError | Function called in case of an error while retrieving the outline. | n/a | (error) => alert('Error while retrieving the outline! ' + error.message) |
| onLoadSuccess | Function called when the outline is successfully retrieved. | n/a | (outline) => alert('The outline has been successfully retrieved.') |
Displays a thumbnail of a page. Does not render the annotation layer or the text layer. Does not register itself as a link target, so the user will not be scrolled to a Thumbnail component when clicked on an internal link (e.g. in Table of Contents). When clicked, attempts to navigate to the page clicked (similarly to a link in Outline). Should be placed inside <Document />. Alternatively, it can have pdf prop passed, which can be obtained from <Document />'s onLoadSuccess callback function.
Props are the same as in <Page /> component, but certain annotation layer and text layer-related props are not available:
On top of that, additional props are available:
| Prop name | Description | Default value | Example values |
|---|---|---|---|
| className | Class name(s) that will be added to rendered element along with the default react-pdf__Thumbnail. | n/a |
|
| onItemClick | Function called when a thumbnail has been clicked. Usually, you would like to use this callback to move the user wherever they requested to. | n/a | ({ dest, pageIndex, pageNumber }) => alert('Clicked an item from page ' + pageNumber + '!') |
The MIT License.
|
| Wojciech Maj |
This project wouldn't be possible without the awesome work of Niklas Närhinen who created its original version and without Mozilla, author of pdf.js. Thank you!
Thank you to all our sponsors! Become a sponsor and get your image on our README on GitHub.
Thank you to all our backers! Become a backer and get your image on our README on GitHub.
Thank you to all our contributors that helped on this project!