How to Restrict PDF with PHP

Learn how to use pdfRest Restrict PDF API Tool to apply PDF file restrictions with PHP
Share this page

Why Use Restrict PDF with PHP?

The pdfRest Restrict PDF API Tool is a powerful resource for developers looking to programmatically apply restrictions to PDF documents. This tutorial will guide you through the process of making an API call to the Restrict PDF tool using PHP.

By integrating this functionality into your applications, you can, for example, prevent unauthorized printing or copying of sensitive documents, ensuring that only intended recipients can interact with the content in predefined ways.

Restrict 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' => 'output',
      'contents' => 'pdfrest_restricted_pdf'
    ],
    [
      'name' => 'new_permissions_password',
      'contents' => 'new_example_pw'
    ],
    [
      'name' => 'restrictions[]',
      'contents' => 'print_low'
    ],
    [
      'name' => 'restrictions[]',
      'contents' => 'accessibility_off'
    ],
    [
      'name' => 'restrictions[]',
      'contents' => 'edit_content'
    ]
  ]
];

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

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

echo $res->getBody();
?>

Source: pdfRest API samples on GitHub

Breaking Down the Code

The code above demonstrates how to use the Guzzle HTTP client in PHP to make a multipart/form-data POST request to the pdfRest API to restrict a PDF file.

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

This snippet includes the necessary classes for making HTTP requests and handling file streams.

$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 fields...
  ]
];

The $options array defines the multipart form data, including the file to restrict, the desired output, and the restrictions to apply.

$request = new Request('POST', 'https://api.pdfrest.com/restricted-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 response body is outputted, which contains the restricted PDF.

Beyond the Tutorial

By following the steps above, you've learned how to use PHP to call the pdfRest Restrict PDF API to apply restrictions to a PDF document. We encourage you to demo all of the pdfRest API Tools in the API Lab and refer to the Cloud API Reference Guide for further exploration.

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

Generate a self-service API Key now!
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.