Pdf Class API

mikehaertl\pdftk\Pdf is the primary class of this library. It acts as a fluent builder for the underlying pdftk shell command.

Constructor

public function __construct(string|Pdf|array|null $pdf = null, array $options = [])
  • $pdf: The input file(s). Can be a string filepath, an existing Pdf instance (for chaining), or an associative array of paths mapped to handles (e.g., ['A' => 'file.pdf']).
  • $options: Configuration array passed to the underlying Command instance (e.g., ['command' => '/path/to/pdftk', 'useExec' => true]).

Execution & Output Methods

These methods trigger the execution of the shell command.

  • execute(): bool Executes the command and stores the output in a temporary file. Returns true on success, false on failure.

  • saveAs(string $name): bool Executes the command and copies the resulting temporary file to the specified $name path.

  • send(?string $filename = null, bool $inline = false, array $headers = []): bool Executes the command and outputs the PDF directly to the browser via HTTP headers. If $filename is provided, it prompts a download. If $inline is true, it renders in the browser.

  • toString(): string|bool Executes the command and returns the raw PDF binary as a string, or false on failure.

  • getError(): string Returns the standard error output (stderr) generated by the shell command, useful for debugging.

Operations

These methods set the primary action for the pdftk command. You may only call one operation per Pdf instance.

  • fillForm(string|array $data, string $encoding = 'UTF-8', bool $dropXfa = true, string $format = 'xfdf'): self Fills PDF forms. Accepts an array of data or a path to an FDF/XFDF file.

  • cat($start = null, $end = null, $handle = null, $qualifier = null, $rotation = null): self Concatenates pages from inputs. $start can also be an array of specific pages.

  • shuffle($start, $end = null, $handle = null, $qualifier = null, $rotation = null): self Interleaves pages from inputs into a single output.

  • burst(?string $filepattern = null): bool Splits a single PDF into multiple single-page PDFs. Immediately executes the command.

  • background(string $file): self Applies the first page of $file as a watermark behind the document.

  • multiBackground(string $file): self Applies pages of $file sequentially as backgrounds.

  • stamp(string $file): self Applies the first page of $file as an overlay on top of the document.

  • multiStamp(string $file): self Applies pages of $file sequentially as overlays.

  • attachFiles(array $files, ?string $toPage = null): self Embeds files into the document or specifically to $toPage.

  • unpackFiles(?string $dir = null): bool Extracts embedded files to the specified directory. Immediately executes the command.

  • generateFdfFile(string $name): bool Extracts form field data from the PDF and saves it to $name. Immediately executes the command.

  • updateInfo(string|array|InfoFields|InfoFile $data, string $encoding = 'UTF-8'): self Injects metadata into the document's Info dictionary.

  • getData(bool $utf8 = true): InfoFields|bool Executes dump_data and returns document metadata. Immediately executes.

  • getDataFields(bool $utf8 = true): DataFields|bool Executes dump_data_fields and returns form structure data. Immediately executes.

Modifiers

These methods append specific flags to the command and return $this for fluent chaining.

  • allow($permissions = null): self Sets privileges (e.g., 'Printing'). Requires owner password.
  • flatten(): self Merges form fields into the static document.
  • compress($compress = true): self Compresses (or uncompresses) page streams.
  • keepId($id = 'first'): self Retains original PDF ID ('first' or 'last') when merging.
  • needAppearances(): self Forces PDF readers to generate visual appearances for filled form fields.
  • dropXfa(): self Strips newer XML form data.
  • dropXmp(): self Strips newer XML metadata.
  • setPassword(string $password): self Sets Owner password.
  • setUserPassword(string $password): self Sets User password.
  • passwordEncryption($strength = 128): self Uses 128-bit or 40-bit encryption.
  • replacementFont(string $path): self Uses a custom local font (requires pdftk-java >= 3.3.0).