Basic usage - React PDF Viewer (2024)

Setting up the worker

pdfjs uses a web worker to process the most tasks which take time such as parsing and rendering a PDF document.The web worker is loaded via the `workerUrl` parameter:

import { Worker } from '@react-pdf-viewer/core';

<Worker workerUrl="https://unpkg.com/[email protected]/build/pdf.worker.min.js">

<!-- The viewer component will be put here -->

...

</Worker>

It's up to you to use the `pdf.worker.min.js` file from popular services, such as

or download and store it on your server.

Note

It's very important to note that the worker version and the `pdfjs` package (mentioned in the Install pdfjs librarysection of Getting started) have to be the same.

This guide uses the pdfjs v3.4.120.Otherwise, you will see the error message like this:

Or

Uncaught Error: Unknown action from worker: undefined

at Worker.MessageHandler._onComObjOnMessage (pdf.js:6846)

However, if you use the Webpack bundler, setting the same version for both worker and the `pdfjs-dist` package can be automated.There are two ways to archive that:

If you use the viewer component in different pages, it's recommended to place the `Worker` at the layout level.

For example, in a typical React application, we often render the `App` component at a `root` element as following:

render(<App />, document.getElementById('root'));

In this case, we should use the Worker component inside the `App` component:

const App = () => {

return <Worker workerUrl="https://unpkg.com/[email protected]/build/pdf.worker.min.js">...</Worker>;

};

Using the viewer component

When the worker is ready, it's time to use the viewer component. To display a PDF document, you have to pass its URL tothe viewer's `fileUrl` parameter:

// Import the main component

import { Viewer } from '@react-pdf-viewer/core';

// Import the styles

import '@react-pdf-viewer/core/lib/styles/index.css';

// Your render function

<Viewer fileUrl="/path/to/document.pdf" />;

The `fileUrl` accepts various sources of documents, including a base 64 string, an array of bytes, or a URL.It's possible to view a document from a remote server as long as the server allows us to access the document.

By default, the viewer will take the full size of its container. Hence you have to indicate the height of element that you wouldlike to display the document:

<div

style={{

border: '1px solid rgba(0, 0, 0, 0.3)',

height: '750px',

}}

>

<Viewer fileUrl="/assets/pdf-open-parameters.pdf" />

</div>

Here is how it looks like:

Congratulation! Now you know how to use the main viewer component to display a PDF document.

By default, the `Viewer` component does not come with other parts such as toolbar and sidebar.If you want to have a full features viewer, please use the default-layout plugin.

Basic usage - React PDF Viewer (2024)

FAQs

What is the default scale for react-PDF viewer? ›

React-PDF uses a default scale of 72ppi.

How to style react-PDF Viewer? ›

Inline styling

There's no need to call StyleSheet.create in order to style components. You can also just pass a plain JS object to the style prop and react-pdf will get the job done.

How does a PDF viewer work? ›

A PDF Reader is a software program that allows you to open and view PDF (Portable Document Format) files. The most basic function of a PDF reader is to present the content of a PDF file in a readable format. This includes text, images, links, and all other elements that might be included in a PDF.

How big should a React file be? ›

As a rule of thumb for our team at Finnovate.io, if a React component has more than 200 lines of code, then it is too big. Big components are difficult to read, difficult to maintain and nearly impossible to unit test. Fortunately, it is not so hard to abstract code away from components with a few pointers.

Does React scale well? ›

React apps are easy to scale thanks to modular nature — components are easy to extract, merge, and reuse. React's modular architecture simplifies its maintenance.

How do I display a PDF as an image in react? ›

import React from 'react'; import Pdf from './Pdf' const App = ()=> { return ( <div className="App"> //Rendering a pdf component <Pdf /> </div> ); } export default App; After creating a pdf component your project directory will look like this. Step 4: In this section, we load the PDF and render it on your app.

Which is the best PDF maker for React? ›

React-PDF is a unique library that provides powerful functionality for creating PDF files using React components. Instead of manually writing your document structure in a JavaScript object, you can create your PDF just like you would build a typical React application - using reusable components and props.

How to make a PDF viewer in React? ›

The simplest way to display a PDF document in React is by using the `<iframe>` element. You can embed the PDF file within an `<iframe>` tag and set the source to the URL of the PDF file.

How do I embed a PDF viewer? ›

Using an iframe tag is the second way to embed a pdf file in an HTML web page. In web development, web developers use the iframe tag to embed files in various formats and even other websites within a web page. Due to its wide compatibility, the iframe tag is widely used for embedding pdf.

How do I view a file in react? ›

Using the React File Viewer in your project is straightforward. First, you need to import the library into your project. Once you've imported the React File Viewer, you can use it to render files. You simply need to pass the file and its type as parameters to the FileViewer component.

Top Articles
What Are the Cheapest Decking Materials? - NA Deck and Patio
If you invest $10,000 today at 10% interest, how much will you have in 10 years?
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Shasta County Most Wanted 2022
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Selly Medaline
Latest Posts
Article information

Author: Ray Christiansen

Last Updated:

Views: 5709

Rating: 4.9 / 5 (69 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Ray Christiansen

Birthday: 1998-05-04

Address: Apt. 814 34339 Sauer Islands, Hirtheville, GA 02446-8771

Phone: +337636892828

Job: Lead Hospitality Designer

Hobby: Urban exploration, Tai chi, Lockpicking, Fashion, Gunsmithing, Pottery, Geocaching

Introduction: My name is Ray Christiansen, I am a fair, good, cute, gentle, vast, glamorous, excited person who loves writing and wants to share my knowledge and understanding with you.