Displaying PDF in React app (2024)

Displaying PDF in React app (3)

I want to show you an easy and simple way to display PDF files using React in the browser. I will be using a library called react-pdf. This library is capable of rendering PDF files given an URL or a local file inside the project or base64 encoded version.

To get started, create a simple React application using Create React App. To do this you can run the following commands:

npx create-react-app display-pdf-app
cd display-pdf-app
npm start

After this, add the react-pdf library to the project using the following command:

npm install --save react-pdf 

To render the PDF file, react-pdf has a Document component where you need to pass a file prop. Then inside the Document component, you need to create a Page component. Simple isn’t it? Sample code:

import React from 'react';
import { Document, Page } from 'react-pdf';

import samplePDF from './test.pdf';

export default function Test() {
return (
<Document file={samplePDF}>
<Page pageNumber={1} />
</Document>
);
}

The final project structure will look like this:

Displaying PDF in React app (4)

Let’s start with creating single-page.js and all-pages.jsinside the components -pdf folder with the following contents:

  • single-page.js

Now navigate to App.js and replace the content with the following code:

Displaying PDF in React app (2024)

FAQs

How to display a pdf on react app? ›

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 to generate a pdf in React JS? ›

Simple usage of react-pdf

import { Document, Page, Text, View, StyleSheet, PDFViewer, } from "@react-pdf/renderer"; // Create styles const styles = StyleSheet. create({ page: { backgroundColor: "#d11fb6", color: "white", }, section: { margin: 10, padding: 10, }, viewer: { width: window.

How to display 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.

How to use pdf js in react js? ›

Install PDF. js using npm, import it into your React components, and initialize it to load and render PDF files. You will also need to handle PDF. js worker setup and manage PDF rendering within the React lifecycle methods.

How do I show uploaded files in Reactjs? ›

To upload image and preview it using React JS we will use the HTML file input for the image input. After taking input the image url is created using URL. createObjectURL() method and store in the useState variable named file. Display the image as preview using the html img tags with the file url in the src prop.

How to print a pdf in React? ›

Getting started 🚀
  1. Installation 💿 Get the react-print component library. ...
  2. Import component ↪️ Import the components you need to your PDF template from our list of pre-build components : ...
  3. Integrate in your document 📄 Integrate your components and include styles where needed. ...
  4. Generate HTML 💻

How to implement pdf JS? ›

To integrate PDF. js, download the library, include the necessary scripts in your HTML, and use JavaScript to load and render PDF files onto a canvas element.

How to upload a PDF in React? ›

To upload a PDF file in React, you can create a file input element and handle the file upload in React component using state. Then, you can send the selected file to your server or API for processing, or you can display the PDF in the browser by using a third-party library like react-pdf.

What is the alternative to React-PDF? ›

react-pdf alternatives and similar libraries
  • react-joyride. 8.7 7.8 react-pdf VS react-joyride. ...
  • react-resizable-and-movable. 8.1 6.9 react-pdf VS react-resizable-and-movable. ...
  • typography. 8.0 4.1 react-pdf VS typography. ...
  • react-split-pane. ...
  • react-simple-chatbot. ...
  • react-awesome-query-builder. ...
  • react-resizable-box. ...
  • react-pdf-viewer.
Jul 9, 2024

How to use React-PDF renderer? ›

Starting with react-pdf is extremely simple.
  1. Using npm. npm install @react-pdf/renderer --save.
  2. Using Yarn. yarn add @react-pdf/renderer.
  3. Using pnpm. pnpm add @react-pdf/renderer.
  4. Using Bun. bun add @react-pdf/renderer.
  5. Save in a file. import ReactPDF from '@react-pdf/renderer'; ReactPDF. ...
  6. Render to a stream. ...
  7. Render in DOM.

How to display pdf in React? ›

React-PDF
  1. Install by executing npm install react-pdf or yarn add react-pdf .
  2. Import by adding import { Document } from 'react-pdf' .
  3. Use by adding <Document file="..." /> . file can be a URL, base64 content, Uint8Array, and more.
  4. Put <Page /> components inside <Document /> to render pages.
Jul 2, 2024

How to view PDF using JS? ›

  1. Loading the PDF. Go to the index.js file and create a variable to hold the PDF document. ...
  2. Navigating through the PDF Pages. Until now, you've only rendered the first page of your PDF document. ...
  3. Displaying a Specific Page. ...
  4. Adding a Zoom Feature to PDF. ...
  5. Serving Your Website.

