Metadata & Attachments

PDFs are more than just visual documents; they can hold rich metadata, bookmarks, form field structures, and even embedded file attachments. php-pdftk provides specialized methods to read and manipulate this hidden data.

Extracting Document Data

To read a document's metadata (Title, Author, Creator) or its Bookmark structure, use the getData() method. To read the technical structure of interactive form fields, use getDataFields().

Both methods execute a dump_data operation and return an ArrayObject (specifically InfoFields or DataFields). You can cast these objects to arrays for easy programmatic access.

use mikehaertl\pdftk\Pdf;

$pdf = new Pdf('/path/to/document.pdf');

// Extract general metadata and bookmarks
$data = $pdf->getData();

if ($data === false) {
    die("Error reading PDF: " . $pdf->getError());
}

// Cast to array for easy access
$dataArray = (array) $data;

echo "Title: " . ($dataArray['Info']['Title'] ?? 'No Title') . "\n";
echo "Author: " . ($dataArray['Info']['Author'] ?? 'Unknown') . "\n";

// Iterate over bookmarks
if (isset($dataArray['Bookmark'])) {
    foreach ($dataArray['Bookmark'] as $bookmark) {
        echo "Bookmark: {$bookmark['Title']} (Page {$bookmark['PageNumber']})\n";
    }
}

To view the raw string output exactly as pdftk produced it, simply cast the object to a string:

$fields = $pdf->getDataFields();
echo (string) $fields;

Updating Metadata

You can inject or overwrite a document's metadata using updateInfo(). You must pass an array formatted similarly to the structure returned by getData().

use mikehaertl\pdftk\Pdf;

$pdf = new Pdf('/path/to/document.pdf');

// Define new metadata
$newData = [
    'Info' => [
        'Title' => 'Annual Financial Report 2024',
        'Author' => 'Finance Department',
        'Subject' => 'Q1-Q4 Earnings',
        'Keywords' => 'finance, 2024, report',
    ]
];

// Apply data and drop older XMP metadata to ensure readers use the new info
$pdf->updateInfo($newData)
    ->dropXmp()
    ->saveAs('/path/to/updated_document.pdf');

Managing Attachments

PDFs support embedding standard files directly into the document structure. This is incredibly useful for formats like ZUGFeRD or Factur-X (where an XML data file is attached to a visual PDF invoice), or simply bundling source code or spreadsheets with a report.

Attaching Files

You can attach files at the document level, or bind them to a specific page.

use mikehaertl\pdftk\Pdf;

$filesToAttach = [
    '/path/to/source_data.csv',
    '/path/to/reference_image.png',
];

$pdf = new Pdf('/path/to/report.pdf');

// Attach to the whole document
$pdf->attachFiles($filesToAttach)
    ->saveAs('/path/to/report_with_attachments.pdf');

// Alternatively, bind the attachments to Page 3
$pdf->attachFiles($filesToAttach, 3)
    ->saveAs('/path/to/report_page3_attached.pdf');

Unpacking Files

If you receive a PDF with embedded attachments, you can extract them to a local directory using unpackFiles().

use mikehaertl\pdftk\Pdf;

$pdf = new Pdf('/path/to/invoice_with_xml.pdf');

// Extract attachments to a specific folder
$result = $pdf->unpackFiles('/path/to/extraction_folder/');

if ($result === false) {
    echo "Failed to unpack: " . $pdf->getError();
}