Import pages from existing PDF documents and use them as templates in FPDF

FPDI

Import pages from existing PDF documents and use them as templates in FPDF

Import different boxes from one document

This demo shows how to import different boundary boxes of a pdf page.

PHP
<?php
require_once('fpdf.php');
require_once('fpdi.php');

// initiate FPDI
$pdf = new FPDI();
// add a page
$pdf->AddPage();
$pdf->SetFont('Arial', '', 10);
// set the sourcefile
$pdf->setSourceFile('PdfDocument.pdf');

$offsetTop = 15;

// import MediaBox from page 1
$box = '/MediaBox';
$pdf->Cell(60, 4, $box);
$tplIdx = $pdf->importPage(1, $box);
$size = $pdf->useTemplate($tplIdx, 10, $offsetTop, 50);
$pdf->Rect(10, $offsetTop, $size['w'], $size['h']);

// re-import page ones CropBox
$box = '/CropBox';
$pdf->Cell(60, 4, $box);
$tplIdx = $pdf->importPage(1, $box);
$size = $pdf->useTemplate($tplIdx, 70, $offsetTop, 50);
$pdf->Rect(70, $offsetTop, $size['w'], $size['h']);

// re-import page ones TrimBox
$box = '/TrimBox';
$pdf->Cell(60, 4, $box);
$tplIdx = $pdf->importPage(1, $box);
$size = $pdf->useTemplate($tplIdx, 130, $offsetTop, 50);
$pdf->Rect(130, $offsetTop, $size['w'], $size['h']);

$offsetTop = 100;
$pdf->Ln($offsetTop - 15);

// re-import page ones BleedBox
$box = '/BleedBox';
$pdf->Cell(60, 4, $box);
$tplIdx = $pdf->importPage(1, $box);
$size = $pdf->useTemplate($tplIdx, 10, $offsetTop, 50);
$pdf->Rect(10, $offsetTop, $size['w'], $size['h']);

// re-import page ones ArtBox
$box = '/ArtBox';
$pdf->Cell(60, 4, $box);
$tplIdx = $pdf->importPage(1, $box);
$size = $pdf->useTemplate($tplIdx, 70, $offsetTop, 50);
$pdf->Rect(70, $offsetTop, $size['w'], $size['h']);

$pdf->Output('newpdf.pdf', 'I');