The Fpdi Class Initiate and use the Fpdi class


The FPDI class is an extension for FPDF allowing you to import existing PDF pages into FPDF. The class offers all available methods as you already know from FPDF and extends it with some more methods.

There are individual classes availabe for the usage with TCPDF or tFPDF: \setasign\Fpdi\Tcpdf\Fpdi or \setasign\Fpdi\Tfpdf\Fpdi


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.


Beside the methods of FPDF and FpdfTpl the class offers or overwrites following methods: 


Get the size of an imported page.


Get the size of an imported page or template.


Imports a page.


Set the source PDF file.


Draws an imported page onto the page.


Draws an imported page or a template onto the page or another template.

Read PDF Documents From Any Source

Internally FPDI makes use of a StreamReader class which uses low level functions, such as fread() or ftell(), to interact with the stream.

The stream reader class offers static methods to create instances by a specific input type. It's constructor allows you to pass any seekable open stream resource: 


StreamReader constructor.


Creates a stream reader instance by a filename.


Creates a stream reader instance by a string value.

With these you can use a PDF document from any source. 

The Fpdi::setSourceFile() method allows you to pass several types which will be evaluated internally to a StreamReader instance:

// use a resource
$fh = fopen('a/path/to/a.pdf', 'rb');
$pdf->setSourceFile(new StreamReader($fh));
// same as
// don't forget to call fclose($fh);

// use a path
$path = 'a/path/to/a.pdf';
// same as

// use a string
$pdfString = '%%PDF-1.4...';

Stream Wrapper

If the source or target is accessible through individual stream wrappers which are registered via stream_wrapper_register() the stream needs to be seekable for reading.

Amazon S3

The AWS SDK for PHP provides an official Amazon S3 PHP stream wrapper. To use this wrapper it is needed to force allow seeking.

This can be done globally by using the stream_context_set_default() method:

// register the stream wrapper
$s3 = new S3Client(...);

// make all files for the s3 protocol seekable
stream_context_set_default(['s3' => ['seekable' => true]]);

// now the file can be handled as any other file

If you don't want to make all s3-streams seekable you have to open the streams manually and make use of a stream context:

// register the stream wrapper
$s3 = new S3Client(...);

// create a stream context
$context = stream_context_create(['s3' => ['seekable' => true]]);
// and open the stream
$stream = fopen("s3://{$bucket}/{$key}", 'r', false, $context);

// then we can use the stream in a StreamReader instance
$pdf->setSourceFile(new StreamReader($stream));

// it is also possible to pass the stream directly
// (this will create a StreamReader instance internally

// ...

// finally close the stream