How to Set Restricted Permissions on a PDF with PHP

Cloudmersive
2 min readFeb 21, 2023

--

The ability to tightly secure PDF documents is incredibly helpful — it’s one of the big reasons so many people rely on PDFs in the first place. If your application is processing PDF documents at scale, the below API can add significant security value: it will allow you to programmatically encrypt, password-protect and set restricted permissions on your PDFs in a single request. This includes options to choose your specific encryption and allow or disallow the following (using Boolean values): printing, document assembly, content extraction, form filling, editing, annotations, and degraded printing.

To implement this API, start by running the following command to install the SDK:

composer require cloudmersive/cloudmersive_document_convert_api_client

After that, complete your API call with the remaining code examples. To authorize your request, include a free-tier API key (get one by registering a free account here) in the $config line:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\EditPdfApi(


new GuzzleHttp\Client(),
$config
);
$owner_password = "owner_password_example"; // string | Password of a owner (creator/editor) of the PDF file (required)
$user_password = "user_password_example"; // string | Password of a user (reader) of the PDF file (optional)
$input_file = "/path/to/inputfile"; // \SplFileObject | Input file to perform the operation on.
$encryption_key_length = "encryption_key_length_example"; // string | Possible values are \"128\" (128-bit RC4 encryption) and \"256\" (256-bit AES encryption). Default is 256.
$allow_printing = true; // bool | Set to false to disable printing through DRM. Default is true.
$allow_document_assembly = true; // bool | Set to false to disable document assembly through DRM. Default is true.
$allow_content_extraction = true; // bool | Set to false to disable copying/extracting content out of the PDF through DRM. Default is true.
$allow_form_filling = true; // bool | Set to false to disable filling out form fields in the PDF through DRM. Default is true.
$allow_editing = true; // bool | Set to false to disable editing in the PDF through DRM (making the PDF read-only). Default is true.
$allow_annotations = true; // bool | Set to false to disable annotations and editing of annotations in the PDF through DRM. Default is true.
$allow_degraded_printing = true; // bool | Set to false to disable degraded printing of the PDF through DRM. Default is true.

try {
$result = $apiInstance->editPdfSetPermissions($owner_password, $user_password, $input_file, $encryption_key_length, $allow_printing, $allow_document_assembly, $allow_content_extraction, $allow_form_filling, $allow_editing, $allow_annotations, $allow_degraded_printing);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling EditPdfApi->editPdfSetPermissions: ', $e->getMessage(), PHP_EOL;
}
?>

You can easily customize your document security preferences via the request parameters above.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet