skip to Main Content

I want to generate a pdf file with specific user information that I have stored on a mysql database, and add to it external pdf files that are stored on the server (these external files belong to the same user ). I’ve tried doing it using TCPDF but it doesn’t have the feature of adding external pdf files to the generated file. How can I achieve this?

2

Answers


  1. Maybe something around these lines:

    $pdf = new TCPDF();
    $pdf->SetCreator('');
    $pdf->AddPage();
    
    // get pdf to be added
    $sourceFile = 'path/to/pdfFile.pdf';
    $pageCount = $pdf->setSourceFile($sourceFile);
    for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
        $templateId = $pdf->importPage($pageNo);
        $pdf->AddPage();
        $pdf->useTemplate($templateId);
    }
    
    $pdf->Output($destinationFile, 'F');
    
    Login or Signup to reply.
  2. I suggest you to use library libmergepdf you can find it online, with that plugin you can add page PDF to your original PDF, so save tcpdf in a temp folder then add pages from other PDF and save the final output like:

    require_once('./vendor/autoload.php');
    
    use iiolibmergepdfMerger;
    use iiolibmergepdfDriverTcpdiDriver;
    $query = $mysqli->prepare("SELECT * FROM document"); //query of files
    $query->execute();
    $result = $query->get_result();
    $merger = new Merger(new TcpdiDriver);
    $merger->addFile($_SERVER['DOCUMENT_ROOT'] . '/files/temp/' . $nameFile); //original path PDF
    while ($document = $result->fetch_array()) {
        $pdf = $_SERVER['DOCUMENT_ROOT'] . $document['urlfile'];
        if (is_file($pdf)) {
            $merger->addFile($pdf);
        }
    }
    ob_end_clean();
    ob_start();
    $merger->merge();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search