jquery - How to build PDF file from binary string returned from a web-service using javascript -
i trying build pdf file out of binary stream receive response ajax request.
via xmlhttprequest receive following data:
%pdf-1.4.... ..... ....hole data representing file .... %% eof what tried far, embed data via data:uri.
now, there's nothing wrong it. works fine. unfortunately not work in ie9, , ff.
possible reason may ff , ie9 have there problems usage of data-uri.
now, i'm looking solution works browsers.
here's code:
// responsetext encoding pdftext=$.base64.decode($.trim(pdftext)); // pdftext contains %pdf-1.4 ...... data...... %%eof var winlogicalname = "detailpdf"; var winparams = 'dependent=yes,locationbar=no,scrollbars=yes,menubar=yes,'+ 'resizable,screenx=50,screeny=50,width=850,height=1050'; var htmltext = '<embed width=100% height=100%' + ' type="application/pdf"' + ' src="data:application/pdf,' + escape(pdftext) + '"></embed>'; // open pdf in new browser window var detailwindow = window.open ("", winlogicalname, winparams); detailwindow.document.write (htmltext); detailwindow.document.close (); as said .... works fine opera , chrome (safari not tested). using ie or ff bring blank new window.
is there solution building pdf file on file system in order let user download it?
i need solution works in browsers @ least in ie, ff, opera, chrome , safari.
i have no permission edit web-service implementation. had solution @ client-side.
any ideas???
is there solution building pdf file on file system in order let user download it?
try setting responsetype of xmlhttprequest blob , substituting download attribute @ a element window.open allow download of response xmlhttprequest .pdf file
var request = new xmlhttprequest(); request.open("get", "/path/to/pdf", true); request.responsetype = "blob"; request.onload = function (e) { if (this.status === 200) { // `blob` response console.log(this.response); // create `objecturl` of `this.response` : `.pdf` `blob` var file = window.url.createobjecturl(this.response); var = document.createelement("a"); a.href = file; a.download = this.response.name || "detailpdf"; document.body.appendchild(a); a.click(); // remove `a` following `save as` dialog, // `window` regains `focus` window.onfocus = function () { document.body.removechild(a) } }; }; request.send();
Comments
Post a Comment