common Posted July 11, 2014 Share Posted July 11, 2014 Hi Everyone, I am struggling with a pdf. On our server I get the pdf as a byte array from the database, which I then build up in a StreamingOutput object and return as a pdf file. This is done using java using JAX-RS service. Now when I open the path/link in my browser(chrome/firefox) the pdf opens in the browser. So this means my backend is working. On my webapp I want to use $.ajax to download the pdf and view it in an Iframe or embed it in an <embed> or <object> tag. I need to do this as the service call is actually under username and password, and we do not create a session between the browser and the server - there is a long explanation for this. I saw the option to use http://[username]:[password]@www.myserver.com/... but this was dropped by most browsers. This is why I am trying ajax. Now in my ajax call I do receive the pdf in the success function as: %PDF-1.4.... This open the browser's pdfviewer correctly and every thing, but the pdf only returns blank pages. The Base64 is a java object that I found on the internet. But I have tried it without the base64 but still now luck. Here is my success function from my ajax call. success: function(data, status, xhr) { var pdfText = Base64.encode(xhr.responseText); var url = "data:application/pdf;base64," + escape(pdfText); var html = '<embed width=100% height=600' + ' type="application/pdf"' + ' src="' + url + '">' + '</embed>'; $("div.inner").html(""); $("div.inner").append(html); }, Can anyone help me? Kind regards Quote Link to comment https://forums.phpfreaks.com/topic/289734-jquery-ajax-data-uri-scheme/ Share on other sites More sharing options...
requinix Posted July 11, 2014 Share Posted July 11, 2014 The Base64 is a java object that I found on the internet.That's right: Java. Java and Javascript are two completely different things. Inline content sucks. Can't you just use a time-limited URL? 1. JS: Do the POST but now you only get a temporary token 2. Java: Store that token in the database along with a reference to the PDF - this is a good opportunity to log views too 3. JS: Construct a specific URL containing that token ("/path/to/service?token=123abc") and embed it 4. Java: Take that token, check that it's still valid, and if so output the PDF Quote Link to comment https://forums.phpfreaks.com/topic/289734-jquery-ajax-data-uri-scheme/#findComment-1484734 Share on other sites More sharing options...
Solution common Posted July 14, 2014 Author Solution Share Posted July 14, 2014 Hi Sorry, meant javascript - The Base64 is a java object that I found on the internet javascript The token is not a bad idea, but got it to work using var xhr = new XMLHttpRequest(); xhr.open(method, url, true); xhr.responseType = 'blob'; xhr.setRequestHeader('Authorization', 'Basic ' + auth); xhr.onload = function(e) { if (this.status === 200) { var blob = new Blob([this.response], {type: this.response.type}); var blobURL = URL.createObjectURL(blob); blobCallBack(blobURL); } }; xhr.send(); blobCallback is a function I provide where I do my inline content: src=blobURL . And that works perfectly. Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/289734-jquery-ajax-data-uri-scheme/#findComment-1484966 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.