mPDF Manual – mPDF functions

__construct()

(mPDF >= 7.0)

@todo

__construct – Initialise an instance of mPDF class

Description

\Mpdf\Mpdf::__construct([array $config = []])

Initialise an instance of mPDF class.

Parameters

$config

This parameter specifies configuration of the new document.

  • Apart from configuration variables defined in ConfigVariables and FontVariables classes it can obtain variables which where the arguments from the constructor of mPDF < 7.0: mode, format, default_font_size, default_font, margin_left, margin_right, margin_top, margin_bottom, margin_header, margin_footer, orientation. These variables are described with their defaults in the next section.

  • The other configuration keys are listed at the configuration overview.

Variables with defaults from constructor

mode

Mode of the document. Is parsed from values of various formats:

  • Codepage Values (case-insensitive)

    'c'
    '...-x'
    '...-s' or 's'
    '...+aCJK' or '+aCJK'
    '...-aCJK' or '-aCJK'

    where ... can be any string. Only language/country codes will have any effect, but other strings are parsed for backwards compatability (but have no effect).

    Only some combinations make sense. See Choosing a configuration for more details.

  • Country/Language code values (case-insensitive)

    Country/language codes are defined in \Mpdf\LangToFont class

    A country/language code can be passed as e.g. 'en-GB' or 'en_GB' or 'en'

format

Can be specified either as a pre-defined page size, or as an array of width and height in millimetres (see Example #2 below).

Default value: 'A4'

Values (case-insensitive)

  • 'A0’ - 'A10', 'B0' - 'B10', 'C0' - 'C10'
  • '4A0', '2A0', 'RA0' - 'RA4', 'SRA0' - 'SRA4'
  • 'Letter', 'Legal', 'Executive', 'Folio'
  • 'Demy', 'Royal'
  • 'A' (Type A paperback 111x178mm)
  • 'B' (Type B paperback 128x198mm)
  • 'Ledger'*, 'Tabloid'*

All of the above values can be suffixed with '-L' to force a Landscape page orientation document e.g. 'A4-L'.

If format is defined as a string, the orientation parameter will be ignored.

* Ledger and Tabloid are standard formats with the same page size but different orientation (Ledger is landscape, and Tabloid is portrait). Prior to mPDF 6.1, mPDF treats these identically; if you wished to use Ledger, you should have specified 'Ledger-L' for landscape.

default_font_size

Sets the default document font size in points (pt)

BLANK or omitted or 0 uses the default value set in defaultCSS configuration variable for the font-size of the BODY.

default_font

Sets the default font-family for the new document.

BLANK or omitted uses default value set in defaultCSS for the font-family of BODY unless codepage has been set to 'win-1252' (e.g. by using 'mode' => 'c'). If 'codepage' => "win-1252", the appropriate core Adobe font will be set i.e. Helvetica, Times, or Courier.

margin_left
margin_right
margin_top
margin_bottom
margin_header
margin_footer

Set the page margins for the new document.

All values should be specified as LENGTH in millimetres.

If you are creating a DOUBLE-SIDED document, the margin values specified will be used for ODD pages; left and right margins will be mirrored for EVEN pages.

BLANK or omitted uses the default values.

Default values:

  • margin_left: 15
  • margin_right: 15
  • margin_top: 16
  • margin_bottom: 16
  • margin_header: 9
  • margin_footer: 9
orientation

This attribute specifies the default page orientation of the new document if format is defined as an array. This value will be ignored if format is a string value.

Default value: 'P'

Values (case-insensitive)

  • 'P': Portrait
  • 'L': Landscape

Changelog

Version Description
6.1 Introduced to support higher versions of PHP
7.0

Parameters replaced with single $config parameter array

Examples

Example #1

<?php

// Require composer autoload
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();

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

$mpdf->Output('filename.pdf');

Example #2

<?php
// Define a new \Mpdf\Mpdf document using utf-8 fonts
$mpdf = new \Mpdf\Mpdf(['mode' => 'utf-8']);

// Define a new \Mpdf\Mpdf document using win-1252 fonts based on a language/country code
$mpdf = new \Mpdf\Mpdf(['mode' => 'en-GB']);

// Define a Landscape page size/format by name
$mpdf = new \Mpdf\Mpdf(['mode' => 'utf-8', 'format' => 'A4-L']);

// Define a page size/format by array - page will be 190mm wide x 236mm height
$mpdf = new \Mpdf\Mpdf(['mode' => 'utf-8', 'format' => [190, 236]]);

// Define a page using all default values except "L" for Landscape orientation
$mpdf = new \Mpdf\Mpdf(['orientation' => 'L']);

Notes

See Also

Fork me on GitHub