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. 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

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 FpdfTpl the class offers or overwrites following methods: 

getImportedPageSize()

Get the size of an imported page.

getTemplateSize()

Get the size of an imported page or template.

importPage()

Imports a page.

setSourceFile()

Set the source PDF file.

useImportedPage()

Draws an imported page onto the page.

useTemplate()

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: 

__construct()

StreamReader constructor.

createByFile()

Creates a stream reader instance by a filename.

createByString()

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:

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

// use a path
$path = 'a/path/to/a.pdf';
$pdf->setSourceFile(StreamReader::createByFile($path));
// same as
$pdf->setSourceFile($path);

// use a string
$pdfString = '%%PDF-1.4...';
$pdf->setSourceFile(StreamReader::createByString($pdfString));

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:

PHP
// register the stream wrapper
$s3 = new S3Client(...);
$s3->registerStreamWrapper();

// 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
$pdf->setSourceFile("s3://{$bucket}/{$key}");

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:

PHP
// register the stream wrapper
$s3 = new S3Client(...);
$s3->registerStreamWrapper();

// 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
$pdf->setSourceFile($stream);


// ...

// finally close the stream
fclose($stream);