Some test text!
Web / Guides / React
This guide will show you how to integrate WebViewer Document Viewer & Editor into a React application.
You can watch a step-by-step video to help you get started.
You can also download a ready-to-go sample on GitHub.
Prior to starting, you should have already installed Node and npm.
Get your Apryse trial key.
Apryse collects some data regarding your usage of the SDK for product improvement.
If you wish to continue without data collection, contact us and we will email you a no-tracking trial key for you to get started.
Run the following command in your Terminal or Command Line:
npm i @pdftron/webviewer
Next we must copy the static assets required for WebViewer to run. The files are located in node_modules/@pdftron/webviewer/public
and must be moved into a location that will be served and publicly accessible. In React, it will be 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';
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 React public
folder.
Add your own license key in place of 'YOUR_LICENSE_KEY'.
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 npm start
.
You can now checkout other guides like sharing WebViewer instance across components , how to open your own documents or how to disable certain features .
Trial setup questions? Ask experts on Discord
Need other help? Contact Support
Pricing or product questions? Contact Sales