如何在mpdf中以横向模式设置页面?

问题描述 投票:9回答:8

我在PHP中使用mpdf库从HTML创建pdf文件。我需要在landscape模式下设置页面模式。

这是我正在使用的代码:

$mpdf=new mPDF('c'); 

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

但是,这是在portrait模式下设置页面模式。任何想法,如何在mpdf中设置横向模式?

php mpdf
8个回答
30
投票

您可以通过在页面格式中添加-L来实现。所以在我们的例子中你要为你的构造函数添加另一个参数:

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

有关mPDF构造函数参数的更多信息,请参见 here (Deadlink中)。


8
投票

这可能对您有用。

最后一个参数是方向。

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肖像

L:风景

“-L”强制执行Landscape页面方向

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

你可以挖掘更多here


7
投票

查看mPDF constructor的文档。

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

5
投票

添加这样的选项:

 $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

3
投票

在mPDF 7.0.0或更高版本中,配置需要解析为array []:

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

在7.0.0之前的旧版本中。它需要这样做:

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
);

1
投票

更改方向的最佳方法是传递带参数的数组。

此变量将传递给构造函数,并称为$config

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

下面是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',
            ];

要将方向从纵向更改为横向,只需更改“方向”参数,如下所示。

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

0
投票

你好去这里找到。 AddPage() have the parameter to set that....

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

0
投票

在mPDF版本7.2.1中,作品形成了我:

$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();
© www.soinside.com 2019 - 2024. All rights reserved.