How to Remove PDF Restrictions with PHP

Learn how to remove restrictions from PDFs to enable printing, editing content and form fields, copying text, and more with the Restrict PDF API Tool.
Share this page

Why Remove PDF Restrictions with PHP?

The pdfRest Restrict PDF API Tool provides a powerful way to apply or remove restrictions on PDF documents programmatically. This tutorial will demonstrate how to make an API call to the /unrestricted-pdf endpoint using PHP, which is a common server-side scripting language used in web development. By integrating this functionality into your PHP application, you can automate the process of managing PDF permissions without manual intervention.

Consider a scenario where a company needs to distribute sensitive reports to its employees but must ensure that these documents cannot be edited or printed. Using the Restrict PDF API, they can programmatically enforce these restrictions before distribution. Conversely, if the company later decides to lift these restrictions for a certain group of users, they can use the /unrestricted-pdf endpoint of the same API to remove the restrictions, making the documents fully accessible again.

Removing PDF Restrictions with PHP Code Example

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

$options = [
  'multipart' => [
    [
      'name' => 'file',
      'contents' => Utils::tryFopen('/path/to/file.pdf', 'r'),
      'filename' => '/path/to/file',
      'headers' => [
        'Content-Type' => ''
      ]
    ],
    [
      'name' => 'current_permissions_password',
      'contents' => 'current_example_pw'
    ],
    [
      'name' => 'output',
      'contents' => 'pdfrest_unrestricted_pdf'
    ]
  ]
];

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

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

echo $res->getBody();
?>

This code is sourced from the pdf-rest-api-samples repository on GitHub.

Breaking Down the Code

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

require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Utils;
$client = new Client();

This section includes the necessary classes and initializes the Guzzle HTTP client.

$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 array elements...
  ]
]

The options array is where the multipart form data is constructed. Each element of the multipart array represents a different part of the form data:

  • 'name' => 'file': Indicates the form field name for the file.
  • 'contents': Uses Utils::tryFopen to open the file for reading.
  • 'filename': Specifies the path to the file being uploaded.
  • 'headers': Contains the Content-Type header for the file, which should match the file's MIME type.
  • 'name' => 'current_permissions_password': The password for the current permissions (if the PDF is already restricted).
  • 'name' => 'output': The desired name for the output file.
$request = new Request('POST', 'https://api.pdfrest.com/unrestricted-pdf', $headers);
$res = $client->sendAsync($request, $options)->wait();
echo $res->getBody();

A new Request object is created for the POST method to the Restrict PDF endpoint. The asynchronous request is sent, and the script waits for the response. The response body, which contains the unrestricted PDF, is then outputted.

Beyond the Tutorial

By following the steps in this tutorial, you've learned how to call the pdfRest Restrict PDF API using PHP to remove restrictions from a PDF document. This can be a valuable tool for automating document workflows and managing document security within your applications.

To further explore what you can do with pdfRest API Tools, you can demo all of them in the API Lab. For more detailed information on the API, refer to the API Reference Guide.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at the pdf-rest-api-samples repository on 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