我在PHP中使用mpdf
库从HTML创建pdf文件。我需要在landscape
模式下设置页面模式。
这是我正在使用的代码:
$mpdf=new mPDF('c');
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;
但是,这是在portrait
模式下设置页面模式。任何想法,如何在mpdf中设置横向模式?
您可以通过在页面格式中添加-L来实现。所以在我们的例子中你要为你的构造函数添加另一个参数:
$mpdf = new mPDF('c', 'A4-L');
有关mPDF构造函数参数的更多信息,请参见 here (Deadlink中)。
这可能对您有用。
最后一个参数是方向。
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
查看mPDF constructor的文档。
$mpdf=new mPDF('c', 'A4-L');
添加这样的选项:
$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
在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
);
更改方向的最佳方法是传递带参数的数组。
此变量将传递给构造函数,并称为$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']);
你好去这里找到。 AddPage() have the parameter to set that....
$mpdf->AddPage('L',.....);
在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();