Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How open new window with base64 PDF content?

I want open new window (with jQuery) and show PDF from base64 content. When I make normal link:

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

It's all good. But I want automatic open new window with this content and I don't know how :-/

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

$(window.document.body).html(html);
like image 735
Kuba Żukowski Avatar asked Feb 11 '23 13:02

Kuba Żukowski


1 Answers

You can generate a temp anchor and click programmatically.

//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);
like image 100
Satpal Avatar answered Feb 13 '23 03:02

Satpal