php - Mpdf : Set 0 margin for page 1 only

标签 php mpdf

$mpdf = new \Mpdf\Mpdf([
        'tempDir' => __DIR__ . '/temp'
    ]);
$mpdf->SetMargins(0, 0, 0); // will set it to 0 for all pages.

是否可以为 PDF 页面的第 1 页设置 0 页边距,并为文档的其余页面设置默认页边距?

我目前正在使用 7.0 版对此进行测试。

最佳答案

如果不需要第1页和其他页面的自动内容溢出,可以使用 AddPageByArray() 方法:

$mpdf = new \Mpdf\Mpdf([]);

$mpdf->AddPageByArray([
    'margin-left' => 0,
    'margin-right' => 0,
    'margin-top' => 0,
    'margin-bottom' => 0,
]);

$mpdf->WriteHTML($html1); // first page

$mpdf->AddPageByArray([
    'margin-left' => '15mm',
    'margin-right' => '20mm',
    'margin-top' => '15mm',
    'margin-bottom' => '15mm',
]);

$mpdf->WriteHTML($html2); // other pages

// All other pages will then have margins of the second `AddPageByArray()` call.

在内容从第一页溢出的情况下,下一个自动创建的页面也将具有零边距。

或者,您可以在构造函数中设置零边距并使用 <pagebreak> 重置后续页面的边距。伪 HTML 标签:
$mpdf = new \Mpdf\Mpdf([
    'margin_left' => 0,
    'margin_right' => 0,
    'margin_top' => 0,
    'margin_bottom' => 0,
]);

$html = 'Content of the first page
    <pagebreak margin-left="15mm" margin-right="15mm" margin-top="15mm" margin-bottom="20mm">
 Other content';   

$mpdf->WriteHTML($html1);

关于php - Mpdf : Set 0 margin for page 1 only,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49920397/

相关文章:

php - 使用 mPDF 错误 :Class 'Mpdf\Mpdf' not found 在 codeigniter 3 中将 html 导出和下载为 pdf

jquery - 在 codeigniter 中使用 ajax 调用 mpdf 生成 PDF

php - 如何将多个 View 加载到 mpdf

php - 为什么使用 mPDF 合并 PDF 文件后超链接丢失?

PHP 上传问题

php - 如何在序列中的同一个插入件上插入序号?

php - 使用 php 将 postgres 输出写入文件

java - 使用ajax保存单击时单选按钮的值

php - 如何在 Yii 中使用 Markdown 类

codeigniter - 使用 mpdf 将 codeigniter View 文件保存为 pdf 文件