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
What is ATR indicator and how to use it in trading
Under court deal with SEC, Binance can continue U.S. operations amid fraud suit
Kmart near me - Perth, WA
Koopa Wrapper 1 Point 0
Ups Stores Near
7 Verification of Employment Letter Templates - HR University
Cottonwood Vet Ottawa Ks
Tyson Employee Paperless
Napa Autocare Locator
Limp Home Mode Maximum Derate
Voorraad - Foodtrailers
Teenbeautyfitness
Sitcoms Online Message Board
Best Restaurants Ventnor
Valentina Gonzalez Leak
How to Store Boiled Sweets
How do you like playing as an antagonist? - Goonstation Forums
Rainfall Map Oklahoma
Craigslist Malone New York
Arboristsite Forum Chainsaw
Daily Voice Tarrytown
Wicked Local Plymouth Police Log 2022
NHS England » Winter and H2 priorities
No Hard Feelings - Stream: Jetzt Film online anschauen
ELT Concourse Delta: preparing for Module Two
Transactions (zipForm Edition) | Lone Wolf | Real Estate Forms Software
Fsga Golf
Melendez Imports Menu
Walgreens 8 Mile Dequindre
Poochies Liquor Store
Cor Triatriatum: Background, Pathophysiology, Epidemiology
Craigslist Northern Minnesota
Ts Modesto
Myaci Benefits Albertsons
Motor Mounts
Slv Fed Routing Number
Murphy Funeral Home & Florist Inc. Obituaries
Appleton Post Crescent Today's Obituaries
Darrell Waltrip Off Road Center
Polk County Released Inmates
The Land Book 9 Release Date 2023
ATM Near Me | Find The Nearest ATM Location | ATM Locator NL
Busch Gardens Wait Times
Ross Dress For Less Hiring Near Me
The best specialist spirits store | Spirituosengalerie Stuttgart
Playboi Carti Heardle
Euro area international trade in goods surplus €21.2 bn
Grand Park Baseball Tournaments
Optimal Perks Rs3
Parks And Rec Fantasy Football Names
Craigslist.raleigh
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.