react-pdf - npm-1
react-pdf - npm-1
com/package/react-pdf
Sign Up Sign In
react-pdf
9.2.1 • Public • Published 12 hours ago
Readme
Code Beta
8 Dependencies
918 Dependents
148 Versions
React-PDF
Display PDFs in your React app as easily as if they were images.
Lost?
This package is used to display existing PDFs. If you wish to create PDFs using React, you
may be looking for @react-pdf/renderer.
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 <Document file="..." /> . file can be a URL, base64 content,
Uint8Array, and more.
• Put <Page /> components inside <Document /> to render pages.
Demo
A minimal demo page can be found in sample directory.
• v8.x
• v7.x
• v6.x
• v5.x
• v4.x
• v3.x
• v2.x
• v1.x
Getting started
Compatibility
Browser support
React-PDF supports all modern browsers. It is tested with the latest versions of Chrome,
Edge, Safari, Firefox, and Opera.
The following browsers are supported out of the box in React-PDF v9:
• Chrome ≥119
• Edge ≥119
• Safari ≥17.4
• Firefox ≥121
You may extend the list of supported browsers by providing additional polyfills (e.g. for
Array.prototype.at , Promise.allSettled or Promise.withResolvers ) and
either configuring your bundler to transpile pdfjs-dist or using legacy PDF.js worker.
If you need to support older browsers, you will need to use React-PDF v6 or earlier.
React
To use the latest version of React-PDF, your project needs to use React 16.8 or later.
If you use an older version of React, please refer to the table below to a find suitable
React-PDF version.
≥16.8 latest
≥16.3 5.x
≥15.5 4.x
Preact
Installation
Add React-PDF to your project by executing npm install react-pdf or yarn add
react-pdf .
Next.js
If you use Next.js without Turbopack enabled, add the following to your
next.config.js :
module.exports = {
+ webpack: (config) => {
+ config.resolve.alias.canvas = false;
+ return config;
+ },
}
module.exports = {
+ experimental: {
+ turbo: {
+ resolveAlias: {
+ canvas: './empty-module.ts',
+ },
+ },
+ },
};
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.
[!NOTE] In Next.js:
• Using App Router, make sure to add 'use client'; to the top of the file.
• Using Pages Router, make sure to disable SSR when importing the component
you're using this code in.
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.
pdfjs.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@
If you need to support older browsers, you may use legacy PDF.js worker. To do so, follow
the instructions above, but replace /build/ with legacy/build/ in PDF.js worker
import path, for example:
or:
-pdfjs.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${pdf
+pdfjs.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${pdf
Usage
Here's an example of basic usage:
function MyApp() {
const [numPages, setNumPages] = useState<number>();
const [pageNumber, setPageNumber] = useState<number>(1);
return (
<div>
<Document file="somefile.pdf" onLoadSuccess={onDocumentLoadSuc
<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.
import 'react-pdf/dist/Page/AnnotationLayer.css';
import 'react-pdf/dist/Page/TextLayer.css';
Warning: The CMap "baseUrl" parameter must be specified, ensure that the "cMa
then you would also need to include cMaps in your build and tell React-PDF where they
are.
Copying cMaps
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 .
Vite
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:
Webpack
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:
module.exports = {
plugins: [
+ new CopyWebpackPlugin({
+ patterns: [
+ {
+ from: cMapsDir,
+ to: 'cmaps/'
+ },
+ ],
+ }),
],
};
Other tools
If you use other bundlers, you will have to make sure on your own that cMaps are copied
to your project's output folder.
Setting up React-PDF
Now that you have cMaps in your build, pass required options to Document component
[!NOTE] Make sure to define options object outside of your React component, and
use useMemo if you can't.
const options = {
cMapUrl: `https://unpkg.com/pdfjs-dist@${pdfjs.version}/cmaps/`
};
The standard font "baseUrl" parameter must be specified, ensure that the "sta
then you would also need to include standard fonts in your build and tell React-PDF
Copying fonts
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 .
Vite
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:
+ }),
]
});
Webpack
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:
module.exports = {
plugins: [
+ new CopyWebpackPlugin({
+ patterns: [
+ {
+ from: standardFontsDir,
+ to: 'standard_fonts/'
+ },
+ ],
+ }),
],
};
Other tools
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.
Setting up React-PDF
Now that you have standard fonts in your build, pass required options to Document
component by using options prop, like so:
[!NOTE] Make sure to define options object outside of your React component, and
use useMemo if you can't.
const options = {
standardFontDataUrl: `https://unpkg.com/pdfjs-dist@${pdfjs.version
};
User guide
Document
Loads a document passed using file prop.
Props
• String:
Class name(s) that will be
"custom-cl
added to rendered
custom-cla
className element along with the n/a
• Array of string
default react-
["custom-c
pdf__Document .
"custom-cl
• String:
"An error
What the component "Failed to • React element
error should display in case of load PDF <p>An erro
an error. file." p>
• Function:
this.rende
One of valid
attribute
unset, which
Link target for external means that • "_self"
externalLinkTarget links rendered in default
• "_blank"
annotations. behavior will be
• "_parent"
used
• "_top"
• Function:
(ref) => {
this.myDoc
A prop that behaves like • Ref created us
ref, but it's passed to this.ref =
inputRef main <div> rendered n/a …
by <Document> inputRef={
component. • Ref created us
const ref
…
inputRef={
• String:
"Please wa
What the component
"Loading • React element
loading should display while
PDF…" <p>Please
loading.
• Function:
this.rende
• String:
"Please se
What the component "No PDF • React element
noData should display in case of file <p>Please
no data. specified." file.</p>
• Function:
this.rende
• cMapUrl ;
• httpHeaders -
custom request
headers, e.g. for
authorization);
• withCredentials
- a boolean to indicate
whether or not to
include cookies in the
request (defaults to
false )
Page
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.
Props
The path used to prefix the src n/a (pdf.js will fall
imageResourcesPath
attributes of annotation SVGs. empty string)
Outline
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.
Props
Default
Prop name Description Example values
value
• String:
Class name(s) that "custom-class-
will be added to name-1 custom-
rendered element class-name-2"
className n/a
along with the • Array of strings:
default react- ["custom-class-
pdf__Outline . name-1", "custom-
class-name-2"]
Default
Prop name Description Example values
value
• Ref created using useRef
const ref =
useRef();
…
inputRef={ref}
Function called
when an outline
item has been ({ dest, pageIndex,
clicked. Usually, pageNumber }) =>
onItemClick you would like to n/a alert('Clicked an
use this callback item from page ' +
to move the user pageNumber + '!')
wherever they
requested to.
(error) =>
Function called in
alert('Error while
case of an error
onLoadError n/a retrieving the
while retrieving
outline! ' +
the outline.
error.message)
Thumbnail
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
Props are the same as in <Page /> component, but certain annotation layer and text
layer-related props are not available:
• customTextRenderer
• onGetAnnotationsError
• onGetAnnotationsSuccess
• onGetTextError
• onGetTextSuccess
• onRenderAnnotationLayerError
• onRenderAnnotationLayerSuccess
• onRenderTextLayerError
• onRenderTextLayerSuccess
• renderAnnotationLayer
• renderForms
• renderTextLayer
Default
Prop name Description Example values
value
Default
Prop name Description Example values
value
• String:
"custom-class-
Class name(s) that will be name-1 custom-
added to rendered class-name-2"
className element along with the n/a • Array of strings:
default react- ["custom-
pdf__Thumbnail . class-name-1",
"custom-class-
name-2"]
({ dest,
Function called when a
pageIndex,
thumbnail has been
pageNumber }) =>
clicked. Usually, you
alert('Clicked
onItemClick would like to use this n/a
an item from
callback to move the user
page ' +
wherever they requested
pageNumber +
to.
'!')
Useful links
• React-PDF Wiki
License
The MIT License.
Author
Wojciech Maj
Thank you
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!
Sponsors
Thank you to all our sponsors! Become a sponsor and get your image on our README on
GitHub.
Backers
Thank you to all our backers! Become a backer and get your image on our README on
GitHub.
Top Contributors
Thank you to all our contributors that helped on this project!
Keywords
Provenance
Source Commit
github.com/wojtekmaj/react-pdf@5e0d135
Build File
.github/workflows/publish.yml
Public Ledger
Transparency log entry
Share feedback
Install
npm i react-pdf
Repository
github.com/wojtekmaj/react-pdf
Homepage
github.com/wojtekmaj/react-pdf#readme
Weekly Downloads
11,07,548
Version License
9.2.1 MIT
38 10
Last publish
12 hours ago
Collaborators
Try on RunKit
Report malware
Support
Help
Advisories
Status
Contact npm
Company
About
Blog
Press
Policies
Terms of Use
Code of Conduct
Privacy