PHPackages                             interactively/php-pdftk - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PDF &amp; Document Generation](/categories/documents)
4. /
5. interactively/php-pdftk

ActiveLibrary[PDF &amp; Document Generation](/categories/documents)

interactively/php-pdftk
=======================

A PDF conversion and form utility based on pdftk.

1.0(3y ago)04.3k↑25%MITPHPPHP &gt;=5.3.0

Since Jul 11Pushed 3y agoCompare

[ Source](https://github.com/CiprianOpreaPrestimedia/php-pdftk)[ Packagist](https://packagist.org/packages/interactively/php-pdftk)[ RSS](/packages/interactively-php-pdftk/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (28)Used By (0)

php-pdftk
=========

[](#php-pdftk)

[![Build Status](https://camo.githubusercontent.com/d2e30c1fa97dcc83503bed18f365917cfd491921551d18c6f77f7caaaa154eb4/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f6d696b6568616572746c2f7068702d706466746b2e706e67)](http://travis-ci.org/mikehaertl/php-pdftk)[![Latest Stable Version](https://camo.githubusercontent.com/88b7f71e917000825528b1f10b35fb365806fbf0a19c300f3f05aa41b8f30c6b/68747470733a2f2f706f7365722e707567782e6f72672f6d696b6568616572746c2f7068702d706466746b2f762f737461626c652e737667)](https://packagist.org/packages/mikehaertl/php-pdftk)[![Total Downloads](https://camo.githubusercontent.com/9fc6a5b179e9c349e8c13ec01ca98977ff0af93286af8838d7f5c4307f91622a/68747470733a2f2f706f7365722e707567782e6f72672f6d696b6568616572746c2f7068702d706466746b2f646f776e6c6f616473)](https://packagist.org/packages/mikehaertl/php-pdftk)[![Latest Unstable Version](https://camo.githubusercontent.com/2671f3fda97dea149ceda103594ea0d4485b7ec4177a5045d482a1c332a83ad7/68747470733a2f2f706f7365722e707567782e6f72672f6d696b6568616572746c2f7068702d706466746b2f762f756e737461626c652e737667)](https://packagist.org/packages/mikehaertl/php-pdftk)[![License](https://camo.githubusercontent.com/8cec456e670c978b878702c39f92e08c876ed505203009c2d366f0e07a97714c/68747470733a2f2f706f7365722e707567782e6f72672f6d696b6568616572746c2f7068702d706466746b2f6c6963656e73652e737667)](https://packagist.org/packages/mikehaertl/php-pdftk)

A PDF conversion and form utility based on pdftk.

Features
--------

[](#features)

*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
- Remove passwords

Requirements
------------

[](#requirements)

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

> **Note** If you're on Ubuntu you may want to install the version from `ppa:malteworld/ppa`. The default packages seems to use snap an there have been reports about file permission issues with this version.

Installation
------------

[](#installation)

You should use [composer](https://getcomposer.org/) to install this library.

```
composer require mikehaertl/php-pdftk

```

Examples
--------

[](#examples)

### Operations

[](#operations)

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

> **Note:** Some commands allow to alias your files with a handle (see examples below). In version 2.x of pdftk a handle can be one or more upper case letters.

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-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('/full/path/to/form.pdf');
$pdf->fillForm([
        'name'=>'ÄÜÖ äüö мирано čárka',
        'nested.name' => 'valX',
    ])
    ->needAppearances()
    ->saveAs('filled.pdf');

// Fill form from FDF
$pdf = new Pdf('form.pdf');
$pdf->fillForm('data.xfdf')
    ->saveAs('filled.pdf');

// Check for errors
if (!$pdf->saveAs('my.pdf')) {
    $error = $pdf->getError();
}
```

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

[](#create-a-xfdffdf-file-from-a-php-array)

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

```
use mikehaertl\pdftk\XfdfFile;
use mikehaertl\pdftk\FdfFile;

$xfdf = new XfdfFile(['name' => 'Jürgen мирано']);
$xfdf->saveAs('/path/to/data.xfdf');

$fdf = new FdfFile(['name' => 'Jürgen мирано']);
$fdf->saveAs('/path/to/data.fdf');
```

#### Cat

[](#cat)

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

```
use mikehaertl\pdftk\Pdf;

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

// Combine pages from several files, demonstrating several ways how to add files
$pdf = new Pdf([
    'A' => '/path/file1.pdf',                 // A is alias for file1.pdf
    'B' => ['/path/file2.pdf','pass**word'],  // B is alias for file2.pdf
]);
$pdf->addFile('/path/file3.pdf','C','**secret**pw');  // C is alias file3.pdf
$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
    ->saveAs('/path/new.pdf');
```

#### Shuffle

[](#shuffle)

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([
    'A' => '/path/file1.pdf',     // A is alias for file1.pdf
    'B' => '/path/file2.pdf',     // B is alias for file2.pdf
]);

// 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
    ->saveAs('/path/new.pdf');
```

#### Burst

[](#burst)

Split a PDF file into one file per page.

```
use mikehaertl\pdftk\Pdf;

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

#### Add background PDF

[](#add-background-pdf)

Add another PDF file as background.

```
use mikehaertl\pdftk\Pdf;

// Set background from another PDF (first page repeated)
$pdf = new Pdf('/path/my.pdf');
$pdf->background('/path/back.pdf')
    ->saveAs('/path/watermarked.pdf');

// Set background from another PDF (one page each)
$pdf = new Pdf('/path/my.pdf');
$pdf->multiBackground('/path/back_pages.pdf')
    ->saveAs('/path/watermarked.pdf');
```

#### Add overlay PDF

[](#add-overlay-pdf)

Add another PDF file as overlay.

```
use mikehaertl\pdftk\Pdf;

// Stamp with another PDF (first page repeated)
$pdf = new Pdf('/path/my.pdf');
$pdf->stamp('/path/overlay.pdf')
    ->saveAs('/path/stamped.pdf');

// Stamp with another PDF (one page each)
$pdf = new Pdf('/path/my.pdf');
$pdf->multiStamp('/path/overlay_pages.pdf')
    ->saveAs('/path/stamped.pdf');
```

#### Generate FDF

[](#generate-fdf)

Create a FDF file from a given filled PDF form.

```
use mikehaertl\pdftk\Pdf;

// Create FDF from PDF
$pdf = new Pdf('/path/form.pdf');
$pdf->generateFdfFile('/path/data.fdf');
```

#### Get PDF data

[](#get-pdf-data)

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

```
use mikehaertl\pdftk\Pdf;

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

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

// Get data as string
echo $data;
$txt = (string) $data;
$txt = $data->__toString();

// Get data as array
$arr = (array) $data;
$arr = $data->__toArray();
$field1 = $data[0]['Field1'];
```

#### How to perform more than one operation on a PDF

[](#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('/path/my.pdf');
$pdf->cat(1, 5)
    ->cat([7, 4, 9]);

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

### Options

[](#options)

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

```
use mikehaertl\pdftk\Pdf;

$pdf = new Pdf('/path/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
    ->saveAs('new.pdf');

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

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

### Shell Command

[](#shell-command)

The class uses [php-shellcommand](https://github.com/mikehaertl/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('/path/my.pdf', [
    'command' => '/some/other/path/to/pdftk',
    // or on most Windows systems:
    // 'command' => 'C:\Program Files (x86)\PDFtk\bin\pdftk.exe',
    'useExec' => true,  // May help on Windows systems if execution fails
]);
```

### Temporary File

[](#temporary-file)

Internally a temporary file is created via [php-tmpfile](https://github.com/mikehaertl/php-tmpfile). You can also access that file directly, e.g. if you neither want to send or save the file but only need the binary PDF content:

```
use mikehaertl\pdftk\Pdf;

$pdf = new Pdf('/path/my.pdf');
$pdf->fillForm(['name' => 'My Name'])
    ->execute();
$content = file_get_contents( (string) $pdf->getTmpFile() );
```

API
---

[](#api)

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

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 76.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~149 days

Recently: every ~398 days

Total

22

Last Release

1174d ago

Major Versions

0.7.0 → 1.02023-02-21

PHP version history (2 changes)0.1.0PHP &gt;=5.0.0

0.4.0PHP &gt;=5.3.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/3ab8ed234d07c14f718f8a4dc5945f562b741bf9d75a73f40b58fc4c53d9dab5?d=identicon)[CiprianOpreaPrestimedia](/maintainers/CiprianOpreaPrestimedia)

---

Top Contributors

[![mikehaertl](https://avatars.githubusercontent.com/u/675062?v=4)](https://github.com/mikehaertl "mikehaertl (87 commits)")[![bravadomizzou](https://avatars.githubusercontent.com/u/1663615?v=4)](https://github.com/bravadomizzou "bravadomizzou (7 commits)")[![mcdope](https://avatars.githubusercontent.com/u/3312979?v=4)](https://github.com/mcdope "mcdope (5 commits)")[![ah017](https://avatars.githubusercontent.com/u/1122168?v=4)](https://github.com/ah017 "ah017 (3 commits)")[![allforme3](https://avatars.githubusercontent.com/u/11186922?v=4)](https://github.com/allforme3 "allforme3 (2 commits)")[![CiprianOpreaPrestimedia](https://avatars.githubusercontent.com/u/39329521?v=4)](https://github.com/CiprianOpreaPrestimedia "CiprianOpreaPrestimedia (2 commits)")[![phoenixrvd](https://avatars.githubusercontent.com/u/3441385?v=4)](https://github.com/phoenixrvd "phoenixrvd (1 commits)")[![rhynodesigns](https://avatars.githubusercontent.com/u/2198266?v=4)](https://github.com/rhynodesigns "rhynodesigns (1 commits)")[![sukrosono](https://avatars.githubusercontent.com/u/3001652?v=4)](https://github.com/sukrosono "sukrosono (1 commits)")[![axessweb](https://avatars.githubusercontent.com/u/1068898?v=4)](https://github.com/axessweb "axessweb (1 commits)")[![boesing](https://avatars.githubusercontent.com/u/2189546?v=4)](https://github.com/boesing "boesing (1 commits)")[![busgurlu](https://avatars.githubusercontent.com/u/1284742?v=4)](https://github.com/busgurlu "busgurlu (1 commits)")[![ibpavlov](https://avatars.githubusercontent.com/u/3340235?v=4)](https://github.com/ibpavlov "ibpavlov (1 commits)")[![peterchaula](https://avatars.githubusercontent.com/u/13304490?v=4)](https://github.com/peterchaula "peterchaula (1 commits)")

---

Tags

pdfpdftk

### Embed Badge

![Health badge](/badges/interactively-php-pdftk/health.svg)

```
[![Health](https://phpackages.com/badges/interactively-php-pdftk/health.svg)](https://phpackages.com/packages/interactively-php-pdftk)
```

###  Alternatives

[mikehaertl/php-pdftk

A PDF conversion and form utility based on pdftk.

1.0k10.9M11](/packages/mikehaertl-php-pdftk)[barryvdh/laravel-dompdf

A DOMPDF Wrapper for Laravel

7.3k87.6M275](/packages/barryvdh-laravel-dompdf)[mikehaertl/phpwkhtmltopdf

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

1.6k20.0M43](/packages/mikehaertl-phpwkhtmltopdf)[tecnickcom/tcpdf

TCPDF is a PHP class for generating PDF documents and barcodes.

4.5k101.8M472](/packages/tecnickcom-tcpdf)[mpdf/mpdf

PHP library generating PDF files from UTF-8 encoded HTML

4.7k77.1M490](/packages/mpdf-mpdf)[knplabs/knp-snappy

PHP library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

4.5k68.3M55](/packages/knplabs-knp-snappy)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
