PHP Requires Chilkat v11.7.0+
Chilkat2 PHP
Find Out Whether a PDF Needs a Password Before Loading It
See more PDF Protection Examples
Demonstrates PreLoadCheck, which examines a PDF file without loading it and without any password. It reports whether the file is encrypted, by which method and algorithm, whether a password is needed to open it, and what its permissions are. An application uses it to decide whether to ask for a password, or which certificate it needs, before calling LoadFile. PreLoadCheckBd does the same for a PDF held in memory.Chilkat PHP Downloads
<?php
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 PDF used in this example is available at encrypted_user_password.pdf
// The PDF used in this example is available at no_copy_permission.pdf
$pdf = new Pdf();
$info = new JsonObject();
// A PDF that is not encrypted. Nothing but "encrypted" is reported for it.
try {
$pdf->PreLoadCheck('qa_data/pdf/two_page_memo.pdf', $info);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
echo "two_page_memo.pdf\n";
echo ' encrypted: ', ($info->BoolOf('encrypted') ? 'true' : 'false'), "\n";
// A PDF that needs a password to open.
try {
$pdf->PreLoadCheck('qa_data/pdf/encrypted_user_password.pdf', $info);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
echo "encrypted_user_password.pdf\n";
echo ' encrypted: ', ($info->BoolOf('encrypted') ? 'true' : 'false'), "\n";
echo ' method: ', $info->StringOf('method'), "\n";
echo ' algorithm: ', $info->StringOf('algorithm'), "\n";
echo ' openPasswordRequired: ', ($info->BoolOf('openPasswordRequired') ? 'true' : 'false'), "\n";
// A PDF that opens without a password but is restricted. The permissions are readable without
// any password.
try {
$pdf->PreLoadCheck('qa_data/pdf/no_copy_permission.pdf', $info);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
echo "no_copy_permission.pdf\n";
echo ' encrypted: ', ($info->BoolOf('encrypted') ? 'true' : 'false'), "\n";
echo ' openPasswordRequired: ', ($info->BoolOf('openPasswordRequired') ? 'true' : 'false'), "\n";
echo ' may copy: ', ($info->BoolOf('permissions.copy') ? 'true' : 'false'), "\n";
// The typical use: ask for a password only when one is needed, then load.
try {
$pdf->PreLoadCheck('qa_data/pdf/encrypted_user_password.pdf', $info);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
if ($info->BoolOf('openPasswordRequired')) {
// An application would prompt for the password here.
$pdf->UserPassword = 'secret';
}
try {
$pdf->LoadFile('qa_data/pdf/encrypted_user_password.pdf');
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
echo 'loaded, pages: ', $pdf->NumPages, "\n";
// Sample output:
// two_page_memo.pdf
// encrypted: False
// encrypted_user_password.pdf
// encrypted: True
// method: password
// algorithm: rc4-128
// openPasswordRequired: True
// no_copy_permission.pdf
// encrypted: True
// openPasswordRequired: False
// may copy: False
// loaded, pages: 1