react-pdf

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

A minimal demo page can be found 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:

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

If you use an older version of React, please refer to the table below to a 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

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

Here's an example of basic usage:

import React, useState > from 'react';
import Document, Page > from 'react-pdf';
function MyApp()
const [numPages, setNumPages] = useState(null);
const [pageNumber, setPageNumber] = useState(1);
function onDocumentLoadSuccess( numPages >)
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.

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.

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

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

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 sure on your own that pdf.worker.js file from pdfjs-dist/build is copied to your project's output folder.

Alternatively, you could use pdf.worker.js from an external CDN:

import pdfjs > from 'react-pdf';
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/$pdfjs.version>/pdf.worker.js`;

Support for annotations

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';

Support for non-latin characters

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 copy-webpack-plugin to your project if you haven't already:

npm install copy-webpack-plugin --save-dev 

Now, in your Webpack config, import the plugin:

import CopyWebpackPlugin from 'copy-webpack-plugin';

and in plugins section of your config, add the following:

new CopyWebpackPlugin([
from: 'node_modules/pdfjs-dist/cmaps/',
to: 'cmaps/'
>,
]),

Parcel, Browserify and others

If you use Parcel, Browserify or other bundling tools, 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 by using options prop, like so:

Document
options=
cMapUrl: 'cmaps/',
cMapPacked: true,
>>
/>

Alternatively, you could use cMaps from external CDN:

import pdfjs > from 'react-pdf';
Document
options=
cMapUrl: `//cdn.jsdelivr.net/npm/pdfjs-dist@$pdfjs.version>/cmaps/`,
cMapPacked: true,
>>
/>

Loads a document passed using file prop.

Displays a page. Should be placed inside . Alternatively, it can have pdf prop passed, which can be obtained from 's onLoadSuccess callback function, however some advanced functions like linking between pages inside a document may not be working correctly.

Displays an outline (table of contents). Should be placed inside . Alternatively, it can have pdf prop passed, which can be obtained from 's onLoadSuccess callback function.

The MIT License.

Wojciech Maj
kontakt@wojtekmaj.pl
http://wojtekmaj.pl

This project wouldn't be possible without awesome work of Niklas Närhinen niklas@narhinen.net who created its initial 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!