1FPDI - Free PDF Document Importer
2=================================
3
4[![Latest Stable Version](https://poser.pugx.org/setasign/fpdi/v/stable.svg)](https://packagist.org/packages/setasign/fpdi)
5[![Total Downloads](https://poser.pugx.org/setasign/fpdi/downloads.svg)](https://packagist.org/packages/setasign/fpdi)
6[![Latest Unstable Version](https://poser.pugx.org/setasign/fpdi/v/unstable.svg)](https://packagist.org/packages/setasign/fpdi)
7[![License](https://poser.pugx.org/setasign/fpdi/license.svg)](https://packagist.org/packages/setasign/fpdi)
8[![Build Status](https://travis-ci.org/Setasign/FPDI.svg?branch=development)](https://travis-ci.org/Setasign/FPDI)
9
10:heavy_exclamation_mark: This document refers to FPDI 2. Version 1 is deprecated and development is discontinued. :heavy_exclamation_mark:
11
12FPDI is a collection of PHP classes facilitating developers to read pages from existing PDF
13documents and use them as templates in [FPDF](http://www.fpdf.org), which was developed by Olivier Plathey. Apart
14from a copy of [FPDF](http://www.fpdf.org), FPDI does not require any special PHP extensions.
15
16FPDI can also be used as an extension for [TCPDF](https://github.com/tecnickcom/TCPDF) or
17[tFPDF](http://fpdf.org/en/script/script92.php), too.
18
19## Installation with [Composer](https://packagist.org/packages/setasign/fpdi)
20
21Because FPDI can be used with FPDF, TCPDF or tFPDF we haven't added a fixed dependency in the main
22composer.json file. You need to add the dependency to the PDF generation library of your choice
23yourself.
24
25To use FPDI with FPDF include following in your composer.json file:
26
27```json
28{
29    "require": {
30        "setasign/fpdf": "1.8.*",
31        "setasign/fpdi": "^2.0"
32    }
33}
34```
35
36If you want to use TCPDF, you have to update your composer.json to:
37
38```json
39{
40    "require": {
41        "tecnickcom/tcpdf": "6.2.*",
42        "setasign/fpdi": "^2.0"
43    }
44}
45```
46
47If you want to use tFPDF, you have to update your composer.json to:
48
49```json
50{
51    "require": {
52        "setasign/tfpdf": "1.31.*",
53        "setasign/fpdi": "^2.3"
54    }
55}
56```
57
58## Manual Installation
59
60If you do not use composer, just require the autoload.php in the /src folder:
61
62```php
63require_once('src/autoload.php');
64```
65
66If you have a PSR-4 autoloader implemented, just register the src path as follows:
67```php
68$loader = new \Example\Psr4AutoloaderClass;
69$loader->register();
70$loader->addNamespace('setasign\Fpdi', 'path/to/src/');
71```
72
73## Changes to Version 1
74
75Version 2 is a complete rewrite from scratch of FPDI which comes with:
76- Namespaced code
77- Clean and up-to-date code base and style
78- PSR-4 compatible autoloading
79- Performance improvements by up to 100%
80- Less memory consumption
81- Native support for reading PDFs from strings or stream-resources
82- Support for documents with "invalid" data before their file-header
83- Optimized page tree resolving
84- Usage of individual exceptions
85- Several test types (unit, functional and visual tests)
86
87We tried to keep the main methods and logical workflow the same as in version 1 but please
88notice that there were incompatible changes which you should consider when updating to
89version 2:
90- You need to load the code using the `src/autoload.php` file instead of `classes/FPDI.php`.
91- The classes and traits are namespaced now: `setasign\Fpdi`
92- Page boundaries beginning with a slash, such as `/MediaBox`, are not supported anymore. Remove
93  the slash or use a constant of `PdfReader\PageBoundaries`.
94- The parameters $x, $y, $width and $height of the `useTemplate()` or `getTemplateSize()`
95  method have more logical correct default values now. Passing `0` as width or height will
96  result in an `InvalidArgumentException` now.
97- The return value of `getTemplateSize()` had changed to an array with more speaking keys
98  and reusability: Use `width` instead of `w` and `height` instead of `h`.
99- If you want to use **FPDI with TCPDF** you need to refactor your code to use the class `Tcpdf\Fpdi`
100(since 2.1; before it was `TcpdfFpdi`) instead of `FPDI`.
101
102## Example and Documentation
103
104A simple example, that imports a single page and places this onto a new created page:
105
106```php
107<?php
108use setasign\Fpdi\Fpdi;
109// or for usage with TCPDF:
110// use setasign\Fpdi\Tcpdf\Fpdi;
111
112// or for usage with tFPDF:
113// use setasign\Fpdi\Tfpdf\Fpdi;
114
115// setup the autoload function
116require_once('vendor/autoload.php');
117
118// initiate FPDI
119$pdf = new Fpdi();
120// add a page
121$pdf->AddPage();
122// set the source file
123$pdf->setSourceFile("Fantastic-Speaker.pdf");
124// import page 1
125$tplId = $pdf->importPage(1);
126// use the imported page and place it at point 10,10 with a width of 100 mm
127$pdf->useTemplate($tplId, 10, 10, 100);
128
129$pdf->Output();
130```
131
132A full end-user documentation and API reference is available [here](https://manuals.setasign.com/fpdi-manual/).