How to Change PDF Password with PHP

Learn how to change a PDF password using pdfRest Encrypt PDF API Tool with PHP
Share this page

Why Change PDF Password with PHP?

The pdfRest Encrypt PDF API Tool lets you update passwords for PDF documents directly with PHP. This eliminates the hassle of manually re-encrypting documents and ensures both enhanced security and streamlined collaboration.

Whether you're strengthening passwords, granting access to new team members, or complying with regulations, the Encrypt PDF API automates the process, saving you time and minimizing errors.

Change PDF Password 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' => 'current_open_password',
      'contents' => 'curr_example_pw'
    ],
    [
      '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 Code

The PHP code provided above is designed to make an API call to the Encrypt PDF endpoint of the pdfRest API. Let's break down the key components of this code:

require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Utils;

This section includes the necessary libraries and sets up the namespaces. Guzzle HTTP client is used for making HTTP requests.

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

The $headers array contains the API key required for authentication with the pdfRest API.

'options' => [
  'multipart' => [
    // ...multipart form data...
  ]
];

The $options array is where the multipart form data is defined. This includes the file to encrypt, the current password, the new password, and the output file name.

$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();
echo $res->getBody();

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

Beyond the Tutorial

In this tutorial, we've successfully made a call to the Encrypt PDF endpoint using the pdfRest API with PHP. By doing so, we've updated a PDF document by setting a new password, ensuring that only authorized individuals with the password can access the document's contents.

Demo all of the pdfRest API Tools in the API Lab and refer to the API Reference Guide for further exploration of the capabilities offered by pdfRest.

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