32

Well I am using the following code to render a PDF that is returned from a webservice as base64 string.

<html>
  <object data="data:application/pdf;base64,{!pdfStampingResponse.pdfBase64}" type="application/pdf" width="100%" class="internal">
    <param name="view" value="fitH" />            
  </object>
</html>

This works well but I want to set the download filename when user tries to save the file. Somehow I am unable to find a working solution. Any idea how to do this ?

alex
  • 5,467
  • 4
  • 33
  • 43
Avidev9
  • 601
  • 1
  • 6
  • 15
  • 1
    I know this is very old but, just in case Avidev9 sees it, did you ever find a solution to this? Or can anyone else help? I am currently facing the same problem. The solution by Stu below doesn't work for me. Thanks. – giles123 Feb 08 '17 at 16:00
  • You need to set the Content Disposition header as shown in this answer to set the file download name. http://stackoverflow.com/a/1741508/728610 – Arvind Sridharan Mar 02 '17 at 12:33
  • Does anyone know how to set the title on the header when i embed a pdf? – Arvind Sridharan Mar 02 '17 at 13:32

4 Answers4

4

please try this

const FILE_NAME = 'myfile.pdf';
const file_header = ';headers=filename';

fetch('https:your-url/myfile.pdf?dl=0').then(r => r.blob())
.then(blob=>{
  const f = new FileReader();
  f.onload = () => myPdfViewer.src = f.result.replace(';', file_header + encodeURIComponent(FILE_NAME) + ';');
  f.readAsDataURL(blob);
  });

Then insert id myPdfViewer to an iframe. I hope it can help.

Robert
  • 71
  • 3
0

You can use the following code syntax

<object data="data/test.pdf" type="application/pdf" width="300" height="200">
  alt : <a href="data/test.pdf">test.pdf</a>
</object>

Found this here.

Hope it helps.

Stu
  • 157
  • 3
  • 18
0

Use download="yourfilename" in anchor tag.

The download attribute specifies that the target will be downloaded when a user clicks on the hyperlink.

PS:This attribute is only used if the href attribute is set.

Abhi Chavda
  • 81
  • 1
  • 6
0

Is there any way around this so that I can control the name?

No. You cannot control the name of a file stored at user local filesystem.

See here: https://stackoverflow.com/a/44061387/9913319

Rufat
  • 536
  • 1
  • 8
  • 25