You would need to pass the responseType in your service call
$http.post('/Service-URL', dataTO, {responseType: 'arraybuffer'});
then in the success of your data call this should open up pdf in a new window:-
getDocument()
.success(function(data) {
var file = new Blob([data], { type: 'application/pdf' });
var fileURL = URL.createObjectURL(file);
window.open(fileURL);
})
From this answer :- https://stackoverflow.com/a/21730535/3645957 by https://stackoverflow.com/users/2688545/michael
Answer from aniltilanthe on Stack OverflowYou would need to pass the responseType in your service call
$http.post('/Service-URL', dataTO, {responseType: 'arraybuffer'});
then in the success of your data call this should open up pdf in a new window:-
getDocument()
.success(function(data) {
var file = new Blob([data], { type: 'application/pdf' });
var fileURL = URL.createObjectURL(file);
window.open(fileURL);
})
From this answer :- https://stackoverflow.com/a/21730535/3645957 by https://stackoverflow.com/users/2688545/michael
If anyone still looks for that, here is what I'm doing (and working) :
var pdfAsDataUri = "data:application/pdf;base64,"+byteArray;
window.open(pdfAsDataUri);
Where byteArray is the data you receive. It's maybe not a nice solution (byte array is visible in the URL), but it works...
The string from the response seem to be Base-64 encoded (ref. fiddle). You can decode it using fetch() (or XMLHttpRequest() for older browsers):
fetch("data:application/pdf;base64," + response.data)
.then(function(resp) {return resp.blob()})
.then(function(blob) {
FileSaver.saveAs(blob, 'foo.pdf')
});
If anyone looking for solution with same problem, here is the nice article with cross browser support.
Snippet from article:
const binaryString = window.atob(fileResponseData); const bytes = new Uint8Array(binaryString.length); const mappedData = bytes.map((byte, i) => binaryString.charCodeAt(i)); const blob = new Blob([mappedData], { type: 'application/pdf' }); FileSaver.saveAs(blob, 'foo.pdf')
Found the solution here it is, i was sending byte array from spring controller which is in the form like %PDF-1 %����. So i send base64 encoded string from spring controller and send the base64 encoded string to browser and it works.
javascript code :
var arrrayBuffer = base64ToArrayBuffer(data); //data is the base64 encoded string
function base64ToArrayBuffer(base64) {
var binaryString = window.atob(base64);
var binaryLen = binaryString.length;
var bytes = new Uint8Array(binaryLen);
for (var i = 0; i < binaryLen; i++) {
var ascii = binaryString.charCodeAt(i);
bytes[i] = ascii;
}
return bytes;
}
var blob = new Blob([arrrayBuffer], {type: "application/pdf"});
var link = window.URL.createObjectURL(blob);
window.open(link,'', 'height=650,width=840');
convert byte array to base64 encoded string in spring controller
String encodedString = Base64.getEncoder().encodeToString(bytearrayofpdf);
You can use PDFObject JavaScript utility to view PDF files in your browser. Just create a div with some id and insert the byte array that you get into that div.
PDFObject.embed(<byte array>, "#pdfObjectViewer");
You need to download the PDFObject library and include it in your project from their site for this to work. Or you can use this CDN.
I asked the question long time ago, so I might be wrong in some details.
It turns out that Blob needs array buffers. That's why base64 bytes need to be converted to array buffers first.
Here is the function to do that:
function base64ToArrayBuffer(base64) {
var binaryString = window.atob(base64);
var binaryLen = binaryString.length;
var bytes = new Uint8Array(binaryLen);
for (var i = 0; i < binaryLen; i++) {
var ascii = binaryString.charCodeAt(i);
bytes[i] = ascii;
}
return bytes;
}
Here is my function to save a pdf file:
function saveByteArray(reportName, byte) {
var blob = new Blob([byte], {type: "application/pdf"});
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
var fileName = reportName;
link.download = fileName;
link.click();
};
Here is how to use these two functions together:
var sampleArr = base64ToArrayBuffer(data);
saveByteArray("Sample Report", sampleArr);
You just need to add one extra line and it should work. Your response is byte array from your server application
var bytes = new Uint8Array(resultByte); // pass your byte response to this constructor
var blob=new Blob([bytes], {type: "application/pdf"});// change resultByte to bytes
var link=document.createElement('a');
link.href=window.URL.createObjectURL(blob);
link.download="myFileName.pdf";
link.click();