mPDF Manual – mPDF functions

UseTemplate() (until 8.0)

(mPDF ≥ 2.3 && 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
2.3 Function was added.
8.0 Function was removed in favour of useTemplate().

Examples

Example #1

<?php

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

$mpdf = new \Mpdf\Mpdf();
$mpdf->SetImportUse(); // only with mPDF <8.0

// 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();
$mpdf->SetImportUse(); // only with mPDF <8.0

$pagecount = $mpdf->SetSourceFile('testfile.pdf');
$tplId = $mpdf->ImportPage($pagecount, 50, 50, 100, 100);
$mpdf->UseTemplate($tplId, '', '', 100, 100);

$mpdf->Output();

See Also

Fork me on GitHub