The following approach worked for me.
import ExampleDoc from '......src/assets/files/exampleDoc.pdf'
<a href={ExampleDoc} download="MyExampleDoc" target='_blank'>
<Button className={classes.navLink}>My Example Doc</Button>
</a>
Answer from Sai Lakshmy on Stack OverflowGitHub
github.com › diegomura › react-pdf › issues › 975
download pdf on custom button click · Issue #975 · diegomura/react-pdf
August 13, 2020 - diegomura / react-pdf Public · There was an error while loading. Please reload this page. Notifications · You must be signed in to change notification settings · Fork 1.3k · Star 16.5k · New issueCopy link · New issueCopy link · Closed · #1902 · Closed · download pdf on custom button click #975 ·
Author pradeepn541
React-pdf
react-pdf.org › advanced
React-pdf
import { PDFDownloadLink, Document, Page } from '@react-pdf/renderer'; const MyDoc = () => ( <Document> <Page> // My document data </Page> </Document> ); const App = () => ( <div> <PDFDownloadLink document={<MyDoc />} fileName="somename.pdf"> {({ blob, url, loading, error }) => loading ? 'Loading document...' : 'Download now!'
html - Download file by clicking a button in ReactJS - Stack Overflow
I have a website built with ReactJS and there are some files (PDF,DOC etc) that I want to let visitors download. But simple href tag doesn't start the download, instead refresh the page. More on stackoverflow.com
reactjs - How do I download a pdf file onClick with react-pdf? - Stack Overflow
I want to generate a pdf from the UI and download it. Been looking to the documentation but couldn't find how to implement I.e. onClick={this.downloadPdf} here's the module reference: https://github.com/diegomura/react-pdf More on stackoverflow.com
How to generate a PDF and download it without rendering it?
I was trying to generate a PDF with some layout. I have configured the values using hooks, but I want to download the generated PDF without rendering it to the client side. How can I achieve this w... More on github.com
How to render AND download on link clink, without rendering on page load
I have an application that generates a pdf based on the current state of the application when the user clicks a button. I'd like to use react-pdf, but I'm not sure how to make it work, since the default behavior seems to be to generate the pdf on page load and then only download it when the ... More on github.com
Videos
06:23
download as PDF button in React; pdf download button in react - ...
05:58
Download Pdf File In React - YouTube
08:16
How to download files in React JS | Download file instead of ...
13:03
How to create and download PDF file using React.js - YouTube
21:01
How to Export to PDF in React: Methods | React PDF Generator Part ...
43:11
How to create a PDF file using React.js - YouTube
Top answer 1 of 8
23
The following approach worked for me.
import ExampleDoc from '......src/assets/files/exampleDoc.pdf'
<a href={ExampleDoc} download="MyExampleDoc" target='_blank'>
<Button className={classes.navLink}>My Example Doc</Button>
</a>
2 of 8
11
Try this:
const DownloadButton = props => {
const downloadFile = () => {
window.location.href = "https://yoursite.com/src/assets/files/exampleDoc.pdf"
}
return (
<button onClick={downloadFile} />
)
}
export default DownloadButton;
GeeksforGeeks
geeksforgeeks.org › reactjs › how-to-download-pdf-file-in-reactjs
How To Download PDF file in ReactJS? - GeeksforGeeks
import React from "react"; const App = () => { const onButtonClick = () => { const pdfUrl = "Sample.pdf"; const link = document.createElement("a"); link.href = pdfUrl; link.download = "your file name.pdf"; 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;
Published July 23, 2025
Top answer 1 of 3
35
I have found below in their site documentation
import { PDFDownloadLink, Document, Page } from '@react-pdf/renderer'
const MyDoc = () => (
<Document>
<Page>
// My document data
</Page>
</Document>
)
const App = () => (
<div>
<PDFDownloadLink document={<MyDoc />} fileName="somename.pdf">
{({ blob, url, loading, error }) => (loading ? 'Loading document...' : 'Download now!')}
</PDFDownloadLink>
</div>
)
2 of 3
9
hi its working fine for me
import React from 'react';
import logo from './logo.svg';
import './App.css';
import { PDFDownloadLink, Page, Text, View, Document, StyleSheet } from '@react-pdf/renderer';
const styles = StyleSheet.create({
page: {
flexDirection: 'row',
backgroundColor: '#E4E4E4'
},
section: {
margin: 10,
padding: 10,
flexGrow: 1
}
});
const MyDoc = () => (
<Document>
<Page size="A4" style={styles.page}>
<View style={styles.section}>
<Text>Section #1</Text>
</View>
<View style={styles.section}>
<Text>Section #2</Text>
</View>
</Page>
</Document>
);
function App() {
return (
<div className="App">
<PDFDownloadLink document={<MyDoc />} fileName="somename.pdf">
{({ blob, url, loading, error }) => (loading ? 'Loading document...' : 'Download now!')}
</PDFDownloadLink>
</div>
);
}
export default App;
Freddydiengott
freddydiengott.com › blogs › pdf-react
Downloading (and displaying) PDFs in React
November 20, 2024 - This is important, because by using ... issue. In our downloadPDF event handler, we do a few things: we use react-pdf's pdf function to render our pdf and convert it into a blob with the toBlob() method....
React PDF Viewer
react-pdf-viewer.dev › examples › use-the-default-download-button
Use the default download button - React PDF Viewer
The following example uses the default download button `<DownloadButton />` provided by the Get File plugin.
Syncfusion
ej2.syncfusion.com › documentation › document processing › pdf › pdf viewer › react › download
Download in React Pdfviewer component | Syncfusion
import * as ReactDOM from 'react-dom'; import * as React from 'react'; import './index.css'; import { PdfViewerComponent, Toolbar, Magnification, Navigation, LinkAnnotation, BookmarkView, ThumbnailView, Print, TextSelection, Annotation, TextSearch, Inject } from '@syncfusion/ej2-react-pdfviewer'; let pdfviewer; function App() { function downloadClicked() { var viewer = document.getElementById('container').ej2_instances[0]; viewer.download(); } return (<div> <div className='control-section'> {/* Render the PDF Viewer */} <button onClick={downloadClicked}>Download</button> <PdfViewerComponent re
Top answer 1 of 2
2
I have created a workaroud using file-saver package
my button:
<button onClick={this.submitForm}>Send</button>
and submitForm method:
submitForm = async (event) => {
event.preventDefault(); // prevent page reload
const blob = await pdf(
<MyDoc />
).toBlob();
saveAs(blob, 'wycena.pdf');
}
2 of 2
1
Just replace the 'Download now' string with
<Button> Print </Button>
and it will work perfectly fine for you.
Top answer 1 of 2
1
just checked on my code, seems you need to place a tag inside the button not above:
<button>
<a
href='/pathToPdf.pdf'
target='_blank'
rel='noopener noreferrer'
>
RESUME
</a>
</button>
2 of 2
0
As i see remove {} from href attribute because it's not JavaScript code then in download meta-attribute you should put your file extenstion ".pdf"
<button>
<a
href="./public/file.pdf"
download="mycv.pdf">
Save
</a>
</button>
JavaScript in Plain English
javascript.plainenglish.io › how-to-download-files-on-button-click-reactjs-f7257e55a26b
How to Create a Download Button Using React | JavaScript in Plain English
July 16, 2024 - When I was building 100 Ideas, I wanted to give users an option to export their content in a text file. It was super simple. This post is an attempt to share that knowledge and log it for the future. Assuming you already know the basics of React, let's begin. <div className="btnDiv"> <button id="downloadBtn" value="download">Download</button> </div>
Mdfaisal
mdfaisal.com › blog › download-html-as-a-pdf-in-react
Download HTML as a PDF in React - Blog | Mohammad Faisal
February 18, 2021 - import React from 'react'; import html2canvas from "html2canvas"; import { jsPDF } from "jspdf"; const GenericPdfDownloader = ({rootElementId , downloadFileName}) => { const downloadPdfDocument = () => { const input = document.getElementById(rootElementId); html2canvas(input) .then((canvas) => { const imgData = canvas.toDataURL('image/png'); const pdf = new jsPDF(); pdf.addImage(imgData, 'JPEG', 0, 0); pdf.save(`${downloadFileName}.pdf`); }) } return <button onClick={downloadPdfDocument}>Download Pdf</button> } export default GenericPdfDownloader; Now you can just place this component anywhere
Stackademic
stackademic.com › blog › downloading-a-react-component-as-pdf-12021aaf0ccc
Downloading React Components as PDF Files | Stackademic
December 6, 2023 - You probably will not need this and things can break, // so use with caution. overrides: { // see https://artskydj.github.io/jsPDF/docs/jsPDF.html for more options pdf: { compress: true }, // see https://html2canvas.hertzen.com/configuration for more options canvas: { useCORS: true } }, }; // you can use a function to return the target element besides using React refs const getTargetElement = () => document.getElementById('content-id'); const Component = () => { return ( <div> <button onClick={() => generatePDF(getTargetElement, options)}> Generate PDF </button> <div id="content-id"> Content to be generated to PDF </div> </div> ); } Here is a demo provided in the react-to-pdf documentation via Code Sandbox. I would also like to mention that the code blocks above are also from the documentation. Like me, you may need to let your users download PDF files generated from components.