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

Restrict Printing, Copying and Editing of a PDF

See more PDF Protection Examples

Demonstrates a PDF that opens without a password but limits what a reader may do with it. The user password is left empty, the owner password protects the restrictions, and the permissions member of the JSON says what is denied. Chilkat honors these permissions itself: with copying denied, text extraction is refused unless the PDF is loaded with its owner password. An empty owner password is refused when anything is restricted, because anyone opening the file would then have owner rights.

Chilkat PHP Downloads

Chilkat2 PHP
<?php

use Chilkat2\JsonObject;
use Chilkat2\Pdf;
use Chilkat2\StringBuilder;

// 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

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

    // No user password: the PDF opens without a prompt.  The owner password is what protects the
    // restrictions, so it must not be empty when anything is restricted.
    $pdf->UserPassword = '';
    $pdf->OwnerPassword = 'owner-secret';

    $json = new JsonObject();
    $json->UpdateString('permissions.print', 'lowQuality');
    $json->UpdateBool('permissions.copy', false);
    $json->UpdateBool('permissions.modify', false);
    $json->UpdateBool('permissions.annotate', false);
    // The permissions not mentioned (fillForms, assemble, accessibility) stay granted.

    $jsonOut = new JsonObject();
    $pdf->Protection('encrypt', $json, $jsonOut);

    $pdf->WriteFile('qa_output/memo_restricted.pdf');

    // Anyone can open the result, with the rights of a user.
    $reader = new Pdf();
    $reader->LoadFile('qa_output/memo_restricted.pdf');

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

echo 'opened with: ', $info->StringOf('loaded.openedWith'), "\n";
echo 'print: ', $info->StringOf('loaded.permissions.print'), "\n";
echo 'copy: ', ($info->BoolOf('loaded.permissions.copy') ? 'true' : 'false'), "\n";
echo 'modify: ', ($info->BoolOf('loaded.permissions.modify') ? 'true' : 'false'), "\n";

// Chilkat honors the restrictions.  Copying is not permitted, so the text cannot be extracted.
$opts = new JsonObject();
$extractOut = new JsonObject();
$sb = new StringBuilder();
try {
    $reader->ExtractTextSb('text', $opts, $sb, $extractOut);
} catch (\Chilkat2\Exception $e) {
    echo "Text extraction was refused: the PDF does not permit copying.\n";
}

// The owner password lifts every restriction.
$owner = new Pdf();
$owner->OwnerPassword = 'owner-secret';
try {
    $owner->LoadFile('qa_output/memo_restricted.pdf');

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

echo 'opened with: ', $ownerInfo->StringOf('loaded.openedWith'), "\n";
try {
    $owner->ExtractTextSb('text', $opts, $sb, $extractOut);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

echo 'characters extracted with the owner password: ', $sb->Length, "\n";

// Sample output:

// opened with: emptyPassword
// print: lowQuality
// copy: False
// modify: False
// Text extraction was refused: the PDF does not permit copying.
// opened with: owner
// characters extracted with the owner password: 1048