» npm install @react-pdf/renderer
Videos
» npm install @react-pdf/render
» npm install react-pdf
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)
If you goal is just to view the pdf in your application, the easiest way is using the object tag in HTML. You don't need to import any libraries and works most of the browsers. But this is lack of customization and styles.
<object data="http://africau.edu/images/default/sample.pdf" type="application/pdf" width="100%" height="100%">
<p>Alternative text - include a link <a href="http://africau.edu/images/default/sample.pdf">to the PDF!</a></p>
</object>
it looks like you're passing the PDF data directly to the <ReactPDF> component as the value of the file prop. But according to the docs you need to use an object containing a data property:
<ReactPDF
file={{
data: renderPdf
}}
/>
it seems you can also set file to an object containing a url property, to let ReactPDF fetch the pdf from your backend by itself, if that's easier:
<ReactPDF
file={{
url: 'http://www.example.com/sample.pdf'
}}
/>