how to display base64 encoded pdf?
It should work with Chrome you can use
<iframe src="data:base64...">
<object data="data:base64...">
I've faced the same issue with IE: it's impossible to display a pdf with a base64
string.
I had to generate temporary files on the server to display them with IE he only display existing file by using a path.
You still can use JS library to display your pdf like PDF.js
.
Cannot download base64 encoded pdf displayed in chrome browser
Your actual problem is more like that you can not download content supplied via a data:
-URI from a new window. Side note: the spinner can be stopped via close()
-ing the document, but it does not affect the inoperational "Save as".
Two workarounds I found:
- Open data in the current window (the "iframe" button does that, then the document can be downloaded via right-clicking, though its name defaults to "download.pdf" in Chrome). It may be an option with short documents / small images
- It is possible to provide an explicit button for downloading, and in this case a filename can be supplied too. "window(tab)" button does that in the example. While generally it does not look nice that the exact same encoded data is moved around twice, it happens locally in the browser after all
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
let pdf="JVBERi0xLjIgCjkgMCBvYmoKPDwKPj4Kc3RyZWFtCkJULyA5IFRmKFRlc3QpJyBFVAplbmRzdHJlYW0KZW5kb2JqCjQgMCBvYmoKPDwKL1R5cGUgL1BhZ2UKL1BhcmVudCA1IDAgUgovQ29udGVudHMgOSAwIFIKPj4KZW5kb2JqCjUgMCBvYmoKPDwKL0tpZHMgWzQgMCBSIF0KL0NvdW50IDEKL1R5cGUgL1BhZ2VzCi9NZWRpYUJveCBbIDAgMCA5OSA5IF0KPj4KZW5kb2JqCjMgMCBvYmoKPDwKL1BhZ2VzIDUgMCBSCi9UeXBlIC9DYXRhbG9nCj4+CmVuZG9iagp0cmFpbGVyCjw8Ci9Sb290IDMgMCBSCj4+CiUlRU9G";
function doFrame(){
let frm=document.createElement("iframe");
frm.style="border-color:black";
frm.src="data:application/pdf;base64,"+pdf;
document.body.appendChild(frm);
}
function doWindow(){
let wnd=open("",Date.now());
let doc=wnd.document.open();
doc.write("<a href='data:application/pdf;base64,"+pdf+"' download='small.pdf'>Download</a><br>");
doc.write("Object<br><object type='application/pdf' data='data:application/pdf;base64,"+pdf+"'></object><br>");
doc.write("Embed<br><embed type='application/pdf' src='data:application/pdf;base64,"+pdf+"'><br>");
doc.write("IFrame<br><iframe src='data:application/pdf;base64,"+pdf+"'/>");
doc.close();
}
</script>
</head>
<body>
<button onclick="doWindow()">window(tab)</button><br>
<button onclick="doFrame()">iframe</button><br>
</body>
</html>
It's not a runnable snippet as the security sandbox here (on StackOverflow) blocks both kind of attempts.
The "window(tab)" variant opens the pdf as object
, embed
and iframe
too, I can confirm that right-click+"Save as" in Chrome does not do anything on them. Download button works (also, in Chrome at least).
Related Topics
How to Compare Two Objects and Get Key-Value Pairs of Their Differences
Check If Value Select2 Is Null
Can We Insert JavaScript into Any Webpage Loaded in the Browser
How to Remove a File from the Filelist
Convert an Object With Multiple Values Per Key into Array of Objects in Js
Domexception: Play() Can Only Be Initiated by a User Gesture
Angular 6 - Navigation to Child Route Refreshes Whole Page
Deselect All Options in Multiple Select With 1 Option
Usestate Hook Not Updating a Value
Javascript Check If Character Is a Vowel
How to Pass Value to a Onclick Function in (Jade)Pug
React.Js - Input Losing Focus When Rerendering
While Looping a Jquery Ajax Call
Exclude Weekends in JavaScript Date Calculation
How to Use Lodash to Remove Keys from a Json Object
Ask for Geolocation Permission Again If It Was Denied
How to Hide Code from Cells in Ipython Notebook Visualized With Nbviewer
Is There Any Workaround (Or Hack) to Make the Audio Autoplay on Chrome, Firefox and Safari