I have a PHP file where I create a PDF. I call to this PHP using AJAX and give some visual feedback (I show and then hide a spinner), because I will create a 500-pages-PDF and this process is a little bit slow.
To create the PDF I use PDFMerger (because I need to merge a few PDFs). And to call this PHP I have a button which has a JavaScript function associated.
The question is, How I can download the file using AJAX. Because if I don't use AJAX and I use the PHP, the file will be downloaded but if I use AJAX instead PHP I will have all the 'PDF-data' on success (...06�P��endstreamendobj965 0 obj<</Filter /FlateDecode /Type /XObject/Subtype /Form/FormType 1/BBox [0.00 0.00 595.28 841.89]/Resources <</ProcSet [/PDF /Text /ImageB /ImageC /ImageI ]/Font <</F1 960 0 R/F2 961 0 R>>/XObject <<>>/Properties...).
My question is not exactly like how to download a file by AJAX. I'm trying to find out how to combine PDFMerger and AJAX to get the PDF.
JavaScript
showSpinner();
$.ajax({
    url: 'downloadPDF.php',
    success: function (data) {
        //And now I want to download the PDF
    },
    complete: function () {
        hideSpinner();
    }
});
PHP (downloadPDF.php)
$pdf = new PDFMerger();
foreach ($arrayPDFs as $url) {
   $pdf->addPDF($url, 'all');
}
$pdf->merge('download');
 
     
    