Render and download a pdf using react-pdf

613 Views Asked by At

I have a React component which is responsible for rendering a pdf from a URL. I want to be able to download this PDF on click of a button.

I am not using react-pdf/renderer as my understanding is that it is used to generate a pdf in the browser and display it whereas I already have the PDF. I know I can download the pdf directly from the URL but the reason I do not want to do so is because I am modifying the pdf in the browser and I want to download the modified pdf. Is there a way to do so.

1

There are 1 best solutions below

0
On

You could try this:

const download = (url: string) => {
  const a = document.createElement("a");
  a.href = url;
  a.download = "Document"; // file name
  document.body.appendChild(a);
  a.click();
  document.body.removeChild(a);
}
<button onClick={()=> download(url)}>Download</button>