4

My angular app has an iframe that renders a PDF:

<iframe ng-src="{{PCtrl.docSrc}}" type="application/pdf" ...></iframe>

docSrc is generated as a BASE64 encoded string, something like:

"data:application/pdf;base64,JVBERi0xLjMKMyA..."

Chrome renders the embedded PDF just fine. The PDF can be downloaded clicking on download, and the user will be presented with a "save as" dialog. The default filename given by the Chrome PDF viewer is "download.pdf", how do I change it?

Miki
  • 1,625
  • 21
  • 29
  • 1
    Already [asked here](https://stackoverflow.com/questions/44061354/set-the-default-save-as-name-for-a-an-embed-or-iframe-that-uses-a-blob/44061918#44061918). No real solution, except running your own pdf viewer. – Kaiido Jan 30 '18 at 04:08
  • try this answer https://stackoverflow.com/questions/40507464/how-can-i-set-the-filename-for-base64-pdf-in-iframe – MichaelEvanchik Feb 05 '18 at 15:52
  • Thanks Kaido/Michael but there is nothing in those answers that help me to rename the file passed to the Google Pdf viewer, so it can be saved with a custom name when downloaded. – Miki Feb 07 '18 at 15:46

1 Answers1

5

window.onload = () => {
  let blob = new Blob(["file"], {
    type: "text/plain"
  });
  let url = URL.createObjectURL(blob);
  let a = document.createElement("a");
  a.href = url;
  a.download = "file.txt";
  document.body.appendChild(a);
  console.log(url);
  a.click();
}

may be this will help you

Selva Ganapathi
  • 982
  • 10
  • 21