Subscribe to PHP Freaks RSS

Oscar Merida: Creating ZIP files and streaming the response with Silex

syndicated from www.phpdeveloper.org on January 19, 2018

On his site Oscar Merida has shared a post showing how to create a ZIP file and stream the response in a Silex-based application using the DOMPDF library and PHP's ZipArchive functionality.

Creating a zip file is easy and Rob Allen has you covered on that front to get you started. I needed to do this to create a zip file of PDF files generated by Dompdf. It turns out, you can add files to a ZipArchive object without actually creating the file if you can get the contents as a string.

The post includes the code to create the DOMPDF instance, load the HTML and render the result. From there the ZipArchive takes over and creates the archive from the PDF result. Finally the code takes the ZIP archive as input and pushes it out with a custom header to tell the browser to download it and streams back the raw ZIP contents.