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/fpdf.php');
require_once('fpdi2/src/autoload.php');

// initiate FPDI
$pdf = new \setasign\Fpdi\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 = \setasign\Fpdi\PdfReader\PageBoundaries::MEDIA_BOX;
$pdf->Cell(60, 4, $box);
$pageId = $pdf->importPage(1, $box);
$size = $pdf->useTemplate($pageId, 10, $offsetTop, 50);
$pdf->Rect(10, $offsetTop, $size['width'], $size['height']);

// re-import page ones CropBox
$box = \setasign\Fpdi\PdfReader\PageBoundaries::CROP_BOX;
$pdf->Cell(60, 4, $box);
$pageId = $pdf->importPage(1, $box);
$size = $pdf->useTemplate($pageId, 70, $offsetTop, 50);
$pdf->Rect(70, $offsetTop, $size['width'], $size['height']);

// re-import page ones TrimBox
$box = \setasign\Fpdi\PdfReader\PageBoundaries::TRIM_BOX;
$pdf->Cell(60, 4, $box);
$pageId = $pdf->importPage(1, $box);
$size = $pdf->useTemplate($pageId, 130, $offsetTop, 50);
$pdf->Rect(130, $offsetTop, $size['width'], $size['height']);

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

// re-import page ones BleedBox
$box = \setasign\Fpdi\PdfReader\PageBoundaries::BLEED_BOX;
$pdf->Cell(60, 4, $box);
$pageId = $pdf->importPage(1, $box);
$size = $pdf->useTemplate($pageId, 10, $offsetTop, 50);
$pdf->Rect(10, $offsetTop, $size['width'], $size['height']);

// re-import page ones ArtBox
$box = \setasign\Fpdi\PdfReader\PageBoundaries::ART_BOX;
$pdf->Cell(60, 4, $box);
$pageId = $pdf->importPage(1, $box);
$size = $pdf->useTemplate($pageId, 70, $offsetTop, 50);
$pdf->Rect(70, $offsetTop, $size['width'], $size['height']);

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