Page 1 of 1

How to merge or append pdf to dompdf export option

Posted: Wed Jul 08, 2020 10:02 pm
by hemin
is there an easy way to merge or append a pdf file into the pdf export option in phpmaker?

Re: How to merge or append pdf to dompdf export option

Posted: Sat Jun 22, 2024 9:43 pm
by danilo.macri

I'm trying to figure out how to do it too


Re: How to merge or append pdf to dompdf export option

Posted: Sun Jun 23, 2024 12:04 am
by mobhar

Did you mean you want to append your additional content to the exported pdf file?


Re: How to merge or append pdf to dompdf export option

Posted: Sun Jun 23, 2024 12:32 am
by danilo.macri

yes, I would also like to merge the attached pdf files
I'm trying like this:

function Page_Exporting(&$doc)
{
      return false;
}

il global code:

use Dompdf\Dompdf;
use Dompdf\Options;
use setasign\Fpdi\Fpdi;

function generaPDF($html) {
    $options = new Options();
   $options->set('isHtml5ParserEnabled', true);
    $options->set('isRemoteEnabled', true);
    $dompdf = new Dompdf();
    $dompdf->loadHtml($html);
    $dompdf->setPaper('A4', 'portrait');
    $dompdf->render();
    return $dompdf->output();
}


function Row_Export($doc, $rs)
{
    $html = $rs['testorapportohtml'];
    
    // Genera il PDF dal contenuto HTML
    $pdfContent = generaPDF($html);
    $rapportoId = $rs['idrapporto']; 
    $pdfPath = "files/rapporto_$rapportoId.pdf";
    file_put_contents($pdfPath, $pdfContent);


    $allegati = ExecuteRows("SELECT * FROM allegati WHERE idrapporto=".$rapportoId);

    $pdfFiles = [$pdfPath];
 foreach ($allegati as $allegato) {
        // Costruisci il percorso del file PDF allegato
        $filePath = 'files/'.$allegato['allegato'];
        $pdfFiles[] = $filePath;
    }


    $pdf = new FPDI();


    foreach ($pdfFiles as $file) {
        $pageCount = $pdf->setSourceFile($file);
        for ($i = 1; $i <= $pageCount; $i++) {
            $tplIdx = $pdf->importPage($i);
            $pdf->AddPage();
            $pdf->useTemplate($tplIdx);
        }
    }

    $mergedPdfPath = "pdf/unito_$rapportoId.pdf";
    $pdf->Output('F', $mergedPdfPath);

    return $mergedPdfPath;
}

if click to export pdf:

SyntaxError: Unexpected token '<', "
"... is not valid JSON


Re: How to merge or append pdf to dompdf export option

Posted: Sun Jun 23, 2024 10:24 am
by arbei
  1. Row_Export server event is used to export a row, not a document.
  2. The server events for export still uses the $doc to output, if you don't use it at all, the output will be wrong.
  3. In your case you better use Row_CustomAction server event.

Re: How to merge or append pdf to dompdf export option

Posted: Sun Jun 23, 2024 3:26 pm
by danilo.macri

How do I merge all the pdf attachments of the table, with the same id, into a single file?


Re: How to merge or append pdf to dompdf export option

Posted: Mon Jun 24, 2024 9:31 am
by mobhar

Currentyl, there are no pdf merging feature.

As mentioned, you should use Row_CustomAction server event for such case.