How to merge or append pdf to dompdf export option

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
hemin
User
Posts: 167

How to merge or append pdf to dompdf export option

Post by hemin »

is there an easy way to merge or append a pdf file into the pdf export option in phpmaker?

danilo.macri
User
Posts: 117

Post by danilo.macri »

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


mobhar
User
Posts: 11905

Post by mobhar »

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


danilo.macri
User
Posts: 117

Post 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


arbei
User
Posts: 9719

Post 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.

danilo.macri
User
Posts: 117

Post by danilo.macri »

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


mobhar
User
Posts: 11905

Post by mobhar »

Currentyl, there are no pdf merging feature.

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


Post Reply