How to Apply TDM Reservations to PDF with PHP

Learn how to protect documents from AI data mining using pdfRest TDM Reserve PDF API with PHP.
Share this page

Why Use TDM Reserve PDF with PHP?

The pdfRest TDM Reserve PDF API Tool is an essential resource for PHP developers looking to defend their digital content against unauthorized text and data mining (TDM). This tutorial will show you how to execute an API call to the TDM Reserve PDF endpoint using PHP. By incorporating this code into your server-side scripts, you can effortlessly embed standardized metadata into your PDF files, explicitly opting them out of AI training datasets and automated web scraping.

Consider a media organization operating a PHP-based Content Management System (CMS) to distribute investigative journalism and exclusive reports. To ensure their original work is not freely harvested by AI companies, developers can integrate the TDM Reserve PDF API directly into the CMS upload workflow. Whenever a new PDF is published to the site, the script automatically applies a machine-readable TDM policy, safeguarding the organization's copyright and ensuring compliance with modern digital rights regulations like the EU CDSM Directive.

TDM Reserve PDF with PHP Code Example

require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client.

use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace.
use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class.
use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams.

// By default, we use the US-based API service. This is the primary endpoint for global use.
$apiUrl = "https://api.pdfrest.com";

/* For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
 * For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
 */
//$apiUrl = "https://eu-api.pdfrest.com";

$client = new Client(); // Create a new instance of the Guzzle HTTP client.

$headers = [
  'Api-Key' =--> 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' // Set the API key in the headers for authentication.
];

$options = [
  'multipart' => [
    [
      'name' => 'file', // Specify the field name for the file.
      'contents' => Utils::tryFopen('/path/to/file', 'r'), // Open the file specified by the '/path/to/file' for reading.
      'filename' => '/path/to/file', // Set the filename for the file to be processed, in this case, '/path/to/file'.
      'headers' => [
        'Content-Type' => '' // Set the Content-Type header for the file.
      ]
    ],
    [
      'name' => 'policy', // Specify the field name for the policy string.
      'contents' => 'https://example.com/tdm-policy' // Set a dummy URL policy value.
    ],
    [
      'name' => 'output', // Specify the field name for the output option.
      'contents' => 'pdfrest_tdm_reserved_pdf' // Set the value for the output option.
    ]
  ]
];

$request = new Request('POST', $apiUrl.'/tdm-reserved-pdf', $headers); // Create a new HTTP POST request with the API endpoint and headers.

$res = $client->sendAsync($request, $options)->wait(); // Send the asynchronous request and wait for the response.

echo $res->getBody(); // Output the response body, which contains the PDF with TDM rights metadata.

Source: GitHub Repository

Breaking Down the Code

First, the code includes the Guzzle HTTP client library using require 'vendor/autoload.php';. This allows us to use Guzzle for making HTTP requests.

The use statements import necessary classes from the Guzzle library: Client for creating HTTP clients, Request for HTTP requests, and Utils for working with streams.

The $apiUrl variable sets the API endpoint URL. By default, it points to the US-based service, but there's an option to switch to the EU-based service for GDPR compliance by uncommenting the alternative URL.

We create a new Guzzle client instance with $client = new Client();.

The $headers array includes the API key for authentication. Replace 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' with your actual API key.

The $options array configures the multipart request. It includes fields for the file, policy, and output options:

  • file: Specifies the file to be processed. The Utils::tryFopen() function opens the file for reading.
  • policy: Contains a URL to the TDM policy, which defines the rights and permissions for text and data mining.
  • output: Sets the output format, in this case, 'pdfrest_tdm_reserved_pdf'.

A new HTTP POST request is created with $request = new Request('POST', $apiUrl.'/tdm-reserved-pdf', $headers);.

The request is sent asynchronously using $client->sendAsync($request, $options)->wait();, and the response body is outputted with echo $res->getBody();.

Beyond the Tutorial

In this tutorial, we demonstrated how to use PHP to send an API request to the pdfRest TDM Reserve PDF endpoint, allowing you to embed TDM rights metadata into your PDF files. This capability is crucial for ensuring compliance with TDM rights management policies.

To explore more features and tools offered by pdfRest, visit the API Lab for demos. For detailed information, 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 GitHub Repository.

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