Skip to content

Text Redactor

TextRedactor draws opaque rectangles over sensitive content. It supports both coordinate-based area redaction and text-search-based redaction. Redactions must be explicitly applied before saving.

Note: This is visual redaction — the underlying text bytes remain in the PDF stream data. The redaction rectangles visually obscure content but do not remove it from the file.

use Phpdftk\Pdf\Toolkit\TextRedactor;
// From file
$redactor = TextRedactor::open('contract.pdf');
// From string
$redactor = TextRedactor::openString($pdfBytes);
// Encrypted PDF
$redactor = TextRedactor::open('secured.pdf', password: 'secret');

Specify a rectangle by page number (1-based), x, y, width, and height in points:

$redactor->redactArea(1, 72, 700, 200, 20); // page 1
$redactor->redactArea(2, 100, 500, 150, 14); // page 2

Search for a literal string and redact all occurrences:

$redactor->redactText('John Smith');

Restrict to specific pages:

use Phpdftk\Pdf\Toolkit\PageSelector;
$redactor->redactText('SSN: 123-45-6789', PageSelector::pages(1));
$redactor->redactPattern('/\d{3}-\d{2}-\d{4}/'); // SSN pattern
$redactor->redactPattern('/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/i'); // email

The default color is black. Change it before applying:

$redactor->setRedactionColor(1.0, 1.0, 1.0); // white
$redactor->setRedactionColor(0.5, 0.5, 0.5); // gray

Redactions are staged until apply() is called. This two-step process lets you review the redaction count before committing:

$redactor
->redactText('CONFIDENTIAL')
->redactArea(1, 72, 700, 200, 20)
->apply();
echo $redactor->getRedactionCount(); // number of redaction rectangles
$redactor->save('redacted.pdf');

Calling save() or toBytes() without first calling apply() will throw a RuntimeException.

TextRedactor::open('contract.pdf')
->redactText('Jane Doe')
->redactPattern('/\d{3}-\d{2}-\d{4}/')
->redactArea(3, 50, 600, 300, 30)
->setRedactionColor(0.0, 0.0, 0.0)
->apply()
->save('redacted.pdf');
$redactor->getPageCount(); // int
$redactor->getRedactionCount(); // int (after apply)
$reader = $redactor->getReader(); // PdfReader