mPDF Manual – mPDF functions

SetWatermarkImage()

(mPDF ≥ 2.2)

SetWatermarkImage – Set an image to use as a Watermark

Description

void SetWatermarkImage ( string|\Mpdf\WatermarkImage $src [, float $alpha [, mixed $size [, mixed $position ]]] )

Set an image to use as a Watermark. The watermark is a semi-transparent background printed on each page, used for text such as “DRAFT” or a background image. The watermark will be added to each page when the Footer is printed if the variable $showWatermarkImage is set to a truthy value. 

Parameters

$src

This parameter specifies the image file to use for the watermark. This can be a full URI or use a relative path.

Since 8.2.0 the value can be an instance of \Mpdf\WatermarkImage class. Its properties will then overwrite internal watermark variables.

$alpha

This parameter defines the transparency value (alpha) to use for the watermark. The Value should be between 0 and 1.

Default: 0.2

$size

Defines the size of the watermark. This parameter takes either a pre-defined string, an integer, or an array of width and height.

Values

  • 'D'
    default i.e. original size of image - may depend on img_dpi
  • 'P'
    Resize to fit the full page size, keeping aspect ratio
  • 'F'
    Resize to fit the print-area (frame) respecting current page margins, keeping aspect ratio
  • INT
    Resize to full page size minus a margin set by this integer in millimeters, keeping aspect ratio
  • array($width, $height)
    Specify a size; units in millimeters

Default: 'D'

$position

Defines the position of the watermark on the page. This parameter takes either a pre-defined string or an array of $x and $y.

Values

  • 'P': Centred on the whole page area
  • 'F': Centred on the page print-area (frame) respecting page margins
  • array($x, $y): Specify a position; units in millimeters

Default: 'P'

Changelog

Version Description
2.2 The function was added.
8.2.0 \Mpdf\WatermarkImage object was introduced.

Examples

Example #1 (Since mPDF 8.2.0)

<?php
$mpdf = new \Mpdf\Mpdf();

$mpdf->SetWatermarkImage(new \Mpdf\WatermarkImage('../images/background.jpg'));
$mpdf->showWatermarkImage = true;

Example #2

<?php
$mpdf = new \Mpdf\Mpdf();

$mpdf->SetWatermarkImage('../images/background.jpg');
$mpdf->showWatermarkImage = true;

$mpdf->WriteHTML('Hello World');

Example #3 - Using a Watermark as a Header

<?php
$mpdf = new \Mpdf\Mpdf([
	'margin_left' => 20,
	'margin_right' => 20,
	'margin_top' => 50,
	'margin_bottom' => 10
]);

// Setting transparency to 1, and exact positioning, you can use a Watermark Image
// as a 'Header'. Note that the page top-margin is set to accomodate the image.
$mpdf->SetWatermarkImage(
    'http://www.yourdomain.com/images/logo.jpg',
    1,
    '',
    [160, 10]
);
$mpdf->showWatermarkImage = true;

$mpdf->WriteHTML('Hello World');

See Also

Fork me on GitHub