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 existingPdfinstance (for chaining), or an associative array of paths mapped to handles (e.g.,['A' => 'file.pdf']).$options: Configuration array passed to the underlyingCommandinstance (e.g.,['command' => '/path/to/pdftk', 'useExec' => true]).
Execution & Output Methods
These methods trigger the execution of the shell command.
-
execute(): boolExecutes the command and stores the output in a temporary file. Returnstrueon success,falseon failure. -
saveAs(string $name): boolExecutes the command and copies the resulting temporary file to the specified$namepath. -
send(?string $filename = null, bool $inline = false, array $headers = []): boolExecutes the command and outputs the PDF directly to the browser via HTTP headers. If$filenameis provided, it prompts a download. If$inlineis true, it renders in the browser. -
toString(): string|boolExecutes the command and returns the raw PDF binary as a string, orfalseon failure. -
getError(): stringReturns 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'): selfFills 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): selfConcatenates pages from inputs.$startcan also be an array of specific pages. -
shuffle($start, $end = null, $handle = null, $qualifier = null, $rotation = null): selfInterleaves pages from inputs into a single output. -
burst(?string $filepattern = null): boolSplits a single PDF into multiple single-page PDFs. Immediately executes the command. -
background(string $file): selfApplies the first page of$fileas a watermark behind the document. -
multiBackground(string $file): selfApplies pages of$filesequentially as backgrounds. -
stamp(string $file): selfApplies the first page of$fileas an overlay on top of the document. -
multiStamp(string $file): selfApplies pages of$filesequentially as overlays. -
attachFiles(array $files, ?string $toPage = null): selfEmbeds files into the document or specifically to$toPage. -
unpackFiles(?string $dir = null): boolExtracts embedded files to the specified directory. Immediately executes the command. -
generateFdfFile(string $name): boolExtracts 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'): selfInjects metadata into the document's Info dictionary. -
getData(bool $utf8 = true): InfoFields|boolExecutesdump_dataand returns document metadata. Immediately executes. -
getDataFields(bool $utf8 = true): DataFields|boolExecutesdump_data_fieldsand returns form structure data. Immediately executes.
Modifiers
These methods append specific flags to the command and return $this for fluent chaining.
allow($permissions = null): selfSets privileges (e.g., 'Printing'). Requires owner password.flatten(): selfMerges form fields into the static document.compress($compress = true): selfCompresses (or uncompresses) page streams.keepId($id = 'first'): selfRetains original PDF ID ('first' or 'last') when merging.needAppearances(): selfForces PDF readers to generate visual appearances for filled form fields.dropXfa(): selfStrips newer XML form data.dropXmp(): selfStrips newer XML metadata.setPassword(string $password): selfSets Owner password.setUserPassword(string $password): selfSets User password.passwordEncryption($strength = 128): selfUses 128-bit or 40-bit encryption.replacementFont(string $path): selfUses a custom local font (requirespdftk-java>= 3.3.0).