How can I get the progress of dompdf. I have a lot of html that is being converted into pdf using dompdf. Because the HTML is a lot it takes a while so I want to know if its possible to get the progress and display it to the user.
Edit:
According to suggestion I have decided to use a spinner. But I need to know when the file is received by the user. I'm using this code to generate the pdf which is copied from here:
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream();
The pdf is generated when a user presses a link that executes this code. The pdf downloads and shows in the downloads section.
Edit 2: Now I am trying to handle the download with ajax. I am receiving the data and I can see it when I do console.log() but how do I get the file to be saved in the users downloads?? Thanks
Dompdf does not currently support a means of reporting its progress. Your best bet in the meantime might be to use a spinner to let the user know that PDF generation is ongoing.
If you're sending the PDF as an attachment you can't really know when the user receives it since there's no event triggered. On the other hand, using pure AJAX is harder still and browser-dependent since it requires certain HTML5 functionality.
You can use a mix of the two methods by using AJAX to trigger PDF rendering, saving the dompdf-generated PDF to the server's file system, returning the filename to the client, and finally redirecting to the file in the AJAX callback.
Assuming jQuery + blockUI (for the lazy dev), here's a simplified example.
First the client code:
<script>
$('.pdf').click( function {evt} (
$.blockUI();
$.get('generate_pdf.php', function (data) {
$.unblockUI();
location.href = data;
});
));
</script>
<button class="pdf">Generate PDF</button>
and the server:
<?php
// require dompdf autoloader, then...
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');
$dompdf->render();
$filename = sprintf('pdf_%s.pdf', date('ymd'));
file_put_contents($filename, $dompdf->output());
echo $filename;
?>
You'll need to craft the code to meet your needs. And of course, you can make it more robust by building in more error handling. Plus you'd want some kind of clean-up to remove old files.
Of course, if you really want to do this all using AJAX it's certainly possible and there are a number of relevant questions already.