The problem here is not Guzzle, that part you already have covert, you can simply do a
$pdf = $response->getBody()->getContents();
To get the pdf in PHP.
The problem is now: An ajax request cannot simply download something.
So you have 2 options:
- Change the ajax request into a regular POST request (the user wont really notice as you are not going to a different page, it will simply trigger the download)
- Change your JS code
If you stick with the ajax request, the easiest way would be a bit of DOM manipulation to make the browser think you're doing a regular download:
$.ajax({
type: 'POST',
dataType: 'json',
url: $(this).attr('data-url'),
data: {order: $(this).attr('data-order')},
success: function(data) {
var blob=new Blob([data]);
var link=document.createElement('a');
link.href=window.URL.createObjectURL(blob);
link.download="my-downloaded-file.pdf";
link.click();
}
});
It will take the response from your server, create a Blob from it, create an invisible element to link to that blob, and click it to start the download.