PDF Watermark PHP API

Nutrient DWS API is an HTTP API that lets you add watermarks to your documents. Use the PHP PDF Watermark API to add text or image watermarks to PDFs via a single API call.

Why Nutrient DWS API?

SOC 2 Compliant

Build the workflows you need without worrying about security. We don’t store any document data, and our API endpoints are served through encrypted connections.

Easy Integration

Get up and running in hours, not weeks. Access well-documented APIs and code samples that make integrating with your existing workflows a snap.

Robust and Flexible

With access to more than 30 tools, you can process one document in multiple ways by using API credits. Generate PDF from HTML, convert Word, Excel, PowerPoint and image files to PDF, and more.

Simple and Transparent Pricing

Select a package that suits your needs according to the number of credits you wish to spend. Each API tool and action has a specific credit cost.

Try It Out

This example will watermark all the pages of your document with the supplied logo.

1

Use Your Free API Calls

Sign up and receive 100 credits for free, or log in to automatically add your API key to sample code. If you are not sure how credits are consumed read more in our pricing documentation , or check out this guide on calculating credit usage.

2

Add a File

Add a PDF named document.pdf and an image file named logo.png to your project folder. You can also use our sample PDF and our sample image. The file name is case sensitive. Make sure the file name matches the file name in the sample code.

3

Run the Code

Copy the code and run it from the same folder you added the files to. For more information, see our language-specific getting started guides.

4

View the Results

Open result.pdf in your project folder to view the results.

<?php

$FileHandle = fopen('result.pdf', 'w+');

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.nutrient.io/build',
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_POSTFIELDS => array(
    'instructions' => '{
      "parts": [
        {
          "file": "document"
        }
      ],
      "actions": [
        {
          "type": "watermark",
          "image": "logo",
          "width": "25%"
        }
      ]
    }',
    'document' => new CURLFILE('document.pdf'),
    'logo' => new CURLFILE('logo.png')
  ),
  CURLOPT_HTTPHEADER => array(
    'Authorization: Bearer your_api_key_here'
  ),
  CURLOPT_FILE => $FileHandle,
));

$response = curl_exec($curl);

curl_close($curl);

fclose($FileHandle);

Using Postman? Download our official collection and start using the API with a single click. Read more 

Your API Key

Examples

The following sections show more examples of how the watermark API can be used.

Multiple Watermarks

This example will add multiple watermarks to the document. The image watermark will be placed in the center of the screen, while the text will be positioned in the bottom left corner. The text watermark's opacity and font attributes can also be customized.

<?php

$FileHandle = fopen('result.pdf', 'w+');

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.nutrient.io/build',
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_POSTFIELDS => array(
    'instructions' => '{
      "parts": [
        {
          "file": "document"
        }
      ],
      "actions": [
        {
          "type": "watermark",
          "image": "logo",
          "width": "50%"
        },
        {
          "type": "watermark",
          "text": "Property of Nutrient",
          "width": 250,
          "height": 200,
          "left": 0,
          "bottom": "100%",
          "opacity": 0.5,
          "rotation": 10,
          "fontSize": 50,
          "fontColor": "#FF0000",
          "fontStyle": [
            "italic",
            "bold"
          ],
          "fontFamily": "Helvetica"
        }
      ]
    }',
    'document' => new CURLFILE('document.pdf'),
    'logo' => new CURLFILE('logo.png')
  ),
  CURLOPT_HTTPHEADER => array(
    'Authorization: Bearer your_api_key_here'
  ),
  CURLOPT_FILE => $FileHandle,
));

$response = curl_exec($curl);

curl_close($curl);

fclose($FileHandle);

Single-Page Watermark

This example will watermark the last page of the document. To do so, declare a part consisting of all pages of the source document but the last one, and a part with the last page and an image watermark action.

<?php

