php - FPDM Only first file output works

one text

Acoording this thread I have a similar problem: Only first pdf file filled with fpdm can be opened

With FPDM (https://github.com/codeshell/fpdm) even with the latest fix (https://gist.github.com/josh-candybox/173cacc476631720a05879327950da4e) I just can't get multiple pdf files processing. One file only. It is not header related, since the files are being thrown out as files (not as downloads).

See me code. One suggested to do the loop with an ajax call. If this is really the only way, how can I do that? I even try to reset the object/class. It just doesn't care...

Error msg: FPDF-Merge Error: getFilter cannot open stream of object because filter '' is not supported, sorry.

$j=1;
foreach ($id as $value => $key) {
                      if ($value == 'adresse') {
                        
                        echo $value." -> ".nl2br($key)."<br>\n";
                        $fields = array(
                            'adresse1' => $key
                        );
                        
                        $pdf = NULL;
                        $pdf = new FPDM(__DIR__.'/fpdm/dmc3fixed.pdf' );
                        $pdf->Load($fields, true); 
                        $pdf->Merge();
                        $filename=__DIR__."/fpdm/dmc".$j.".pdf";
                        $pdf->Output($filename,'F');
                        $pdf->closeFile();
                        unset($pdf);
                        $pdf = NULL;                        
                        
                        $j++;
                                                
                      } else { ... }

P.S.: Kind of workaround, but doesn't answer my question: So, if anyone of you has the same problem, actually I managed to accomplish generating multiple PDFs with dynamic text. In my case I wanted to put addresses to letter templates. So I made a PDF Form with a multi cell. I ended up just printing the address with FPDF and FPDI, so... here you go:

require_once __DIR__ . DIRECTORY_SEPARATOR .'fpdi'.DIRECTORY_SEPARATOR.'autoload.php';
require_once(__DIR__ . DIRECTORY_SEPARATOR .'fpdf'.DIRECTORY_SEPARATOR.'fpdf.php');
require_once(__DIR__ . DIRECTORY_SEPARATOR .'fpdi'.DIRECTORY_SEPARATOR.'fpdi.php');
use setasign\Fpdi\Fpdi;
$pdf = null;
$i = 1;
foreach ($result as $value => $key) {
  $pdf = new FPDI();
  $pagecount = $pdf->setSourceFile(__DIR__ . DIRECTORY_SEPARATOR.'template.pdf');
  for ($n = 1; $n <= $pagecount; $n++) {
    $pdf->AddPage();
    $tplIdx = $pdf->importPage($n);
    $pdf->useTemplate($tplIdx);
    $pdf->SetFont('Arial', '', 11);
    $pdf->SetXY(25, 60);
    $pdf->MultiCell(80, 5, $address);
    $pdf->Output(__DIR__ . DIRECTORY_SEPARATOR."output".$i.".PDF", "F");
    $pdf = NULL;
    $i++;
  }
}

Source