hey @geeky01adarsh, I believe you just don't want to display it right? In that case, you could use the @react-pdf/renderers pdf method and the file-saver package to achieve this.
import React from 'react'; import { saveAs } from 'file-saver'; import { pdf } from '@react-pdf/renderer'; import YourDocument from './YourDocument'; const DownloadButton = () => { const downloadPdf = async () => { const fileName = 'test.pdf'; const blob = await pdf(<YourDocument />).toBlob(); saveAs(blob, fileName); }; return <button onClick={downloadPdf}>Download PDFbutton>; }; export default DownloadButton;
this will convert your pdf component into a Blob and then saves it using saveAs method from file-saver
» npm install react-pdf
Videos
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>
)
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;
Download File in React.js To download a file with React.js, we can add the download attribute to an anchor element.
For instance, we can write:
import React from "react";
export default function App() {
return (
<div>
<a href="https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"
download
>
Click to download
</a>
</div>
);
}
We just add the download prop to do the download.
If we don’t want to use an anchor element, we can also use the file-saver package to download our file.
To install it, we run:
npm i file-saver
Then we can call the saveAs function from the package by writing:
import React from "react";
import { saveAs } from "file-saver";
export default function App() {
const saveFile = () => { saveAs("https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf",
"example.pdf"
);
};
return (
<div>
<button onClick={saveFile}>download</button>
</div>
);
}
The first argument is the URL to download and the 2nd argument is the file name of the downloaded file.
I just had the same issue myself, literally a few minutes ago, and I found two solutions to this: Download the file or open it in a new tab and allow the user to decide if they want to download it or not.
A quick note: the file I have is located in the /public/data folder of my project, since I treat it as a static asset.
- Download the file:
<a
href={process.env.PUBLIC_URL + 'data/random_file.pdf'}
download='random_file.pdf'
>
<button>Download Random File</button>
</a>
- Open the file in a new tab for the user to view or download.
<a
href={process.env.PUBLIC_URL + 'data/random_file.pdf'}
target='_blank'
rel='noopener noreferrer'
>
<button>Open Random File</button>
</a>
Also, it might be a bit late to mention but I am extremely new to web development and React so please take this info with a grain of salt. And I hope this solution works for you too, or at least points you in the right direction.
» npm install react-to-pdf
How to download the latest official React documentation as a single pdf file?
I know how to print to pdf but that's one page at a time.
I know there's code in the docs and sometimes in tabs. This will get lost in the pdf. I don't care.
I know I can develop a crawler and the crawler can save each page but that's work for me.
I want a ready-made solution. Perhaps someone has already done this work.
I tried squizz's way, but the problem I'm finding is that if you click the link more than once or twice, it will revert to going back to going to the last route.
You can make a folder in src to hold your pdf's, and link to them normally, ex:
import pdf from '../files/myfile.pdf'
render () {
<a href={pdf}>Click here for my pdf</a>
}
The only problem is, you will get a hash appended to your file, so it will come out as myfile.d2e24234.pdf or something. I think it has to do with file-loader...currently trying to figure it out.
EDIT
The answer if you don't want to use src is to delete the service worker from the create-react-app project. For some reason it affects react-router's handling of server routes.
I made a github issue here to read about it: https://github.com/facebookincubator/create-react-app/issues/3608
I met the same issue, I think it's because of the way CRA handles queries : I ended up putting my PDF files in the public folder, and link to them using :
{process.env.PUBLIC_URL + '/myfile.pdf'}
as src to my tags.
Not the best way I guess, but works fine enough...
sip mighty fearless gaze school scary one shelter profit dinner
This post was mass deleted and anonymized with Redact