Available Now: Explore our latest release with enhanced accessibility and powerful IDP features
By Andrey Safonov | 2023 Jun 05
3 min
Tags
pdf viewer
react
webviewer
react pdf viewer
In this blog, we will cover how to embed a PDF viewer into a React app as a component. We’ll be using the Apryse WebViewer in this tutorial, although there are other PDF viewers available. For example, there are a number of viewers out there primarily built on top of PDF.js. This is an open-source PDF rendering engine built by Mozilla and maintained by the development community.
A lot of projects like react-pdf, react-pdf/renderer, @phuocng/react-pdf-viewer, and react-file-viewer are based on PDF.js, although it has its shortcomings. In addition, all the above PDF viewers are maintained by a single dev, which is a tremendous effort.
Here are some top issues with PDF.js:
Nothing against PDF.js; it is good for what it is meant for, and that is for a quick preview in-browser. You can watch a video where I compare a number of open-source projects built on PDF.js. In some ways, it is even better than PDFium, but that is a story for another day.
Now why would you pick Apryse WebViewer to embed in a React App?
Okay, now let’s see how to get started with embedding Apryse WebViewer in your React App. You can also just clone a ready-to-go sample from our GitHub https://github.com/PDFTron/webviewer-react-sample.
Learn how to build a customizable PDF viewer in React.
From the terminal in your React app location, run the following command:
npm i @pdftron/webviewer
This pulls down all the necessary components. Next, you want to copy static resources necessary for WebViewer to run into your public directory. The files are located innode_modules/@pdftron/webviewer/public and must be moved into your public folder.
Inside of a GitHub project, we automate the copying of static resources by executing copy-webviewer-files.js.
Import WebViewer into your component.
import WebViewer from '@pdftron/webviewer';
Create a reference where WebViewer will be placed or mounted.
import {useRef} from 'react';
import WebViewer from '@pdftron/webviewer';
const MyComponent = () => {
const viewer = useRef(null);
return (
<div className="MyComponent">
<div className="header">React sample</div>
<div className="webviewer" ref={viewer} style={{height: "100vh"}}></div>
</div>
);
};
Inside of useEffect hook or componentDidMount lifecycle method, initialize WebViewer. Ensure that the path property in the constructor points to where you copied static assets node_modules/@pdftron/webviewer/public in the React public folder.
import {useEffect, useRef} from 'react';
import WebViewer from '@pdftron/webviewer'
const MyComponent = () => {
const viewer = useRef(null);
useEffect(() => {
WebViewer(
{
path: '/webviewer/lib',
licenseKey: 'YOUR_LICENSE_KEY',
initialDoc: 'https://pdftron.s3.amazonaws.com/downloads/pl/demo-annotated.pdf',
},
viewer.current,
).then((instance) => {
const { documentViewer } = instance.Core;
// you can now call WebViewer APIs here...
});
}, []);
return (
<div className="MyComponent">
<div className="header">React sample</div>
<div className="webviewer" ref={viewer} style={{height: "100vh"}}></div>
</div>
);
};
Run the app by running in your terminal:
npm start
Learn more about PDF editing and manipulation using React.
Now that you have PDF Viewer up and running check out our other guides on how to load files or how to customize the WebViewer. You can always grab a ready-to-go sample on our GitHub, or chat with us on chat with us on Discord if you get stuck.
PRODUCTS
Enterprise
Small Business
Popular Content