2

On my webpage, I set my pdf as follow:

<object data="myURL" type="application/pdf" width="300" height="200"></object>

where the URL is created with javascipt from a blob like this:

var myURL = URL.createObjectURL(blob);

The pdf gets displayed but when I click on the save button on the bottom right corner (with chrome), I get a pre-defined name (probably the URL name created with javascript)...

How can I change that name?

Nate
  • 7,606
  • 23
  • 72
  • 124
  • @tremor That question is about a link not an object. But I found another question that is about objects. The solution (work-around) is to add an invisible link and let that do the work. – GolezTrol Jun 07 '15 at 17:59
  • which uses Filesaver.js like I initially suggested... go figure. – tremor Jun 07 '15 at 18:01
  • This might help https://stackoverflow.com/a/41947861/4997994 – oomer Apr 30 '22 at 12:39

2 Answers2

0

As far as I know the ability to customize filenames in this scenario is browser dependant. The way I've been able to implement it is using code from here: https://github.com/eligrey/

The blob (https://github.com/eligrey/Blob.js) library implements blob in browsers that do not natively support it (helpful) and

The filesaver (https://github.com/eligrey/FileSaver.js) library is what I use in one of my webapps and it makes saving a blob with a filename pretty simple.. it's as easy as this:

var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
saveAs(blob, "hello world.txt");

If you want to make your own implementation of it the code is all right there. Older browsers may not be able to use filenames but Chrome should be perfectly acceptable. This solution is more javascript and creates the blob functionally rather than with an HTML tag as you have in your question.

tremor
  • 3,068
  • 19
  • 37
  • I know how to save the file using FileSaver but it's not the subject of the question! – Nate Jun 07 '15 at 17:49
  • The first sentence of my answer is on subject, which is i dont think so.. I then offer you a working solution as an alternative. – tremor Jun 07 '15 at 17:53
  • I appreciate but it's not what I'm looking for... Thanks – Nate Jun 07 '15 at 17:57
0

You could add a download='filename' attribute to your embedded pdf link...

<object data="myURL" type="application/pdf" download="FileName" width="300" height="200"></object>

in jQuery you could change the attribute on dom ready, with your required filename.. eg.

$(document).ready(function(){
   $('object[data=myURL]').attr('download', 'newFilename.pdf');
});

source of the answer

Community
  • 1
  • 1
mk117
  • 753
  • 2
  • 13
  • 26