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.
The usage of FPDI follows a simply logic:
- Define the document to take pages from.
- Import an existing page of the document
- 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.
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.
Internally FPDI makes use of a
StreamReader class which uses low level functions, such as
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:
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.
// 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));