Stop a large PDF report from timing out

We’ve written an app for tracking the membership of a volunteer youth group. One of the reports is a “parent permission form” for the kids to get signed to attend outside events - rather than fill it out each time it pulls at the information from the database for the parents to read and sign. It uses CakePDF and the dompdf engine.

The report exports fine for a single member or a small number, but if I try to run the report for a whole group (say 30 members) the report times out.

I’m looking for ideas on how to get around this, for example running the report in a batch etc. Has anyone had a similar problem before?

Actually think I’ve worked it out myself, I’ve written out each individual report to the filesystem using cakePdf->write rather than render with PDF view, and then created a function to ZIP, download and clean up

public function zipDownloads($files = array(), $zipFile = NULL) {

	$zip = new ZipArchive();
	if ( $zip->open($zipFile, ZipArchive::CREATE) !== TRUE) {
	foreach($files as $file){
	    $zip->addFile($file, basename($file));

	foreach($files as $file){

	header('Content-Type: application/zip');
	header('Content-disposition: attachment; filename='.basename($zipFile));
	header('Content-Length: ' . filesize($zipFile));
	$fileStatus = readfile($zipFile);

	if ($fileStatus){
	    return true;
	} else {
	    return false;		    
1 Like

One suggestion I have is to use Wkhtmltopdf instead of DomPDF. It is orders of magnitude faster!

@dakota Thanks for the suggestion.

Potentially this report is 130+ pages and takes some time to generate and comes in at 5mb!

The solution I ended up with seems to meet the needs for now, it creates multiple separate files zips and downloads. Will look at Wkhtmltopdf for the future though

The ideal solution would be hand over the task of PDF generation to a queue system like Gearman or RabbitMQ. When the report is created you can have an email sent or something.

Here is a link to get you started with Gearman, once you understand the concepts you can search for Gearman CakePHP plugin to help you out.