【发布时间】:2014-01-17 12:06:18
【问题描述】:
我在 PHP 中使用mpdf 库从 HTML 创建一个 pdf 文件。我需要将页面模式设置为landscape 模式。
这是我正在使用的代码:
$mpdf=new mPDF('c');
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;
但是,这是将页面模式设置为portrait 模式。任何想法,如何在 mpdf 中设置横向模式?
【问题讨论】:
我在 PHP 中使用mpdf 库从 HTML 创建一个 pdf 文件。我需要将页面模式设置为landscape 模式。
这是我正在使用的代码:
$mpdf=new mPDF('c');
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;
但是,这是将页面模式设置为portrait 模式。任何想法,如何在 mpdf 中设置横向模式?
【问题讨论】:
这可能对你有用。
最后一个参数是方向。
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:默认纵向
L:横向
“-L”强制横向页面方向
// 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');
【讨论】:
您可以通过在页面格式中添加 -L 来做到这一点。所以在我们的例子中,你需要在你的构造函数中添加另一个参数:
$mpdf = new mPDF('c', 'A4-L');
有关 mPDF 构造函数参数的更多信息可以找到 here(死链接)。
【讨论】:
改变方向的最好方法是传递一个带参数的数组。
这个变量被传递给构造函数并被称为$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']);
【讨论】:
在 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();
【讨论】:
在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
);
【讨论】:
添加这样的选项:
$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 constructor 的文档。
$mpdf=new mPDF('c', 'A4-L');
【讨论】:
'c'时,我使用的自定义字体都不会显示,而是pdf文件中的字体只是一种普通字体(我猜是Times New Roman)。这是为什么?
你好去这里找到那个。 AddPage() have the parameter to set that....
$mpdf->AddPage('L',.....);
【讨论】: