Skip to content

Metadata and Encryption

Dragon edited this page Jun 3, 2026 · 1 revision

Metadata and Encryption

Set document information fields visible in a PDF reader's properties dialog, and optionally protect a document with passwords and permission restrictions.

Metadata

$document->metadata() returns a fluent Metadata builder. All fields are optional.

use DateTimeImmutable;
use DragonOfMercy\PhpPdf\Document;

$pdf = new Document();
$pdf->metadata()
    ->title('Invoice 2026-001')
    ->author('Acme Corp')
    ->subject('Monthly invoice')
    ->keywords('invoice acme 2026')
    ->creator('BillingApp 3.0')
    ->creationDate(new DateTimeImmutable());

$pdf->addPage();
$pdf->save('invoice.pdf');

Metadata methods

All methods return $this for chaining.

Method Type Description
title(string $value) string Document title (/Title).
author(string $value) string Author name (/Author).
subject(string $value) string Document subject (/Subject).
keywords(string $value) string Space-separated keywords (/Keywords).
creator(string $value) string Name of the application that created the document (/Creator).
producer(string $value) string Name of the application that produced the PDF (/Producer).
creationDate(DateTimeImmutable $value) DateTimeImmutable Document creation date (/CreationDate).
modDate(DateTimeImmutable $value) DateTimeImmutable Document modification date (/ModDate).
trapped(bool $value) bool /Trapped flag (colour separation pre-press).
documentId(string $hexId) hex string Explicit document ID used instead of the auto-generated one.

Encryption

$document->encryption() returns a fluent Encryption builder. Encryption uses AES-256 (PDF 2.0 / R6). All permissions default to denied; enable each one explicitly with the allow* methods.

$pdf = new Document();
$pdf->metadata()
    ->title('Invoice 2026-001')
    ->author('Acme Corp')
    ->creationDate(new DateTimeImmutable());
$pdf->encryption()
    ->userPassword('user')
    ->ownerPassword('owner')
    ->allowPrint();
$pdf->addPage();
$pdf->save('invoice.pdf');

Encryption methods

All methods return $this for chaining.

Method Description
userPassword(string $password) Password required to open the document (may be empty for "owner-only" restriction).
ownerPassword(string $password) Password that grants unrestricted access and sets permissions.
allowPrint() Permit printing (enables both low- and high-quality print permission bits).
allowCopy() Permit text and content extraction for accessibility or copy/paste.
allowModify() Permit modifications, form filling, and annotation editing.
encryptMetadata(bool $value) Whether the document metadata stream is also encrypted (default: false).

Constraints

  • Signing and encryption cannot be combined. Configuring both on the same document throws a PdfException at output time.
  • Requires ext-openssl.

See also

Clone this wiki locally