Download file without saving it to webserver from AJAX call

I’m submitting a form with ajax and after the form is submitted I need to download a pdf file generated on the server side with the php library mpdf. I don’t want to save the pdf file in the server because after the user donwloads the file is not needed anymore. I’ve searched on google and someone suggest to use a Blob object. I’m also returning a json response to show to the user. Can someone explain if it is possible and how to achieve it? many thanks

this is my javascript code:

  $.ajax({

                    type: form.method,
                    data: $(form).serialize(),
                    dataType: 'JSON',

                    beforeSend: function() {
                        // Show loader
                        $(".loader").show();
                    },


                    success: function(response) {

                        // Hide loader
                        $(".loader").hide();


                        if (response.error_code == '200') {

                            // Add class alert danger to the message
                            $('.alert', $('.horizontal-form')).addClass('alert-success').show();
                            // Show error message
                            $('#error_message').html(response.error_message);
                            // Remove default validation error message from template
                            $('#error_message').get(0).nextSibling.remove();
                            // Remove class alert danger if was left by previous validation
                            $('.alert', $('.horizontal-form')).removeClass('alert-danger');
                            // reset form fields
                            form.reset();
                            // Scroll back to show success message
                            scrollTo(success, -200);

                        }

                    }

                });

and this is my php code

if(isset($_POST['userLetter'])){

                        header("Content-Type", "application/pdf");

                        // Render the view using twig and pass params to it
                        $html=$twig->render('/km-letters/km-letter-new-user.twig', array(
                            // Render user details on twig topbar
                            'prova' => 'pippo'

                        ));

                        $mpdf->WriteHTML($html);


                        return $mpdf->Output('foobar-' . time() . '.pdf','D');


                    }

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.