Skip to content

How to generate a PDF and download it without rendering it? #2352

Answered by coskuncakir
geeky01adarsh asked this question in Q&A
Discussion options

You must be logged in to vote

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 PDF</button>;
};

export default DownloadButton;

this will convert your pdf component into a Blob and then saves it using saveAs m…

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
4 replies
@geeky01adarsh
Comment options

@Venkata2138279
Comment options

@Qodestackr
Comment options

@HetarthJain
Comment options

Answer selected by geeky01adarsh
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants