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).
Note: The template will be printed onto the page as the bottom ‘layer’ i.e.
anything else written to that page by mPDF will be written on top of the template. NB If you use WriteHTML()
and have
a background-color set on BODY this will hide the template from view e.g.
<body style="background-color:#FFFFFF;">
Note: If you are using automatic header-margins, you need to set the header before starting the first page; if you start
the document with useTemplate()
this will move it to page 1, so the order needs to be:
<?php
$mpdf = new \Mpdf\Mpdf();
$mpdf->SetHTMLHeader($header);
$pagecount = $mpdf->setSourceFile('logoheader.pdf');
$tplIdx = $mpdf->importPage($pagecount);
$mpdf->useTemplate($tplIdx);
$mpdf->WriteHTML($html);
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 to0
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 to0
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
- Thumbnail() - Print thumbnails of an external PDF file
- setSourceFile() - Specify the source PDF file used to import pages into the document
- importPage() - Import a page from an external PDF file
- SetPageTemplate() - Specify a page from an external PDF file to use as a template
- SetDocTemplate() - Specify an external PDF file to use as a template
- RestartDocTemplate() - Re-start the use of a Document template from the next page