How to generate a PDF and download it without rendering it? #2352
-
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 with React? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
hey @geeky01adarsh, I believe you just don't want to display it right? In that case, you could use the 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 PDF</button>;
};
export default DownloadButton; this will convert your pdf component into a Blob and then saves it using saveAs method from file-saver |
Beta Was this translation helpful? Give feedback.
-
Can I also like Generate PDF client side while triggering a server action to get the link of a resource? (Nextjs that is). |
Beta Was this translation helpful? Give feedback.
hey @geeky01adarsh, I believe you just don't want to display it right? In that case, you could use the
@react-pdf/renderer
s pdf method and thefile-saver
package to achieve this.this will convert your pdf component into a Blob and then saves it using saveAs m…