Lauren
Lauren

Reputation: 785

Streaming mPDF Output for Download

This is more of a conceptual question, but I'm wondering if it's possible to stream the output of mPDF directly to the user in a download (e.g. without saving in a temp folder on the server or loading into the user's browser).

I'm using a similar method successfully for downloading a zip file of S3 photos using ZipStream and AWS PHP S3 Stream Wrapper which works very well, so I would like to employ a similar method for my PDF generation.

I use the mPDF library to generate reports that have S3 images on Heroku. The mPDF documentation shows four output options including inline and download; inline loads it right into the user's browser and download forces the download prompt (desired behavior).

I've enabled S3 Stream Wrapper and embedded images in the PDF per mPDF Image() documentation like this:

$mpdf->imageVars['myvariable'] = '';

while (!feof($streamRead)) {
    // Read 1,024 bytes from the stream
    $mpdf->imageVars['myvariable'] .= fread($streamRead, 1024);
}
fclose($streamRead);

$imageHTML = '<img src="var:myvariable" class="report-img" />';
$mpdf->WriteHTML($imageHTML);

I've also added the header('X-Accel-Buffering: no'); which was required to get the ZipStream working within the Heroku environment, but the script always times out if there are more than a couple of images.

Is it possible to immediately prompt the download and just have the data stream directly to the user? I'm hoping this method can be used for more than just zip downloads but haven't had luck with this particular application yet.

Upvotes: 1

Views: 632

Answers (0)

Related Questions