Examples

Simple

Following example simply imports a single page and displays it at x=10, y=10 and a width of 100 mm. Then it will simply add some more text on top of it:

PHP
<?php
use \setasign\Fpdi;

require_once('fpdf/fpdf.php');
require_once('fpdi2/src/autoload.php');

// initiate FPDI
$pdf = new Fpdi\Fpdi();
// add a page
$pdf->AddPage();
// set the source file
$pdf->setSourceFile("Fantastic-Speaker.pdf");
// import page 1
$tplIdx = $pdf->importPage(1);
// use the imported page and place it at point 10,10 with a width of 100 mm
$pdf->useImportedPage($tplIdx, 10, 10, 100);

// now write some text above the imported page
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(255, 0, 0);
$pdf->SetXY(30, 30);
$pdf->Write(0, 'This is just a simple text');

$pdf->Output();            

Import a Whole Document

To import a whole document you need to iterate over all existing pages. The page count will be returned by the setSourceFile() method:  

PHP
<?php
use \setasign\Fpdi;

require_once('fpdf/fpdf.php');
require_once('fpdi2/src/autoload.php');

// initiate FPDI
$pdf = new Fpdi\Fpdi();

// get the page count
$pageCount = $pdf->setSourceFile('Laboratory-Report.pdf');
// iterate through all pages
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
    // import a page
    $templateId = $pdf->importPage($pageNo);

    $pdf->AddPage();
    // use the imported page and adjust the page size
    $pdf->useTemplate($templateId, ['adjustPageSize' => true]);

    $pdf->SetFont('Helvetica');
    $pdf->SetXY(5, 5);
    $pdf->Write(8, 'A complete document imported with FPDI');
}

// Output the new PDF
$pdf->Output();            

Concatenate Several Documents

So concatenating imported pages of different documents is that easy, too:

PHP
<?php
use \setasign\Fpdi;

require_once('fpdf/fpdf.php');
require_once('fpdi2/src/autoload.php');

// define some files to concatenate
$files = array(
    'Boombastic-Box.pdf',
    'Fantastic-Speaker.pdf',
    'Noisy-Tube.pdf'
);

// initiate FPDI
$pdf = new Fpdi\Fpdi();

// iterate through the files
foreach ($files AS $file) {
    // get the page count
    $pageCount = $pdf->setSourceFile($file);
    // iterate through all pages
    for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
        // import a page
        $templateId = $pdf->importPage($pageNo);
        // get the size of the imported page
        $size = $pdf->getTemplateSize($templateId);

        // add a page with the same orientation and size
        $pdf->AddPage($size['orientation'], $size);

        // use the imported page
        $pdf->useTemplate($templateId);

        $pdf->SetFont('Helvetica');
        $pdf->SetXY(5, 5);
        $pdf->Write(8, 'A simple concatenation demo with FPDI');
    }
}

// Output the new PDF
$pdf->Output();            

Letterhead

Following demo shows you how to add a letterhead to every created page in an implemented Header() method: 

PHP
<?php
use \setasign\Fpdi;

require_once('fpdf/fpdf.php');
require_once('fpdi2/src/autoload.php');

// Some dummy functions to generate text content
function GenerateWord()
{
    // Get a random word
    $nb = rand(3, 10);
    $w = '';
    for ($i = 1; $i <= $nb; $i++)
        $w .= chr(rand(ord('a'), ord('z')));
    return $w;
}

function GenerateSentence($words = 500)
{
    // Get a random sentence
    $nb = rand(20, $words);
    $s = '';
    for ($i = 1; $i <= $nb; $i++)
        $s .= GenerateWord() . ' ';
    return substr($s, 0, -1);
}

// The class, that overwrites the Header() method:
class Pdf extends Fpdi\Fpdi
{
    protected $_tplIdx;

    public function Header()
    {
        if (null === $this->_tplIdx) {
            $this->setSourceFile('Letterhead.pdf');
            $this->_tplIdx = $this->importPage(1);
        }

        $this->useImportedPage($this->_tplIdx);
    }
}

// initiate PDF
$pdf = new Pdf();
$pdf->SetTopMargin(30);

// Add a single page and trigger some auto page breaks.
$pdf->AddPage();
$pdf->SetFont('Helvetica');
for ($i = 20; $i > 0; $i--) {
    $pdf->MultiCell(0, 6, GenerateSentence());
    $pdf->Ln();
}

// Output the new PDF
$pdf->Output();