Laravel Pdf File Download from Ajax Request (Laravel 5)

Unable to download file as PDF from browser when using LARAVEL AJAX

solution here:

   success: function(blob, status, xhr) {
var ieEDGE = navigator.userAgent.match('/Edge/g');
var ie = navigator.userAgent.match('/.NET/g'); // IE 11+
var oldIE = navigator.userAgent.match('/MSIE/g');

if (ie || oldIE || ieEDGE) {
window.navigator.msSaveBlob(blob, fileName);
} else {
var fileURL = URL.createObjectURL(blob);
document.write('<iframe src="' + fileURL + '" frameborder="0" style="border:0; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%;" allowfullscreen></iframe>');
}

DomPDF does not download file for AJAX request on Laravel

Since you're making the request using AJAX, you need a way to have the browser load the resource.

There are ways to go but not limited to the following:

  • Return the resource URL in the response to the AJAX request then make a separate browser request to load the resource.

  • Make a blob of the resource response to the AJAX request then load it from the blob's URL.

    This is shown in the example below.

function downloadFile(response) {
var blob = new Blob([response], {type: 'application/pdf'})
var url = URL.createObjectURL(blob);
location.assign(url);
}

$.ajax({
url: "{{ route('screen.pdfticket') }}",
method: 'POST',
data: {
id: $('#screenid').val()
}
})
.done(downloadFile);

How to open pdf from ajax response

Don't do this way,
I have a better option for you!

Don't send information via ajax. use window.location.href

example code:

$('body').on('click', '.printInvoice', function () {
var purchase_id = $(this).data("id");
window.location.href = "/purchases/print" + '/' + purchase_id;
});

Don't forget to die at end of the route.

Not able to download Pdf/Excel with JQuery AJAX get request with some data sent with get request

This works

$.ajax({
url: "http://localhost:8000/get_excel",
type: "POST",
data: {data : list_of_tables},
dataType: 'binary',
xhrFields: {
'responseType' : 'blob'
},
headers: {
'Accept': "application/json",
'Authorization': 'Bearer ' + getCookie(ACCESS_TOKEN),
},
success: function (data, textStatus, request) {
alert("success in fetching data");
let url = URL.createObjectURL(data);
let $a = $('<a />', {
'href': url,
'download': 'document.xlsx',
'text': "click"
}).hide().appendTo("body")[0].click();

// URL.revokeObjectURL(url);

},
error: function (XMLHttpRequest, data) {
console.log("error in show_history");
console.log(data);
console.log(XMLHttpRequest.status);
console.log(XMLHttpRequest.responseJSON);
}
});



Related Topics



Leave a reply



Submit