How to Encrypt PDF with PHP

Learn how to encrypt PDF documents using PHP to call the pdfRest Encrypt PDF API Tool.
Share this page

Why Encrypt PDF with PHP?

The pdfRest Encrypt PDF API Tool allows developers to secure PDF documents by encrypting them with a password. By using this API, you can ensure that sensitive information within a PDF is protected and can only be accessed by individuals with the correct password.

This tutorial will guide you through the process of sending an API call to Encrypt PDF with PHP. A real-world example of using Encrypt PDF might involve a company that needs to send confidential financial reports to its stakeholders. By encrypting the documents, the company can safeguard the data and maintain privacy.

Encrypt PDF with PHP Code Example

 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
];

$options = [
  'multipart' => [
    [
      'name' => 'file',
      'contents' => Utils::tryFopen('/path/to/file', 'r'),
      'filename' => '/path/to/file',
      'headers' => [
        'Content-Type' => ''
      ]
    ],
    [
      'name' => 'new_open_password',
      'contents' => 'new_example_pw'
    ],
    [
      'name' => 'output',
      'contents' => 'pdfrest_encrypted_pdf'
    ]
  ]
];

$request = new Request('POST', 'https://api.pdfrest.com/encrypted-pdf', $headers);

$res = $client->sendAsync($request, $options)->wait();

echo $res->getBody();
?>

Source of the provided code: GitHub

Breaking Down the PHP Code

The provided PHP code makes use of the Guzzle HTTP client to send a multipart/form-data request to the pdfRest Encrypt PDF API endpoint. Here's a breakdown of the key components:

require 'vendor/autoload.php';

This line includes the Composer autoload file, which is necessary for loading the Guzzle HTTP client and other dependencies.

$client = new Client();

Here, we instantiate a new Guzzle HTTP client object.

$headers = [
  'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
];

The 'Api-Key' header is required for authentication with the pdfRest API.

$options = [
  'multipart' => [
    // ... multipart form fields ...
  ]
];

The 'multipart' array within the '$options' array contains the form fields and files to be sent with the request:

  • 'file': The file to be encrypted.
  • 'new_open_password': The password to encrypt the PDF with.
  • 'output': The desired name for the output file.
$request = new Request('POST', 'https://api.pdfrest.com/encrypted-pdf', $headers);

A new HTTP POST request is created with the API endpoint and the headers.

$res = $client->sendAsync($request, $options)->wait();

The request is sent asynchronously, and the script waits for the response, which contains the encrypted PDF content.

Beyond the Tutorial

In this tutorial, we've successfully sent an API call to encrypt a PDF document using PHP. The encrypted PDF can now be securely shared, knowing that only individuals with the password can open it. I encourage you to demo all of the pdfRest API Tools in the API Lab at API Lab and refer to the API Reference documentation at API Reference Guide for more details.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at GitHub.

Generate a self-service API Key now!

Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.

Compare Plans
Contact Us