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.
// 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.
};