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
404 Error Page
How to recover crypto you accidentally sent to the wrong network address
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: 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.