php-pdftk 213

A PDF conversion and form utility based on pdftk


Written in PHP by 5 contributors


Need Support?

we offer maintenance, support and development services for all our open source projects.

Contact Us


Build Status Latest Stable Version Total Downloads Latest Unstable Version HHVM Status License

A PDF conversion and form utility based on pdftk.


php-pdftk brings the full power of pdftk to PHP - and more.

  • Fill forms, either from a XFDF/FDF file or from a data array (UTF-8 safe for unflattened forms, requires pdftk 2.x !)
  • Create XFDF or FDF files from PHP arrays (UTF-8 safe!)
  • Create FDF files from filled PDF forms
  • Combine pages from several PDF files into a new PDF file
  • Split a PDF into one file per page
  • Add background or overlay PDFs
  • Read out meta data about PDF and form fields
  • Set passwords and permissions


  • The pdftk command must be installed and working on your system
  • This library is written for pdftk 2.x versions. You should be able to use it with pdftk 1.x but not all methods will work there. For details consult the man page of pdftk on your system.


You should use use composer to install this library.

composer require mikehaertl/php-pdftk



Please consult the pdftk man page for each operation to find out how each operation works in detail and which options are available.

For all operations you can either save the PDF locally through saveAs($name) or send it to the browser with send(). If you pass a filename to send($name) the client browser will open a download dialogue whereas without a filename it will usually display the PDF inline.

IMPORTANT: You can always only perform one of the following operations on a single PDF instance. Below you can find a workaround if you need multiple operations.

Fill Form

Fill a PDF form with data from a PHP array or an XFDF/FDF file.

use mikehaertl\pdftk\Pdf;

// Fill form with data array
$pdf = new Pdf('form.pdf');
        'name'=>'ÄÜÖ äüö мирано čárka',
        '' => 'valX',

// Fill form from FDF
$pdf = new Pdf('form.pdf');

Note: When filling in UTF-8 data, you should always add the needAppearances() option. This will make sure, that the PDF reader takes care of using the right fonts for rendering, something that pdftk can't do for you. Also note that flatten() doesn't really work well if you have special characters in your data.

Create a XFDF/FDF file from a PHP array

This is a bonus feature that is not available from pdftk.

use mikehaertl\pdftk\FdfFile;

$fdf = new XfdfFile(['name'=>'Jürgen мирано']);

$fdf = new FdfFile(['name'=>'Jürgen мирано']);


Assemble a PDF from pages of one or more PDF files.

use mikehaertl\pdftk\Pdf;

// Extract pages 1-5 and 7,4,9 into a new file
$pdf = new Pdf('my.pdf');
$pdf->cat(1, 5)
    ->cat(array(7, 4, 9))

