Kiran
Kiran

Reputation: 8518

How to set the page in landscape mode in mpdf?

I am using mpdf library in PHP to create a pdf file from HTML. I need to set the page mode in landscape mode.

Here is the code I am using :

$mpdf=new mPDF('c'); 

$mpdf->WriteHTML($html);
$mpdf->Output();
exit;

However, this is setting the page mode in portrait mode. Any idea, how to set the landscape mode in mpdf ?

Upvotes: 14

Views: 73631

Answers (9)

H. M..
H. M..

Reputation: 659

As of October 2022, as documented in Mpdf github page, it suffices to add mode and format array parameters, or simply orientation.

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

// Define a default 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 default page using all default values except "L" for Landscape orientation
$mpdf = new \Mpdf\Mpdf(['orientation' => 'L']);

Go to the link

Upvotes: 0

RaviRokkam
RaviRokkam

Reputation: 789

This may be useful for you.

The last Parameter is orientation.

class mPDF ([ string $mode [, mixed $format [, float $default_font_size [, string $default_font [, float $margin_left , float $margin_right , float $margin_top , float $margin_bottom , float $margin_header , float $margin_footer [, string $orientation ]]]]]])

P: DEFAULT Portrait

L: Landscape

"-L" to force a Landscape page orientation

// Define a Landscape page size/format by name
$mpdf=new mPDF('utf-8', 'A4-L');

// Define a page using all default values except "L" for Landscape orientation
$mpdf=new mPDF('','', 0, '', 15, 15, 16, 16, 9, 9, 'L');

You can dig more into it here here

Upvotes: 9

MaGnetas
MaGnetas

Reputation: 5008

You can do that by adding -L to your page format. So in our case you'd add another param to your constructor:

$mpdf = new mPDF('c', 'A4-L'); 

More about mPDF constructor params can be found here(deadlink).

Upvotes: 36

Neo Morina
Neo Morina

Reputation: 401

The best way to change the orientation is to pass an array with arguments.

This variable gets passed to the constructor and is called $config

public function __construct(array $config = []){ }

Down below are the default configurations of the Mpdf

$default_config= [
                'mode' => '',
                'format' => 'A4',
                'default_font_size' => 0,
                'default_font' => '',
                'margin_left' => 15,
                'margin_right' => 15,
                'margin_top' => 16,
                'margin_bottom' => 16,
                'margin_header' => 9,
                'margin_footer' => 9,
                'orientation' => 'P',
            ];

To change the orientation from Portrait to Landscape just change the "orientation" parameter as it is written below.

$mpdf = new Mpdf(['orientation' => 'L']);

Upvotes: 1

user8376095
user8376095

Reputation: 1

In mPDF version 7.2.1 works form me :

$mpdf = new \Mpdf\Mpdf(array('', '', 0, '', 15, 15, 16, 16, 9, 9, 'L'));

$mpdf->WriteHTML('<p>This is just a <strong>test</strong>, This is just a <strong>test</strong></p>');
$mpdf->Output();

Upvotes: 0

lin
lin

Reputation: 18392

In mPDF version 7.0.0 or later the configuration need to be parsed as array[]:

$myMpdf = new Mpdf([
    'mode' => 'utf-8',
    'format' => 'A4-L',
    'orientation' => 'L'
]

In older version before version 7.0.0. it need to be done like this:

myMpdf = new mPDF(
    '',    // mode - default ''
    'A4-L',    // format - A4, for example, default ''
    0,     // font size - default 0
    '',    // default font family
    15,    // margin_left
    15,    // margin right
    16,    // margin top
    16,    // margin bottom
    9,     // margin header
    9,     // margin footer
    'L'    // L - landscape, P - portrait
);

Upvotes: 13

Mazeltov
Mazeltov

Reputation: 551

add options like this:

 $mpdf = new mPDF('',    // mode - default ''
 '',    // format - A4, for example, default ''
 0,     // font size - default 0
 '',    // default font family
 15,    // margin_left
 15,    // margin right
 16,     // margin top
 16,    // margin bottom
 9,     // margin header
 9,     // margin footer
 'L');  // L - landscape, P - portrait

Upvotes: 6

aebersold
aebersold

Reputation: 11506

Check the docs for the mPDF constructor.

$mpdf=new mPDF('c', 'A4-L'); 

Upvotes: 8

Sibiraj PR
Sibiraj PR

Reputation: 1481

Hi go for here to find that. AddPage() have the parameter to set that....

$mpdf->AddPage('L',.....);

Upvotes: 0

Related Questions