0

I started to work with fpdi with fpdf and I try to add more than one image to multiple pages and in the end, I want to download one PDF with the images over the PDF pages. The problem is that always just the last PDF downloaded with the last page. Why I can't download one file with all the images?

foreach ($signatures as $signa) {
  $fileContent = file_get_contents('http://www.africau.edu/images/default/sample.pdf','rb');
  $pageCount = $pdf->setSourceFile(StreamReader::createByString($fileContent));
  $pdf->setSourceFile(StreamReader::createByString($fileContent));
  $tplId = $pdf->importPage($signa->page);
  $pdf->useTemplate($tplId, 10, 10, 100);
  $pdf->Image('signature.jpg', $signa->position->x, $signa->position->y, $signa->size->width, $signa->size->height);

  if($signa->page === 2) {
    $pdf->Output('D');   
  }
}
Baruch Mashasha
  • 951
  • 1
  • 11
  • 29
  • Unless you create the PDF as a file then download it after you are done creating it you won't be able to send more than one PDF to the browser. – Dave Mar 26 '20 at 12:58
  • ok how can i wait it complete all and then download the full file ? @Dave – Baruch Mashasha Mar 26 '20 at 13:12
  • Change your output statement to write to a file then use the appropriate headers and `readfile` to send the newly created file to the browser. – Dave Mar 26 '20 at 13:14
  • 1
    You don't have an example ? @Dave – Baruch Mashasha Mar 26 '20 at 13:18
  • Afraid that's not how SO works. The documentation for FPDF shows the options for `output` and how to send a file to the browser is easily searched for (with Google or here on SO). It's fairly trivial since you appear to have most of what you need already. – Dave Mar 26 '20 at 13:20

1 Answers1

0

I found this Solution and its work for me.

Solution on my code:

$pdf = new Fpdi();
foreach ($signatures as $signa) {
$pdf->AddPage();
$fileContent = file_get_contents('http://www.africau.edu/images/default/sample.pdf','rb');
$pdf->setSourceFile(StreamReader::createByString($fileContent));
$tplId = $pdf->importPage($signa->page);
$pdf->useTemplate($tplId, 10, 10, 100);
$pdf->Image('signature.png', $signa->position->x, $signa->position->y, $signa->size->width, $signa->size->height);
}
$pdf->Output('newpdf1.pdf', 'D');   
Baruch Mashasha
  • 951
  • 1
  • 11
  • 29
  • You should move the `file_get_contents()` outside of the loop! And remove the 2nd parameter. If this is an URI to a local file you should use the local path instead! – Jan Slabon Mar 26 '20 at 15:49