Installation & Setup

To effectively use php-pdftk, you must configure two components: the PHP library itself (via Composer), and the underlying pdftk system binary on your server.

1. Install the PHP Library

The library is available on Packagist and is installed via Composer. It requires PHP 8.1 or higher.

Run the following command in your project directory:

composer require mikehaertl/php-pdftk

This will also install its dependencies: mikehaertl/php-shellcommand (for secure CLI execution) and mikehaertl/php-tmpfile (for managing the temporary files pdftk requires).

2. Install the pdftk Binary

The pdftk command must be installed and accessible in your system's PATH. This library is written for pdftk 2.x versions. While it is generally backwards compatible with 1.x, some advanced features (like multi-character file handles or explicit page rotations) may behave differently.

Ubuntu / Debian

The recommended approach on modern Ubuntu systems is to use the Java port of pdftk, which is actively maintained and available via apt.

sudo apt-get update
sudo apt-get install pdftk-java

⚠️ Important: The Ubuntu Snap Issue

There is a known issue if you install the pdftk package via Canonical's snap package manager.

The Problem: Snap applications are heavily sandboxed. The snap version of pdftk does not have permission to read from or write to the system's default /tmp directory. Because php-pdftk relies on php-tmpfile to generate intermediate files in /tmp, operations will fail silently or throw permission errors.

The Solution:

  1. Preferred: Uninstall the snap version (sudo snap remove pdftk) and install pdftk-java via apt as shown above.
  2. Alternative: If you must use the snap version, you must configure a custom temporary directory that the snap application can access, and pass it to the library. See Temporary Files for instructions on setting a custom $pdf->tempDir.

CentOS / RHEL / Fedora

On RPM-based systems, pdftk is often available in standard or EPEL repositories.

sudo yum install pdftk
# OR for newer Fedora/RHEL
sudo dnf install pdftk

Windows

php-pdftk works perfectly on Windows servers. You will need to download and install PDFtk Server (the command-line tool, not just the GUI builder) from the official PDFtk website.

By default, Windows does not always add PDFtk to the system PATH. You may need to specify the exact path to the executable in your PHP code. See Configuration for details on passing the command option to the constructor.

Verifying Installation

Once both components are installed, you can verify your setup by checking the version from the command line:

pdftk --version

If this outputs version information (e.g., pdftk port to java 3.3.3...), your server is ready. Proceed to the Quick Start guide.