Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
105 views
in Technique[技术] by (71.8m points)

javascript - Display pdf from base64 data

I am trying to make preview from base64 data . Here it works for any base64 image but if I use base64 pdf then it shows plugin is not supported . Here is my code . There is a post which closely similar but did not meet up my expectation.

if(response.responseCode == 1)
{
  $("#image-info-content").html('<object data="'+response.data+'"></object>');
  $('#image-modal').modal();
}

I also tried with type="application/pdf" . Still it's not working. Though I don't want this because there might be image or pdf file. Thanks in advance

response.data like here data:application/pdf;base64,JVBERi0xLjMKJeLjz9MKMSAwIG9iago8.......

question from:https://stackoverflow.com/questions/65859393/display-pdf-from-base64-data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With this way we can open the pdf in a new tab. i found this in somewhere, but i don't remember exactly where

var objbuilder = '';
objbuilder += ('<object width="100%" height="100%" 
data="data:application/pdf;base64,');
objbuilder += (myBase64string);
objbuilder += ('" type="application/pdf" class="internal">');
objbuilder += ('<embed src="data:application/pdf;base64,');
objbuilder += (myBase64string);
objbuilder += ('" type="application/pdf"  />');
objbuilder += ('</object>');

var win = window.open("#","_blank");
var title = "my tab title";
win.document.write('<html><title>'+ title +'</title><body style="margin-top: 
0px; margin-left: 0px; margin-right: 0px; margin-bottom: 0px;">');
win.document.write(objbuilder);
win.document.write('</body></html>');
layer = jQuery(win.document);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...