如何用base64 PDF内容打开新窗口?

时间:2015-03-30 08:06:55

标签: javascript jquery base64

我想打开新窗口(使用jQuery)并从base64内容显示PDF。当我做正常链接时:

<a href=\"data:application/pdf;base64,'.$answer->shipping_label_content.'\" target=\"blank\">Show PDF</a>

一切都很好。但是我想用这个内容自动打开新窗口,我不知道如何: - /

var window = window.open();
var html = "data:application/pdf;base64,'.$answer->shipping_label_content.'";

$(window.document.body).html(html);

1 个答案:

答案 0 :(得分:6)

您可以生成临时anchor并以编程方式单击。

//this trick will generate a temp <a /> tag
var link = document.createElement("a");
link.href = "data:application/pdf;base64,'.$answer->shipping_label_content.'";

//Set properties as you wise
link.download = "SomeName";
link.target = "blank";

//this part will append the anchor tag and remove it after automatic click
document.body.appendChild(link);
link.click();
document.body.removeChild(link);