mPDF Manual – mPDF functions

UseTemplate()

(mPDF >= 2.3)

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 ouput 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 ouput the template in proportion to the original.

Return Value

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

Changelog

td>2.3</td>
VersionDescription
Function was added.

Examples

Example #1

<?php

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

$mpdf = new mPDF();

$mpdf->SetImportUse();

// 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('Hallo World');

$mpdf->Output();

?>
Example #2 - Using a 'cropped' page
<?php

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

$mpdf = new mPDF();

$mpdf->SetImportUse();

$pagecount = $mpdf->SetSourceFile('testfile.pdf');

$tplId = $mpdf->ImportPage($pagecount, 50, 50, 100, 100);

$mpdf->UseTemplate($tplId, '', '', 100, 100);

$mpdf->Output();

?>

See Also