Search code examples
phpfpdffpdi

FPDF, FPDI - how can I combine separate files each with unique text using PHP?


I'm stuck with using fpdf and fpdi.

I'm using this code to create 1 pdf file from 5 or more files (!important) and would like to input unique text on each page. the code in its current form puts page01test on every page rather than page02test on page 2 etc.

Each Document imported consists of one page.

I got the base of this code from: http://www.setasign.com/products/fpdi/demos/concatenate-fake/

require('fpdf.php');
require('fpdi.php');
class ConcatPdf extends FPDI
{
public $files = array();

public function setFiles($files)
{
    $this->files = $files;
}

public function concat()
{
    foreach($this->files AS $file) {
        $pageCount = $this->setSourceFile($file);
        for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
            $tplIdx = $this->ImportPage($pageNo);
            $this->AddPage();
            $this->useTemplate($tplIdx);
            $this->AddFont('NotoSans', '', 'notosans.php');
            $this->SetFont('NotoSans', '', 18);
            $this->SetTextColor(63,76,89);

            if ($pageNo == 1) {
                $this->SetXY(55, 94);
                $this->Write(5, 'page01test');
            }
            if ($pageNo == 2) {
                $this->SetXY(55, 94);
                $this->Write(5, 'page02test');
            }
            if ($pageNo == 3) {
                $this->SetXY(55, 94);
                $this->Write(5, 'page03test');
            }
            if ($pageNo == 4) {
                $this->Write(5, 'page04test');
            }
            if ($pageNo == 5) {
                $this->SetXY(55, 94);
                $this->Write(5, 'page05test');
            }
        }           
    }
 }
}


$pdf = new ConcatPdf();
$pdf->setFiles(array("Proposal1.pdf", "Proposal2.pdf","Proposal3.pdf", "Proposal4.pdf","Proposal5.pdf"));
$pdf->concat();
$pdf->Output();

Solution

  • I've been playing around with this and have found the answer myself through trial and error,

    require('customervalues.php');
    require('fpdf.php');
    require('fpdi.php');
    
    class ConcatPdf extends FPDI
    {
    public $files = array();
    
    public function setFiles($files)
    {
        $this->files = $files;
    }
    
    public function concat()
    {
        foreach ($this->files AS $file) {
            $pageCount = $this->setSourceFile($file);
            for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
                $tplIdx = $this->ImportPage($pageNo);
                $this->AddPage();
                $this->useTemplate($tplIdx);
                $this->AddFont('NotoSans', '', 'notosans.php');
                $this->SetFont('NotoSans', '', 18);
                $this->SetTextColor(63,76,89);
                if ($file == 'Proposal1.pdf') {
                    $this->SetXY(55, 94);
                    $this->Write(5, 'page01test');
                }
                if ($file == 'Proposal2.pdf') {
                    $this->SetXY(55, 94);
                    $this->Write(5, 'page02test');
                }
                if ($file == 'Proposalxl.pdf') {
                    $this->SetXY(55, 94);
                    $this->Write(5, 'page03test');
                }
                if ($file == 'Proposal4.pdf') {
                    $this->SetXY(55, 94);
                    $this->Write(5, 'page04test');
                    $this->SetXY(55, 150);
                    $this->Write(5, 'page04test2');
                }
                if ($file == 'Proposal5.pdf') {
                    $this->SetXY(55, 94);
                    $this->Write(5, 'page05test');
                }
    
            }           
        }
    }
    }
    
    $pdf = new ConcatPdf();
    $pdf->setFiles(array("Proposal1.pdf", "Proposal2.pdf","Proposalxl.pdf", "Proposal4.pdf","Proposal5.pdf"));
    $pdf->concat();
    $pdf->Output();
    

    This allows me to write unique text on each page and export it as a final document. there might be cheaper ways to do it however it works for my needs.