Encrypted Documents Handle and Create Encrypted PDF Documents


The SetaPDF-Merger component is able to process and create PDF documents with standard PDF security throughout because it is based on the Core component

Merging Encrypted Documents

As the security handler of a document needs a kind of authorization to allow the extraction of pages it is a requirement that its document instance is accessable outside of the merger.

This is possible by creating a document instance manually and passing it later to the addDocument() method or by using the getDocumentByFilename() helper method of the Merger class: 

/* Get the document instance through the Merger. 
 * The document will already be cached in the merger instance
$filename = 'path/to/Example-PDF-1-encrypted.pdf';
$encryptedDoc = $merger->getDocumentByFilename($filename);
if ($encryptedDoc->hasSecHandler()) {
    $secHandler = $encryptedDoc->getSecHandler();
    // authenticate with a password (in that case the owner password)

// Add the filename

/* Create a separate document instance 
$filename = 'path/to/Example-PDF-2-encrypted.pdf';
$encryptedDoc = SetaPDF_Core_Document::loadByFilename($filename);
if ($encryptedDoc->hasSecHandler()) {
    $secHandler = $encryptedDoc->getSecHandler();
    // authenticate with a password (in that case the owner password)

// Add the document instance

Encrypting the Resulting Document

To encrypt the resulting document a security handler has to be passed to the resulting document instance before calling its save() method. Details about creating security handlers can be found here


$merger = new SetaPDF_Merger();
// add files from the file system

// merge them together

// get the resulting document instance
$document = $merger->getDocument();

/* define a handler with an owner password and without a user
 * password, allow print and copy, and do not encrypt metadata
$secHandler = SetaPDF_Core_SecHandler_Standard_Aes128::factory(
    SetaPDF_Core_SecHandler::PERM_PRINT | SetaPDF_Core_SecHandler::PERM_COPY,

// Attach the handler to the document

// add a writer
$document->setWriter(new SetaPDF_Core_Writer_Http('encrypted.pdf', true));
// save and finish