// Combine pages from several files, demonstrating several ways how to add files
$pdf = new Pdf(array(
    'A' => 'file1.pdf',                 // Reference file as 'A'
    'B' => ['file2.pdf','pass**word'],  // Reference file as 'B'
$pdf->addFile('file3.pdf','C','**secret**pw');  // Reference file as 'C'
$pdf->cat(1, 5, 'A')                // pages 1-5 from A
    ->cat(3, null, 'B')             // page 3 from B
    ->cat(7, 'end', 'B', null, 'east') // pages 7-end from B, rotated East
    ->cat('end',3,'A','even')       // even pages 3-end in reverse order from A
    ->cat([2,3,7], 'C')             // pages 2,3 and 7 from C


Like cat() but create "streams" and fill the new PDF with one page from each stream at a time.

use mikehaertl\pdftk\Pdf;

$pdf = new Pdf(array(
    'A' => 'file1.pdf',     // Reference file as 'A'
    'B' => 'file2.pdf',     // Reference file as 'B'

// new.pdf will have pages A1, B3, A2, B4, A3, B5, ...
$pdf->shuffle(1, 5, 'A')    // pages 1-5 from A
    ->shuffle(3, 8, 'B')    // pages 3-8 from B


Split a PDF file into one file per page.

use mikehaertl\pdftk\Pdf;

$pdf = new Pdf('my.pdf');
$pdf->burst('page_%d.pdf');     // Supply a printf() pattern

Add background PDF

Add another PDF file as background.

use mikehaertl\pdftk\Pdf;

// Set background from another PDF (first page repeated)
$pdf = new Pdf('my.pdf');

// Set background from another PDF (one page each)
$pdf = new Pdf('my.pdf');

Add overlay PDF

Add another PDF file as overlay.

use mikehaertl\pdftk\Pdf;

// Stamp with another PDF (first page repeated)
$pdf = new Pdf('my.pdf');

// Stamp with another PDF (one page each)
$pdf = new Pdf('my.pdf');

Generate FDF

Create a FDF file from a given filled PDF form.

use mikehaertl\pdftk\Pdf;

// Create FDF from PDF
$pdf = new Pdf('form.pdf');

Get PDF data

Read out metadata or form field information from a PDF file.

use mikehaertl\pdftk\Pdf;

// Get data
$pdf = new Pdf('my.pdf');
$data = $pdf->getData();

// Get form data fields
$pdf = new Pdf('my.pdf');
$data = $pdf->getDataFields();

How to perform more than one operation on a PDF

As stated above, you can only perform one of the preceeding operations on a single PDF instance. If you need more than one operation you can feed one Pdf instance into another:

use mikehaertl\pdftk\Pdf;

// Extract pages 1-5 and 7,4,9 into a new file
$pdf = new Pdf('my.pdf');
$pdf->cat(1, 5)
    ->cat(array(7, 4, 9));

// We now use the above PDF as source file for a new PDF
$pdf2 = new Pdf($pdf);
$pdf2->fillForm(array('name'=>'ÄÜÖ äüö мирано čárka'))


You can combine the above operations with one or more of the following options.

use mikehaertl\pdftk\Pdf;

$pdf = new Pdf('my.pdf');

$pdf->allow('AllFeatures')      // Change permissions
    ->flatten()                 // Merge form data into document (doesn't work well with UTF-8!)
    ->compress($value)          // Compress/Uncompress
    ->keepId('first')           // Keep first/last Id of combined files
    ->dropXfa()                 // Drop newer XFA form from PDF
    ->dropXmp()                 // Drop newer XMP data from PDF
    ->needAppearances()         // Make clients create appearance for form fields
    ->setPassword($pw)          // Set owner password
    ->setUserPassword($pw)      // Set user password
    ->passwordEncryption(128)   // Set password encryption strength

// Example: Fill PDF form and merge form data into PDF
// Fill form with data array
$pdf = new Pdf('form.pdf');
$pdf->fillForm(array('name'=>'My Name'))

// Example: Remove password from a PDF
$pdf = new Pdf;
$pdf->addPage('my.pdf', null, 'some**password')


The class uses php-shellcommand to execute pdftk. You can pass $options for its Command class as second argument to the constructor:

use mikehaertl\pdftk\Pdf;

$pdf = new Pdf('my.pdf', [
    'command' => '/some/other/path/to/pdftk',
    'useExec' => true,  // May help on Windows systems if execution fails


Please consult the source files for a full documentation of each method.

43 other PHP projects

phpwkhtmltopdfPHP 716

A slim PHP wrapper around wkhtmltopdf with an easy to use and clean OOP interface

yii2-localeurlsPHP 189

Automatic locale/language management for URLs

YiiRedisPHP 131

A set of wrappers for different data types in redis

yiipasswordPHP 81

Password strategies for Yii

yii2-dockerizedPHP 64

A template for docker based Yii 2 applications

YiiSolrPHP 50

A wrapper for the pecl solr library that provides common Yii constructs, such as models, data providers etc

php-shellcommandPHP 48

A simple object oriented interface to execute shell commands in PHP

Yii-Docs-GeneratorPHP 47

Generates HTML documentation for Yii applications

YiiGitPHP 36

A git wrapper for Yii, allows access to all git commands programatically

YiiElasticSearchPHP 32

Elastic Search client for Yii

YiiCurlPHP 30

A curl library for Yii

YiiStateMachinePHP 28

A state machine behavior for Yii

yii2-streamlogPHP 19

A Yii 2 log target for streams in URL format

oauth2yiiPHP 18

An OAuth2 client / server extension for the Yii framework

restyiiPHP 16

A RESTful extension for Yii.

packagecompressorPHP 16

A Javascript/CSS compressor based on Yii's package system

localeurlsPHP 12

Automatic locale/language management for URLs

php-excel-readerPHP 12

It reads the binary format of XLS files directly and can return values and formats from any cell. This project is the fork of that, apparently, is no longer maintained.

pdfablePHP 10

A Yii extension to create PDFs with PHPWkHtmlToPdf/wkhtmltopdf

yii-api-vimPHP 8

Yii API manual plugin for VIM

yii2-excel-messagePHP 7

Translate messages via Excel files

yii2-bs3activeformPHP 7

A Bootstrap 3 enhanced ActiveForm for Yii 2

xcrudcontrollerPHP 7

A base class to quickly build customized CRUD interfaces.

handlebarsphpPHP 6

Transpiles handlebars templates into native PHP templates

Yii-Package-ManagerPHP 6

A package manager for Yii

php-tmpfilePHP 6

A convenience class for temporary files

yii2-apidoc-vimPHP 5

Yii2 apidoc as Vim helpfiles

php-orientdbPHP 5

A fast PHP driver for the OrientDB binary protocol.

YiiUsersPHP 4

User management for Yii

translatablePHP 4

Transparent attribute translation for ActiveRecords

flushablePHP 3

Yii dependency that allows to flush records from the cache

Yii-Resource-ManagerPHP 3

Helpers to allow Yii to interact with resources (files) of different types

bs3activeformPHP 2

A lightweight utility to render Bootstrap 3 forms in Yii

YiiEmailerPHP 2

Emailing functions for Yii

AccessRestrictablePHP 2

A Yii ActiveRecordBehavior that automatically applies conditions for access restriction to every query.

defaultpersisterPHP 1

Yii extension to save and restore model values in user session.

hybridauthmanagerPHP 1

An AuthManager for Yii that stores the hierarchy in a flat PHP file and the assignments in DB

YiiLinkablePHP 1

A simple extension for Yii allowing easy and consistent access to model URLs and appropriate anchor text.

YiiYaaPHP 1

YAA is an additional abstraction layer for Yii that aggregates a number of child models into a clean single model that is easy to cache


Yii extension to create URLs that allow to return to a page by storing its GET Parameters on a stack.


Work with PHP files in Active Record way


An alternative Yii2 application template for purists


simple useless twitter client