-2

I am using FPDF to save the pdf using converting text.

I am using following code :

   <?php
require('fpdf.php');
class PDF extends FPDF
{

function ChapterBody($file)
{
    // Read text file
    $txt = file_get_contents($file);
    $txt = str_replace("{{id_no}}","002",$txt);
    // Times 12
    $this->SetFont('Times','',12);
    // Output justified text
    $this->MultiCell(0,5,$txt);
    // Line break
    $this->Ln();
    // Mention in italics
    $this->SetFont('','I');
    //$this->Cell(0,5,'(end of excerpt)');
}

function PrintChapter($num, $title, $file)
{
    $this->AddPage();
   // $this->ChapterTitle($num,$title);
    $this->ChapterBody($file);
}
}


for($i=0;$i<3;$i++)
{
$pdf = new PDF();
$pdf->PrintChapter('','A RUNAWAY REEF','test.txt');
$pdf->Output('test'.$i.'.pdf','D');
}
?>

I have used for loop to download the pdf file for 3 times, but when I am executing this code then it is downloading only one time with file name test0.pdf, why test1.pdf and test2.pdf id not downloading using this code ?

Rohitashv Singhal
  • 4,517
  • 13
  • 57
  • 105
  • Possible duplicate of [Make multiple files to force-download](https://stackoverflow.com/questions/16390601/make-multiple-files-to-force-download) – AymDev Aug 29 '18 at 14:57
  • You just can't, it's not about FPDF – AymDev Aug 29 '18 at 14:57
  • Or you might like to see [the 2nd answer on this question](https://stackoverflow.com/questions/2339440/download-multiple-files-with-a-single-action) with link to a script generating one file. – AymDev Aug 29 '18 at 15:01
  • You could also output the PDF as strings (`$pdf->Output('S');`) and send them into a mail if it fits the needs of your project. – AymDev Aug 29 '18 at 15:04

2 Answers2

0

You can't. It's not a PHP limitation, it's an HTTP/Web-Browser limitation. HTTP doesn't provide a mechanism for sending multiple files over one request.

FPDF has nothing to do with it. If you want to send 3 files you can package them as a zip first and then just send the zip.

Another time consuming alternative is to write a script that generates multiple files and trick it that way.

Boombox93
  • 11
  • 1
  • 1
0

You can't download the 3 files together, because the HTTP protocol was designed to send only one file per request.

You need to store the 3 files one at a time on a server's folder, then you can zip them. This answer explains how to store the files in a zip file and download it.

Alberto
  • 674
  • 13
  • 25