Metadata Editor
MetadataEditor provides typed access to the PDF Info dictionary fields (Title, Author, Subject, etc.) and writes changes via incremental updates.
Opening a PDF
Section titled “Opening a PDF”use Phpdftk\Pdf\Toolkit\MetadataEditor;
// From file$editor = MetadataEditor::open('doc.pdf');
// From string$editor = MetadataEditor::openString($pdfBytes);
// Encrypted PDF$editor = MetadataEditor::open('secured.pdf', password: 'secret');Reading metadata
Section titled “Reading metadata”Individual fields
Section titled “Individual fields”$editor->getTitle(); // ?string$editor->getAuthor(); // ?string$editor->getSubject(); // ?string$editor->getKeywords(); // ?string$editor->getCreator(); // ?string$editor->getProducer(); // ?string$editor->getCreationDate(); // ?DateTimeImmutable$editor->getModDate(); // ?DateTimeImmutable$editor->getTrapped(); // ?string ('True', 'False', or 'Unknown')All fields at once
Section titled “All fields at once”$info = $editor->getAll(); // MetadataInfo
$info->title;$info->author;$info->subject;$info->keywords;$info->creator;$info->producer;$info->creationDate; // ?DateTimeImmutable$info->modDate; // ?DateTimeImmutable$info->trapped;MetadataInfo is a readonly value object with all nine standard Info dictionary fields.
Writing metadata
Section titled “Writing metadata”All setters are fluent:
$editor ->setTitle('Quarterly Report Q4 2025') ->setAuthor('Jane Doe') ->setSubject('Financial Summary') ->setKeywords('finance, quarterly, 2025') ->setCreator('Report Generator') ->setProducer('phpdftk');$editor->setCreationDate(new \DateTimeImmutable('2025-01-15'));$editor->setModDate(new \DateTime('now'));Trapped flag
Section titled “Trapped flag”$editor->setTrapped('True'); // True, False, or UnknownCustom fields
Section titled “Custom fields”Add non-standard keys to the Info dictionary:
$editor->setCustom('Department', 'Engineering');$editor->setCustom('ReviewStatus', 'Approved');Saving
Section titled “Saving”// To file$editor->save('updated.pdf');
// To string$bytes = $editor->toBytes();Complete example
Section titled “Complete example”MetadataEditor::open('doc.pdf') ->setTitle('Annual Report 2025') ->setAuthor('Acme Corp') ->setModDate(new \DateTimeImmutable()) ->save('updated.pdf');Document info
Section titled “Document info”$editor->getPageCount(); // intEscape hatch
Section titled “Escape hatch”$reader = $editor->getReader(); // PdfReader