Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set filename for base64 rendered pdf using object tag?

Tags:

html

pdf

base64

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 ?

like image 871
Avidev9 Avatar asked Sep 15 '13 20:09

Avidev9


1 Answers

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.

like image 159
Robert Avatar answered Sep 20 '22 01:09

Robert