0

I'm creating dynamically a pdf file and sending it to the client:

private function createPdf($foo)
{
    //Delete pdf file if it exists
    if(file_exists('path_to_directory' . $foo->getId() . '.pdf'))
        unlink('path_to_directory' . $foo->getId() . '.pdf');

    //Create new pdf file
    $this->get('knp_snappy.pdf')->generateFromHtml(
        $this->renderView(
            'AcmeFooBundle:Default:pdfTemplate.html.twig', array(
                'foo' => $foo)
        ),
        'path_to_directory' . $foo->getId() . '.pdf'
    );

    // open the file in a binary mode
    $fp = fopen('path_to_directory' . $foo->getId() . '.pdf', 'rb');
    header("Content-Type: application/pdf");
    header('Content-Disposition: attachment; filename="foo.pdf"');

    // dump the picture and stop the script
    fpassthru($fp);
    exit;
}

This function works right, but I'd want to continue execution after sending the file to the client to render a "success" template and send it to the client. I've tried things like removing exit; and this: Continue execution after calling php passthru() function, but no success. Any idea?

Community
  • 1
  • 1
Manolo
  • 24,020
  • 20
  • 85
  • 130

1 Answers1

1

Achieved it adding a Javascript event to the file generation button:

Pure JS:

<script>
    window.onload = function() {

        function redirectToMeeting() {
            window.setTimeout(function() {
                window.location.href="url_to_go";
            }, 2000);

        }
        if(document.getElementsByClassName('create-file-button')) {
            document.getElementsByClassName('create-file-button')[0].addEventListener('click', redirectToMeeting);
        }
    };
</script>

Then, when the button is clicked, the new file is created and sent to the client, and then (after 2 seconds) the client is redirected to the url_to_go.

JQuery:

    $(function(){
        $('#create-file-button').submit(function(ev) {
            $("#continue").show();
        });
    });

When the form is submitted, a new button (#continue) will be shown which lets the user go to the next page (url_to_go).

Manolo
  • 24,020
  • 20
  • 85
  • 130