1

I'm generating a dynamic table with DOMPDF. When a form is submitted, $_POST variables are sent to test.php:

<?php
session_start();

$theTemplate = 'test.php';

function renderToPDF($templateFile)
{
  require_once("./dompdf/dompdf_config.inc.php");
  ob_start();
  include $templateFile;
  $contents = ob_get_clean(); 

  if ($contents !== false)
  {
    $dompdf = new DOMPDF();
    $dompdf->load_html($contents);
    $dompdf->render();
    $dompdf->stream("Custom_Data.pdf");

 }
}

renderToPDF($theTemplate);
?>

tabletest.php has a table with dynamic values, including some $_POST from the form and some variables from a MYSQL table. When the form is submitted, the PDF is generated successfully, but is there any way to do a redirect to another page after the PDF is downloaded? I didn't find a conclusive answer to similar questions (Create PDF with DOMPDF and Redirect and How to generate pdf file using dompdf and redirect to another page)...

Community
  • 1
  • 1
grownupteeth
  • 131
  • 2
  • 12

2 Answers2

3

Short answer: you can't.

When you call $dompdf->stream() the content of the PDF is immediately sent to the browser and script execution ended. At this point you can no longer redirect the browser to another page.

What you might try is sending the user to the landing page first then redirecting the user to the PDF render from there. You can do that using either an HTML meta header or JavaScript. If you specify that the PDF should be sent as an attachment the user will receive the download dialog after landing on the intermediate page.

This is the technique sites use for downloads when they display a page with the message "Your download will start in ...".

BrianS
  • 13,284
  • 15
  • 62
  • 125
1

Posting to hopefully help someone in the future.

The way I got around this was setting a cookie after the stream event. In my case, I'm downloading a label after button click and my event listeners were getting removed after the download completed.

Its a very particular use-case, a simpler method would be the answer given here about using the target attribute on a link tag: https://stackoverflow.com/a/11315861/4484799 unfortunately I couldn't use this for the particular use-case.

My solution was:

<?php

$dompdf->stream($filename);
// Set cookie after stream. From what I noticed the cookie doesn't
// get set until the stream is done 
setcookie('my_cookie', 'download complete', time() + 86400, "/");

?>

In Javascript I'm checking if this cookie exists (using this tried and tested library for js cookie handling), if it does, then i refresh the page:

(Jquery)

    $( window ).load(function() {
        
        function checkLabelDownloadCookie(){
            const cookie =  Cookies.get('my_cookie');
            if( cookie  ){
                Cookies.remove('my_cookie'); //remove cookie
                location.reload(); //reload page
            }else{
                console.log("PDF download cookie not found yet");
            }
        }
        
        function monitorLabelDownloadCookie(){
          // check if the cookie exists every 500ms
            setInterval( checkLabelDownloadCookie, 500 );
        }

        monitorLabelDownloadCookie();

    });

This was my solution, of course you'd maybe add some more conditions to the code for your use-case

Uriahs Victor
  • 1,049
  • 14
  • 32