How to download PDF file in ReactJS ? - GeeksforGeeks (2024)

Last Updated : 24 Jul, 2024

Summarize

Comments

Improve

To download pdf in React JS there are methods given in JavaScript and HTML DOM. They provide a convenient way to download files and store them in local computers. Here we will use locally stored pdf files in the project folder to use as a sample pdf downloaded by the browser.

These are the approaches to download a PDF in React JS projects.

Table of Content

  • Using HTML | DOM Anchor Object
  • Using fetch() method

Steps to create the application:

Step 1: Create a React.js application using the following command:

npx create-react-app <project-name>

Step 2: After creating your project folder, move into that directory using the following command:

cd <project-name>

Step 3: You need to copy and paste your PDF file into the Public folder.

Project Structure: Your project structure will look like this:

How to download PDF file in ReactJS ? - GeeksforGeeks (1)

Let’s understand the implementation through example.

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.

JavaScript
import React from "react";const App = () => { const onButtonClick = () => { const pdfUrl = "Sample.pdf"; const link = document.createElement("a"); link.href = pdfUrl; link.download = "document.pdf"; // specify the filename document.body.appendChild(link); link.click(); document.body.removeChild(link); }; return ( <> <center> <h1>Welcome to Geeks for Geeks</h1> <h3> Click on below button to download PDF file </h3> <button onClick={onButtonClick}> Download PDF </button> </center> </> );};export default App;

Steps to run the program: To run the application using the below command:

npm start

Output: The output will be visible in http://localhost:3000/

How to download PDF file in ReactJS ? - GeeksforGeeks (2)

Approach 2: Using fetch() method

We will use the fetch() method provided by JavaScript to get PDF files from specified locations. To achieve this task we do not need to install any external module.

Example: In this example, we will use the fetch() method provided by Java Script and directly send the file name as a parameter.

App.js
// Filename - App.jsimport React from "react";const App = () => { // Function will execute on click of button const onButtonClick = () => {  // using Java Script method to get PDF file fetch("SamplePDF.pdf").then((response) => { response.blob().then((blob) => {  // Creating new object of PDF file const fileURL = window.URL.createObjectURL(blob);  // Setting various property values let alink = document.createElement("a"); alink.href = fileURL; alink.download = "SamplePDF.pdf"; alink.click(); }); }); }; return ( <> <center> <h1>Welcome to Geeks for Geeks</h1> <h3> Click on below button to download PDF file </h3> <button onClick={onButtonClick}> Download PDF </button> </center> </> );};export default App;

Steps to run the program: To run the application using the below command:

npm start

Output: The output will be visible in http://localhost:3000/

How to download PDF file in ReactJS ? - GeeksforGeeks (3)



Please Login to comment...

How to download PDF file in ReactJS ? - GeeksforGeeks (2024)
Top Articles
Tips for Handling Identity Verification Requests From the IRS
Central Bank: Definition, History, Functions, and Limitations
Friskies Tender And Crunchy Recall
Ohio Houses With Land for Sale - 1,591 Properties
Walgreens Boots Alliance, Inc. (WBA) Stock Price, News, Quote & History - Yahoo Finance
Craigslist St. Paul
Txtvrfy Sheridan Wy
Top Financial Advisors in the U.S.
LA Times Studios Partners With ABC News on Randall Emmett Doc Amid #Scandoval Controversy
Ucf Event Calendar
Erin Kate Dolan Twitter
World History Kazwire
Athens Bucket List: 20 Best Things to Do in Athens, Greece
Oc Craiglsit
Dc Gas Login
Dr Adj Redist Cadv Prin Amex Charge
Tvtv.us Duluth Mn
Iu Spring Break 2024
Fraction Button On Ti-84 Plus Ce
Fort Mccoy Fire Map
Euro Style Scrub Caps
Food Universe Near Me Circular
Teen Vogue Video Series
Rust Belt Revival Auctions
Bòlèt Florida Midi 30
Understanding Gestalt Principles: Definition and Examples
Is Henry Dicarlo Leaving Ktla
Orange Park Dog Racing Results
What is Software Defined Networking (SDN)? - GeeksforGeeks
Rogold Extension
47 Orchid Varieties: Different Types of Orchids (With Pictures)
Soiza Grass
1400 Kg To Lb
404-459-1280
Giantess Feet Deviantart
Chris Provost Daughter Addie
Edict Of Force Poe
Hisense Ht5021Kp Manual
Daily Jail Count - Harrison County Sheriff's Office - Mississippi
Can You Buy Pedialyte On Food Stamps
The Thing About ‘Dateline’
Check From Po Box 1111 Charlotte Nc 28201
Thelemagick Library - The New Comment to Liber AL vel Legis
Acts 16 Nkjv
Nu Carnival Scenes
Craigslist Binghamton Cars And Trucks By Owner
Eat Like A King Who's On A Budget Copypasta
15 Best Places to Visit in the Northeast During Summer
Sandra Sancc
Worland Wy Directions
Definition of WMT
Wayward Carbuncle Location
Latest Posts
Article information

Author: Kerri Lueilwitz

Last Updated:

Views: 6028

Rating: 4.7 / 5 (67 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Kerri Lueilwitz

Birthday: 1992-10-31

Address: Suite 878 3699 Chantelle Roads, Colebury, NC 68599

Phone: +6111989609516

Job: Chief Farming Manager

Hobby: Mycology, Stone skipping, Dowsing, Whittling, Taxidermy, Sand art, Roller skating

Introduction: My name is Kerri Lueilwitz, I am a courageous, gentle, quaint, thankful, outstanding, brave, vast person who loves writing and wants to share my knowledge and understanding with you.