Encrypt and Password Protect a PDF in PHP
Apply encryption and password protection to PDF documents programmatically.
Content security is one of the primary benefits of publishing documents in PDF format. Once we’ve written any content to a PDF, we don’t want folks to get away with editing finalized versions of that content - and it certainly isn’t easy to make changes to a published PDF document.
Securing PDFs One Step Further
There are, however, situations where intrinsic PDF security isn’t enough. If we’re sharing highly sensitive information with a specific target audience, we don’t want unauthorized folks opening and viewing our PDF content whatsoever. In those cases, we’ll need to go a step further and bolster document security in other ways.
Applying encryption and password protection to a PDF document is a great way to make sure only a select audience can view it. It’s simple enough to do this manually in any PDF rendering & processing application; if we want to do so programmatically, however, we may find ourselves writing more code than we want to from scratch.
Thankfully, there’s no need to write a bunch of new code - we can use a simple, low-code API solution to handle our PDF encryption and password protection workflow. In this tutorial, we’ll learn how to call a PDF encryption and password protection API with ready-to-run PHP code examples, and we’ll solve our PDF security problem in minutes.
Let’s begin!
Step 1: Install the SDK with Composer
To get things started, we’re first going to run the following command to install the client SDK with Composer:
composer require cloudmersive/cloudmersive_document_convert_api_client
Step 2: Configuration
In our next step, we’re going to add in our configuration snippet and authorize our API calls with a Cloudmersive API key.
<?php
require_once(__DIR__ . '/vendor/autoload.php');
// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');
To get a Cloudmersive API key for free, we can simply head to the Cloudmersive website and create a free account. A free API key will give us a limit of 800 API calls per month with zero additional commitments (once we reach that limit, our total will reset the following month).
Step 3: Instance the API & Call the Function
In this last step of our tutorial, we’ll create an instance of the API in our PHP application, and we’ll call the PDF encryption and password protection function directly after.
$apiInstance = new Swagger\Client\Api\EditPdfApi(
new GuzzleHttp\Client(),
$config
);
$input_file = "/path/to/inputfile"; // \SplFileObject | Input file to perform the operation on.
$user_password = "user_password_example"; // string | Password of a user (reader) of the PDF file
$owner_password = "owner_password_example"; // string | Password of a owner (creator/editor) of the PDF file
$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.
try {
$result = $apiInstance->editPdfEncrypt($input_file, $user_password, $owner_password, $encryption_key_length);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling EditPdfApi->editPdfEncrypt: ', $e->getMessage(), PHP_EOL;
}
?>
In our request, we can supply a user password, an owner password, an encryption key length (default is 256-bit AES encryption; our other option is 128-bit RC4 encryption), and our PDF file path.
Conclusion
That’s all the code we need! We can now limit access to sensitive PDF content by applying powerful encryption and password protection measures in our PHP application.
Have any additional questions for us after this tutorial? Not a problem - feel free to contact us any time!