Sample code for 30+ languages & platforms
PHP Requires Chilkat v11.7.0+

Encrypt a PDF for a Certificate Instead of a Password

See more PDF Protection Examples

Demonstrates certificate security, where a PDF is encrypted for one or more recipients and each opens it with their own certificate and private key. No password is involved. Only the public certificate of a recipient is needed to encrypt. Each recipient has its own permissions, and the changeSecurity permission gives a recipient owner rights. PreLoadCheck tells an application that a file is certificate-encrypted and which certificates can open it. A PDF that cannot be opened, because no matching certificate and private key were provided, fails to load.

Chilkat PHP Downloads

Chilkat2 PHP
<?php

use Chilkat2\Cert;
use Chilkat2\JsonObject;
use Chilkat2\Pdf;

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// The PDF used in this example is available at two_page_memo.pdf

// The recipient's certificate.  Only the public certificate is needed to encrypt.
$recipient = new Cert();
try {
    $recipient->LoadFromFile('qa_data/certs/recipient.cer');

    $pdf = new Pdf();
    $pdf->LoadFile('qa_data/pdf/two_page_memo.pdf');

    // Call AddEncryptCert once for each recipient.
    $pdf->AddEncryptCert($recipient);

    // recipientPermissions[0] belongs to the first certificate added.  changeSecurity gives that
    // recipient owner rights, so the file can be managed later.
    $json = new JsonObject();
    $json->UpdateString('method', 'certificate');
    $json->UpdateBool('recipientPermissions[0].changeSecurity', true);

    $jsonOut = new JsonObject();
    $pdf->Protection('encrypt', $json, $jsonOut);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

echo 'method: ', $jsonOut->StringOf('pending.method'), "\n";
echo 'algorithm: ', $jsonOut->StringOf('pending.algorithm'), "\n";
echo 'recipients: ', $jsonOut->SizeOfArray('pending.recipients'), "\n";

try {
    $pdf->WriteFile('qa_output/memo_for_recipient.pdf');

    // PreLoadCheck says how the file is protected, without loading it.
    $reader = new Pdf();
    $info = new JsonObject();
    $reader->PreLoadCheck('qa_output/memo_for_recipient.pdf', $info);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

echo 'saved file, method: ', $info->StringOf('method'), "\n";
echo 'saved file, recipients: ', $info->SizeOfArray('recipients'), "\n";

// The recipient opens it with the certificate and its private key, here from a .pfx file.
// (On Windows, a certificate that is installed in the Windows certificate store with its private
// key is found automatically when the PDF is loaded, and SetDecryptCert is not needed for it.)
$myCert = new Cert();
try {
    $myCert->LoadPfxFile('qa_data/pfx/recipient.pfx', 'pfx-password');

    $reader->SetDecryptCert($myCert);

    $reader->LoadFile('qa_output/memo_for_recipient.pdf');

    $empty = new JsonObject();
    $loadedInfo = new JsonObject();
    $reader->Protection('getInfo', $empty, $loadedInfo);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

echo 'opened with: ', $loadedInfo->StringOf('loaded.openedWith'), "\n";
echo 'pages: ', $reader->NumPages, "\n";

// Sample output:

// method: certificate
// algorithm: aes-256
// recipients: 1
// saved file, method: certificate
// saved file, recipients: 1
// opened with: certificate
// pages: 2