This code saves file dirctly, I want to preview my pdf file Note: I have content which is plain/text so first I have to create pdf file then open or preview
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
Embed pdf using object tag
var objectPDF = document.createElement("object");
objectPDF.data = 'data:application/pdf;base64,' +text;
objectPDF.type = 'application/pdf';
document.body.appendChild(objectPDF);
You can also use iframe
As mentioned in comment if you just want to show your file in a new window, remove doanload
attribute and add target="_blank"
. Sample here
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:application/pdf;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('target','_blank');
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
To make it further more simpler use window.open()
instead of a temp link. Sample here
function viewFile(filename, text) {
window.open('data:application/pdf;charset=utf-8,' + encodeURIComponent(text));
}
try this
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
Use embed element for holding pdf files
var pdf = "pdf file link here";
$('<div/>')
.html('<embed class="fullFrame" ' +
'type="application/pdf" ' +
'src="' + pdf + '"></embed>')
.dialog({
autoOpen: true,
modal: true,
height: 400,
width: 400,
title: "Resume"
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.