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 Is The Rule of 72 to Double Your Investment - Its Formula And Calculation
Understanding Real Estate Agent Compensation in California
Pollen Count Los Altos
Cold Air Intake - High-flow, Roto-mold Tube - TOYOTA TACOMA V6-4.0
Methstreams Boxing Stream
Combat level
Chicago Neighborhoods: Lincoln Square & Ravenswood - Chicago Moms
Koordinaten w43/b14 mit Umrechner in alle Koordinatensysteme
Did 9Anime Rebrand
Bank Of America Appointments Near Me
Nikki Catsouras Head Cut In Half
Craigslist Cars And Trucks Buffalo Ny
Myunlb
Tamilblasters 2023
Craigslist Jobs Phoenix
Babyrainbow Private
Jack Daniels Pop Tarts
Samantha Lyne Wikipedia
Amc Flight Schedule
"Une héroïne" : les funérailles de Rebecca Cheptegei, athlète olympique immolée par son compagnon | TF1 INFO
Urban Dictionary: hungolomghononoloughongous
Tips on How to Make Dutch Friends & Cultural Norms
Doki The Banker
yuba-sutter apartments / housing for rent - craigslist
Wisconsin Volleyball Team Boobs Uncensored
Engineering Beauties Chapter 1
Craigslistodessa
Living Shard Calamity
§ 855 BGB - Besitzdiener - Gesetze
Keshi with Mac Ayres and Starfall (Rescheduled from 11/1/2024) (POSTPONED) Tickets Thu, Nov 1, 2029 8:00 pm at Pechanga Arena - San Diego in San Diego, CA
The Creator Showtimes Near Baxter Avenue Theatres
Greater Orangeburg
Napa Autocare Locator
How to Play the G Chord on Guitar: A Comprehensive Guide - Breakthrough Guitar | Online Guitar Lessons
Nacho Libre Baptized Gif
Andhra Jyothi Telugu News Paper
Mistress Elizabeth Nyc
Craigs List Palm Springs
The best bagels in NYC, according to a New Yorker
Guy Ritchie's The Covenant Showtimes Near Grand Theatres - Bismarck
FREE - Divitarot.com - Tarot Denis Lapierre - Free divinatory tarot - Your divinatory tarot - Your future according to the cards! - Official website of Denis Lapierre - LIVE TAROT - Online Free Tarot cards reading - TAROT - Your free online latin tarot re
Lucifer Morningstar Wiki
Exploring the Digital Marketplace: A Guide to Craigslist Miami
[Teen Titans] Starfire In Heat - Chapter 1 - Umbrelloid - Teen Titans
844 386 9815
Deezy Jamaican Food
Dyi Urban Dictionary
Sinai Sdn 2023
Worland Wy Directions
New Starfield Deep-Dive Reveals How Shattered Space DLC Will Finally Fix The Game's Biggest Combat Flaw
Public Broadcasting Service Clg Wiki
Lux Nails & Spa
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.