PHP Requires Chilkat v11.7.0+
Chilkat2 PHP
Remove the Password and Encryption from a PDF
See more PDF Protection Examples
Demonstrates the removeEncryption operation, which saves the PDF with no encryption at all: no password, no restrictions. It needs owner rights, so the PDF is loaded with its owner password. As with every Protection operation, the change is applied when the file is written.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 encrypted_user_password.pdf
$pdf = new Pdf();
// Removing the encryption needs owner rights: load with the owner password.
$pdf->OwnerPassword = 'owner-secret';
try {
$pdf->LoadFile('qa_data/pdf/encrypted_user_password.pdf');
$json = new JsonObject();
$jsonOut = new JsonObject();
$pdf->Protection('getInfo', $json, $jsonOut);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
echo 'encrypted, as loaded: ', ($jsonOut->BoolOf('loaded.encrypted') ? 'true' : 'false'), "\n";
echo 'algorithm: ', $jsonOut->StringOf('loaded.algorithm'), "\n";
try {
$pdf->Protection('removeEncryption', $json, $jsonOut);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
echo 'will be saved encrypted: ', ($jsonOut->BoolOf('pending.encrypted') ? 'true' : 'false'), "\n";
try {
$pdf->WriteFile('qa_output/minutes_unprotected.pdf');
// The result is an ordinary PDF that opens without a password.
$reader = new Pdf();
$info = new JsonObject();
$reader->PreLoadCheck('qa_output/minutes_unprotected.pdf', $info);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
echo 'encrypted: ', ($info->BoolOf('encrypted') ? 'true' : 'false'), "\n";
try {
$reader->LoadFile('qa_output/minutes_unprotected.pdf');
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
echo 'pages: ', $reader->NumPages, "\n";
// Sample output:
// encrypted, as loaded: True
// algorithm: rc4-128
// will be saved encrypted: False
// encrypted: False
// pages: 1