1

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,203
  • 17
  • 26
  • 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

2 Answers2

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
  • 820
  • 8
  • 19
0

Similar suggestions provided by Silvia also in #28310366 which also includes a reference to https://github.com/alferov/angular-file-saver which itself leverages https://github.com/eligrey/FileSaver.js/

Peter Scott
  • 1,258
  • 12
  • 19
  • Thanks Peter. The file can be named and saved in multiple ways (i.e. see Selva response below), but that's not the issue here. The issue is to make the Google Pdf viewer to use a predefined name for the file instead of the default "download.pdf". – Miki Feb 06 '18 at 15:30