How to Watermark PDF with PHP

Learn how to apply a watermark image to a PDF using PHP to call the Watermark PDF API Tool from pdfRest.
Share this page

Why Watermark PDF with PHP?

The pdfRest Watermark PDF API Tool allows developers to easily add watermarks to PDF documents programmatically. This tutorial will guide you through making an API call to the Watermark PDF endpoint using PHP.

Watermarking PDFs can be crucial for branding, protecting copyright, or indicating the status of a document. For instance, a legal firm might watermark drafts as "Confidential" before sharing them with clients.

Watermark PDF with PHP Code Example

require 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Utils;

$client = new Client();

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

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

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

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

echo $res->getBody();

Source of the provided code: GitHub

Breaking Down the PHP Code

The code begins by loading required dependencies using Composer's autoload feature. It then imports the necessary classes from the GuzzleHttp library.

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

An instance of the Guzzle HTTP client is created, and the API key is set in the headers for authentication.

$client = new Client();
$headers = [
  'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
];

The options array is constructed with the 'multipart' key containing the file, watermark text, and output options.

$options = [
  'multipart' => [
    // ... file and watermark_text details ...
    [
      'name' => 'output',
      'contents' => 'pdfrest_watermarked_pdf'
    ]
  ]
];

Each field in the multipart form data is detailed, with 'name' being the field name, 'contents' being the value or file stream, and 'headers' for any additional headers needed for the file part.

A POST request is then created with the specified endpoint and headers, and the request is sent asynchronously. The response is waited on and the body of the response, which should be the watermarked PDF, is echoed out.

$request = new Request('POST', 'https://api.pdfrest.com/watermarked-pdf', $headers);
$res = $client->sendAsync($request, $options)->wait();
echo $res->getBody();

Beyond the Tutorial

This tutorial demonstrated how to call the pdfRest Watermark PDF API using PHP to watermark a PDF document. The code example provided shows how to construct and send a multipart/form-data request using the Guzzle HTTP client.

To further explore the capabilities of pdfRest API Tools, visit the API Lab. For more detailed information about the API endpoints and available options, refer to the API Reference documentation.

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.