How to embed a PDF in JavaScript? ›

Embed PDF by integrating PDF.js

In order to do it, we will have to set up PDF. js on our server and then we will pass ForcePDFJS: true and PDFJS_URL: path/to/viewer. html/in/pdf.

How do I view PDF in screen? ›

You can refer to this guide:
  1. Open the PDF with Adobe Acrobat Reader.
  2. Press Ctrl + L to enter the full screen mode.
  3. To exit the full screen mode, you can press Esc or Ctrl + L again.
Nov 28, 2023

How to display a PDF in HTML? ›

Embed PDF in HTML Using the < embed > Tag. To embed external resources in a webpage, HTML provides an embed tag. Using the embed tag, we can include external resources such as PDFs, media players, and webpages. The tag has the src attribute, which allows us to specify the path to the file that will be embedded.

How to display a PDF in NextJS? ›

To open a file, you need to create a file picker and, when selecting a file, convert it to ArrayBuffer using the FileReader API (see an example here). Once you have the PDF in the ArrayBuffer format, you can call PSPDFKit. load with it.

How do I download a PDF from react app? ›

Approach 1: Using HTML | DOM Anchor Object

This approach use HTML DOM Anchor element to link a file adderess along with the anchor tag and download file using link. download with custom file name. Example: Here link the anchor tag to the pdf address / URL and use link. download to to save the file in local storage.

Top Articles
Help youth build the Five C’s: Competence, confidence, connection, character and compassion
How to Disable or Uninstall Windows Media Player in Windows 10
Metra Union Pacific West Schedule
What is Mercantilism?
Greedfall Console Commands
East Cocalico Police Department
Kansas Craigslist Free Stuff
America Cuevas Desnuda
Best Transmission Service Margate
3656 Curlew St
Driving Directions To Atlanta
Housework 2 Jab
272482061
Gdp E124
Navy Female Prt Standards 30 34
Tyler Sis University City
Transactions (zipForm Edition) | Lone Wolf | Real Estate Forms Software
Atdhe Net
Dark Entreaty Ffxiv
Danielle Ranslow Obituary
Https E22 Ultipro Com Login Aspx
Jesus Revolution Showtimes Near Regal Stonecrest
Margaret Shelton Jeopardy Age
Busted Mugshots Paducah Ky
Doctors of Optometry - Westchester Mall | Trusted Eye Doctors in White Plains, NY
Medline Industries, LP hiring Warehouse Operator - Salt Lake City in Salt Lake City, UT | LinkedIn
Stockton (California) – Travel guide at Wikivoyage
APUSH Unit 6 Practice DBQ Prompt Answers & Feedback | AP US History Class Notes | Fiveable
Craigslist Central Il
Sports Clips Flowood Ms
P3P Orthrus With Dodge Slash
Gerber Federal Credit
Σινεμά - Τι Ταινίες Παίζουν οι Κινηματογράφοι Σήμερα - Πρόγραμμα 2024 | iathens.gr
Craigslist Hamilton Al
The disadvantages of patient portals
Sc Pick 4 Evening Archives
Gym Assistant Manager Salary
Craigslist Farm And Garden Reading Pa
Satucket Lectionary
Peace Sign Drawing Reference
Pike County Buy Sale And Trade
Strange World Showtimes Near Century Stadium 25 And Xd
Sc Pick 3 Past 30 Days Midday
The 13 best home gym equipment and machines of 2023
Doelpuntenteller Robert Mühren eindigt op 38: "Afsluiten in stijl toch?"
Understanding & Applying Carroll's Pyramid of Corporate Social Responsibility
About us | DELTA Fiber
A Snowy Day In Oakland Showtimes Near Maya Pittsburg Cinemas
Where Is Darla-Jean Stanton Now
Varsity Competition Results 2022
Latest Posts
Article information

Author: Melvina Ondricka

Last Updated:

Views: 6384

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Melvina Ondricka

Birthday: 2000-12-23

Address: Suite 382 139 Shaniqua Locks, Paulaborough, UT 90498

Phone: +636383657021

Job: Dynamic Government Specialist

Hobby: Kite flying, Watching movies, Knitting, Model building, Reading, Wood carving, Paintball

Introduction: My name is Melvina Ondricka, I am a helpful, fancy, friendly, innocent, outstanding, courageous, thoughtful person who loves writing and wants to share my knowledge and understanding with you.