Novis\Pdf\Document::withPassword
$document->withPassword(string $password): Document$document->withPassword( string $password, ): Document
Encrypts the PDF file, so a reader needs the password to open it.
The encryption is AES-256, which every current PDF viewer can open. A person with the password can open the file and change it. The same document with the same password always gives the same bytes.
A PDF/A file cannot have a password, so withPassword with a PDF/A standard makes render throw a LogicError. Reader and Pages cannot open an encrypted file. They throw a ParseError.
withPassword returns a new Document and does not change the one you called it on.
Good to know: a password protects a file that travels, such as a statement sent by email. Send the password another way, never in the same email.
The examples below encrypt a statement, give each customer their own password, and show the error for a PDF/A file.
Parameters
$passwordstring
Return value
Returns Document.
Changelog
| Version | Change |
|---|---|
0.0.6 | Each Novis\Pdf\Document method that returns a new document now starts with with, for example withHeader() and withPassword(). created() is now withCreationTime(). |
Examples
A password protected statement
<?nvs// Shows a bank statement that a customer opens with a password.use Novis\Pdf\Document;use Novis\Pdf\Reader;
string $html = '<h1>Statement for March</h1><table>' . '<tr><td>1 March</td><td>Shop</td><td>-12.50 EUR</td></tr>' . '<tr><td>3 March</td><td>Salary</td><td>2400.00 EUR</td></tr></table>';
// `withPassword` encrypts the file with AES-256.// A PDF viewer asks for the password before it shows the pages.var $statement = Document::fromHtml($html)->withPassword('Ada-1815-12-10')->render();echo $statement->pageCount, " page\n";echo Core\Bytes::contains($statement->bytes, '/Encrypt' as bytes) ? 'encrypted' : 'not encrypted', "\n";
// `Reader` cannot read an encrypted file, so it throws a `ParseError`.try { Reader::open($statement->bytes);} catch (ParseError $e) { echo $e->message, "\n";}1 pageencryptedthe PDF is encrypted. Only a PDF without encryption can be read, merged or placedOne password for each customer
<?nvs// Shows statements for two customers, each with its own password.use Novis\Pdf\Document;
var $statement = Document::fromHtml('<h1>Statement for March</h1><p>Balance: 2387.50 EUR</p>');
// `withPassword` returns a new document, so one document can give a file for each customer.bytes $ada = $statement->withPassword('ada-7731')->render()->bytes;bytes $grace = $statement->withPassword('grace-1906')->render()->bytes;
// The same document and the same password give the same bytes.// A different password gives a different file. This prints `same` and then `different`.bytes $again = $statement->withPassword('ada-7731')->render()->bytes;echo $again == $ada ? 'same' : 'different', "\n";echo $ada == $grace ? 'same' : 'different', "\n";samedifferentA password on a pdf a file
<?nvs// Shows the error for a password on a PDF/A file.use Novis\Pdf\Document;
string $html = "<html lang=\"en\"><head><title>Statement</title></head>" . '<body><h1>Statement for March</h1></body></html>';
// PDF/A does not allow encryption, so `render` throws a `LogicError`.try { Document::fromHtml($html) ->withStandard(Document::Standard::PdfA2b) ->withCreationTime(Core\Time\Instant::fromIso('2026-03-31T18:00:00Z')) ->withPassword('ada-7731') ->render();} catch (LogicError $e) { echo $e->message, "\n";}a PDF/A document cannot have a password