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/renderer
Videos
In case you face the same problem, I found something that help. Replace PDFViewer for the code below:
<PDFDownloadLink document={<FeeAcceptance />} fileName="fee_acceptance.pdf">
{({ blob, url, loading, error }) => (loading ? 'Loading document...' : 'Download now!')}
</PDFDownloadLink>
https://react-pdf.org/components
We can also use ReactPDF.pdf. here's a small code sample of an async function that returns a url (or even blob if the case requires !). We can use this url to download our PDF.
renderUrl = async() => new Promise((resolve, reject) => {
const blob = await ReactPDF.pdf(<FeeAcceptance member_detail={'test'} />).toBlob()
const url = URL.createObjectURL(blob)
if(url && url.length>0){
resolve(url)
}
}
).then(res => res).catch(err => console.log(err))
To Download the generated url we can use the download attribute of a tag. Here's an example
let sampleTab = window.open()
if(sampleTab) {
renderUrl().then(generatedUrl => {
if( generatedUrl ){
let aTag = document.createElement('a')
aTag.href = generatedUrl
aTag.style = "display: none";
aTag.download = 'FeeAcceptance.pdf'
document.body.appendChild(aTag)
aTag.click()
} // else -> means something went wrong during pdf generation
}).catch(err => console.log(err))
} // else -> means new tab can't be opened ... (some Adblock issue)
» npm install @react-pdf/render
Hello everyone!
I'm currently engaged in a project that involves generating invoices and creating PDFs for them. The backend is built on Django, and the frontend is powered by Next.js. I'm contemplating the best approach for generating PDFs—whether to do it in React on the frontend or in Python on the backend. I came across a package on npm called `@react-pdf/renderer` for frontend PDF generation, is this good?
Any guidance would be highly appreciated!
P.S.: This is my first post!