mPDF Manual – mPDF functions

useTemplate() (since 8.0)

(mPDF ≥ 8.0)

useTemplate – Insert an imported page from an external PDF file

Description

array useTemplate ( int $templateID [, float $x [, float $y [, float $width [, float $height ]]]])

Insert an imported page/template from an external PDF file into the current document. The page, or ‘cropped’ page, must have already been stored as a ‘template’ using setSourceFile(). The template is inserted on the current page of the document. useTemplate() returns an array of height and width of the imported page as it is printed (see Example #1).

Parameters

$templateID

This parameter specifies the ID of the page template to insert.

$x

Sets the $x co-ordinate (abscissa) to output the template. Value should be specified as LENGTH in millimetres.

Default: null - Sets $x co-ordinate to 0

BLANK or omitted - uses default (0)

-1: Uses current writing position in document

$y

Sets the $y co-ordinate (ordinate) to output the template. Value should be specified as LENGTH in millimetres.

Default: null - Sets $y co-ordinate to 0

BLANK or omitted - uses default (0)

-1: Uses current writing position in document

$width

Specifies the width for the template to appear on the page. Value should be specified as LENGTH in millimetres.

Default or 0 will output the template at the original size (if neither $width nor $height are set) or if $height is set, the $width is automatically set to output the template in proportion to the original.

$height

Specifies the height for the template to appear on the page. Value should be specified as LENGTH in millimetres.

Default or 0 will output the template at the original size (if neither $width nor $height are set) or if $width is set, the $height is automatically set to output the template in proportion to the original.

Return Value

useTemplate() returns an array of the calculated $width and $height.

Changelog

Version Description
8.0 Function was added.

Examples

Example #1

<?php

// Require composer autoload
require_once __DIR__ . '/vendor/autoload.php';

$mpdf = new \Mpdf\Mpdf();

// Add First page
$mpdf->AddPage();

$pagecount = $mpdf->setSourceFile('logoheader.pdf');
$tplId = $mpdf->importPage($pagecount);

$actualsize = $mpdf->useTemplate($tplId);

// The height of the template as it was printed is returned as $actualsize['h']
// The width of the template as it was printed is returned as $actualsize['w']

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

$mpdf->Output();

Example #2 - Using a ‘cropped’ page

<?php

// Require composer autoload
require_once __DIR__ . '/vendor/autoload.php';

$mpdf = new \Mpdf\Mpdf();

$pagecount = $mpdf->setSourceFile('testfile.pdf');
$tplId = $mpdf->importPage($pagecount);

$mpdf->useTemplate($tplId, 50, 50, 100, 100);

$mpdf->Output();

See Also

Fork me on GitHub