title: How to save dynamically generated PDF files into a zip file?
body: <p>I am using the <a href="http://www.tcpdf.org" rel="nofollow">TCPDF</a> library to create PDFs on the fly.  For my application, I'd like the user to be able to batch download these documents into a ZIP file containing individual PDF documents.  Any ideas on how I might accomplish this?  </p>
  
  <p>Below is a test script to try and create three PDFs on the fly.  It currently just times out when run.</p>
  
  <p>TCPDF documentation lists different methods for outputting files, but none seem to work:</p>
  
  <p><a href="http://www.tcpdf.org/doc/classTCPDF.html#a3d6dcb62298ec9d42e9125ee2f5b23a1" rel="nofollow">http://www.tcpdf.org/doc/classTCPDF.html#a3d6dcb62298ec9d42e9125ee2f5b23a1</a></p>
  
  <pre><code>&lt;?php
  /* creates a compressed zip file */
  function create_zip($files = array(),$destination = '',$overwrite = false) {
    //if the zip file already exists and overwrite is false, return false
    if(file_exists($destination) &amp;&amp; !$overwrite) { return false; }
    //vars
    $valid_files = array();
    //if files were passed in...
    if(is_array($files)) {
      //cycle through each file
      foreach($files as $file) {
        //make sure the file exists
        if(file_exists($file)) {
          $valid_files[] = $file;
        }
      }
    }
    //if we have good files...
    if(count($valid_files)) {
      //create the archive
      $zip = new ZipArchive();
      if($zip-&gt;open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
        return false;
      }
      //add the files
      foreach($valid_files as $file) {
        $zip-&gt;addFile($file,$file);
      }
      //debug
      //echo 'The zip archive contains ',$zip-&gt;numFiles,' files with a status of ',$zip-&gt;status;
  
      //close the zip -- done!
      $zip-&gt;close();
  
      //check to make sure the file exists
      return file_exists($destination);
    }
    else
    {
      return false;
    }
  }
  date_default_timezone_set('America/Los_Angeles');
  
  $files_to_zip = array();
  
  require_once('../../libraries/tcpdf/tcpdf.php');
  
  for($i=0;$i &lt; 2 ;$i+1){
  
      $pdf = new TCPDF();
  
      $pdf-&gt;AddPage('P', 'Letter'); 
  
      $txt = 'Test '.$i;
  
      $pdf-&gt;MultiCell(1, 1, $txt);
  
      $filename = $pdf-&gt;Output('test'.$i.'.pdf', 'S');  
  
      $files_to_zip[] = $filename;
  
  }
  
  //if true, good; if false, zip creation failed
  $result = create_zip($files_to_zip,'my-archive.zip');
  
  header('Content-type: application/zip');
  header('Content-Disposition: attachment; filename="my-archive-test.zip"');
  readfile('my-archive-test.zip');
  
  ?&gt;
  </code></pre>