You are reading the documentation of the legacy and unsupported version 1 of FPDI.
For version 2 please refer to this documentation.

The FPDI Class Initiate and use the FPDI class

Introduction

The FPDI class is an extension for FPDF allowing you to import existing PDF pages into FPDF. It extends FPDF_TPL which adds a kind of template feature to FPDF.

FPDI transforms a PDF page into the same structure that is used by FPDF_TPL. The class offers all available methods as you already know from FPDF and extends it with some more methods. 

Usage

The usage of FPDI follows a simply logic:

  1. Define the document to take pages from.
  2. Import an existing page of the document
  3. Use the imported page on a page created with FPDF

By this you will notice that you will not edit the original document but you will create a completely new document by import another one page by page.

Methods

Beside the methods of FPDF and FPDF_TPL the class offers or overwrites following methods (ordered by its usage priority): 

setSourceFile()

Description
public FPDI::setSourceFile (
$filename
): int

Set a source-file.

Depending on the PDF version of the used document the PDF version of the resulting document will be adjusted to the higher version.

Parameters
$filename

A valid path to the PDF document from which pages should be imported from

Return Values

The number of pages in the document

Exceptions

Throws Exception

importPage()

Description
public FPDI::importPage (
$pageNo [, $boxName = 'CropBox' [, $groupXObject = true ]]
): int

Import a page.

The second parameter defines the bounding box that should be used to transform the page into a form XObject.

Following values are available: MediaBox, CropBox, BleedBox, TrimBox, ArtBox. If a box is not especially defined its default box will be used:

  • CropBox: Default -> MediaBox
  • BleedBox: Default -> CropBox
  • TrimBox: Default -> CropBox
  • ArtBox: Default -> CropBox

It is possible to get the used page box by the getLastUsedPageBox() method.

Parameters
$pageNo

The page number

$boxName

The boundary box to use when transforming the page into a form XObject

$groupXObject

Define the form XObject as a group XObject to support transparency (if used)

Return Values

An id of the imported page/template to use with e.g. fpdf_tpl::useTemplate()

Exceptions

Throws LogicException,InvalidArgumentException

See

useTemplate()

Description
public FPDI::useTemplate (
$tplIdx [, $x = null [, $y = null [, $w = 0 [, $h = 0 [, $adjustPageSize = false ]]]]]
): array

Use a template or imported page in current page or other template.

You can use a template in a page or in another template. You can give the used template a new size. All parameters are optional. The width or height is calculated automatically if one is given. If no parameter is given the origin size as defined in beginTemplate() or of the imported page is used.

The calculated or used width and height are returned as an array.

Parameters
$tplIdx

A valid template-id

$x

The x-position

$y

The y-position

$w

The new width of the template

$h

The new height of the template

$adjustPageSize

If set to true the current page will be resized to fit the dimensions of the template

Return Values

The height and width of the template (array('w' => ..., 'h' => ...))

Exceptions

Throws LogicException,InvalidArgumentException

getLastUsedPageBox()

Description
public FPDI::getLastUsedPageBox (
void
): string

Returns the last used page boundary box.

Return Values

The used boundary box: MediaBox, CropBox, BleedBox, TrimBox or ArtBox

cleanUp()

Description
public FPDI::cleanUp (
void
): void

Removes cycled references and closes the file handles of the parser objects.