Search code examples
phpfpdf

Generate multiple pdfs using FPDF class


I have added a link on a page clicking on which generates a pdf and asks for download for which i have used fpdf class.

My new requirement is that clicking on the link should generate n number of pdf with different content and should ask for downloading these pdfs.

I am unable to find out the method to accomplish the same.

Please help me on this.

Thanks


Solution

  • At http://www.phpconcept.net/pclzip/ you'll find a nice php zip library. Imagine having an array of filenames like

    $filenames = array(
        "file_01.txt",
        "file_02.doc",
        "file_03.pdf"
    );
    

    the code would look like this (untested)

    require_once('pclzip.lib.php');
    $archive = new PclZip('archive.zip');
    foreach($filenames as $filename) {
        $result = $archive->add($filename);
        if($result==0) {
            die ("Error: " . $archive->errorInfo(true));
        }
    }
    header("Content-type: application/octet-stream");
    header("Content-disposition: attachment; filename=archive.zip");
    readfile("archive.zip");
    

    Hope this helps ;-)