$FileHandle = fopen('result.pdf', 'w+');

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.nutrient.io/build',
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_POSTFIELDS => array(
    'instructions' => '{
      "parts": [
        {
          "file": "document",
          "pages": {
            "end": -2
          }
        },
        {
          "file": "document",
          "pages": {
            "start": -1
          },
          "actions": [
            {
              "type": "watermark",
              "image": "logo",
              "width": "50%"
            }
          ]
        }
      ]
    }',
    'document' => new CURLFILE('document.pdf'),
    'logo' => new CURLFILE('logo.png')
  ),
  CURLOPT_HTTPHEADER => array(
    'Authorization: Bearer your_api_key_here'
  ),
  CURLOPT_FILE => $FileHandle,
));

$response = curl_exec($curl);

curl_close($curl);

fclose($FileHandle);

Aligning Watermarks

This example will show some common watermark alignments by adding a text watermark in each of the corners.

<?php

$FileHandle = fopen('result.pdf', 'w+');

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.nutrient.io/build',
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_POSTFIELDS => array(
    'instructions' => '{
      "parts": [
        {
          "file": "document"
        }
      ],
      "actions": [
        {
          "type": "watermark",
          "text": "Top Left",
          "width": 150,
          "height": 20,
          "left": 0,
          "top": 0
        },
        {
          "type": "watermark",
          "text": "Top Center",
          "width": 150,
          "height": 20,
          "top": 0
        },
        {
          "type": "watermark",
          "text": "Top Right",
          "width": 150,
          "height": 20,
          "right": "100%",
          "top": 0
        },
        {
          "type": "watermark",
          "text": "Center Left",
          "width": 150,
          "height": 20,
          "left": 0
        },
        {
          "type": "watermark",
          "text": "Center",
          "width": 150,
          "height": 20
        },
        {
          "type": "watermark",
          "text": "Center Right",
          "width": 150,
          "height": 20,
          "right": "100%"
        },
        {
          "type": "watermark",
          "text": "Bottom Left",
          "width": 150,
          "height": 20,
          "left": 0,
          "bottom": "100%"
        },
        {
          "type": "watermark",
          "text": "Bottom Center",
          "width": 150,
          "height": 20,
          "bottom": "100%"
        },
        {
          "type": "watermark",
          "text": "Bottom Right",
          "width": 150,
          "height": 20,
          "right": "100%",
          "bottom": "100%"
        }
      ]
    }',
    'document' => new CURLFILE('document.pdf')
  ),
  CURLOPT_HTTPHEADER => array(
    'Authorization: Bearer your_api_key_here'
  ),
  CURLOPT_FILE => $FileHandle,
));

$response = curl_exec($curl);

curl_close($curl);

fclose($FileHandle);

Reference

The following schema shows all the options and valid inputs for the watermark action.

Typescript
// The size of the watermark can either be a positive integer or a percentage (e.g. representing a ratio of the page dimension).
type Dimension = number | string;

// The position of the watermark can either be a non-negative integer or a percentage (e.g. representing a ratio of the page dimension).
type Position = number | string;

// Represents one part that was sent in the multipart request. Should be the
// `name` that was specified for the part.
type MultipartReference = string;

type WatermarkAction = {
  type: "watermark",

  // Only either "text" or "image" can be specified.
  text?: string, // When specified, this is the text that the watermark will contain.
  image?: MultipartReference, // When specified, this is the image that will be used as the watermark.

  // For images, one dimension needs to be specified. For text watermarks, both dimensions need to be specified.
  width?: Dimension,
  height?: Dimension,

  // The position defaults to the center of the page.
  // You can only specify one position per axis, so either:
  // top or bottom
  // and either
  // left or right.
  top?: Position,
  right?: Position,
  bottom?: Position,
  left?: Position,

  // The value of opacity should be between 0 and 1.
  opacity?: float,

  // Angle of rotation in degrees. Should be between 0 and 360.
  rotation?: integer,

  // Font and styling (Only valid for text watermarks)
  fontSize?: integer,
  fontColor?: string, // Font color as hex code. Example: "#FF0000".
  fontStyle?: string[], // Either ["italic", "bold"], or any one of them.
  fontFamily?: string, // String describing the font family.
};