PHP mPDF 库无法正确编号偶数/奇数页

发布于 2024-10-21 01:54:22 字数 950 浏览 1 评论 0原文

我正在尝试用 PHP 生成 PDF。我发现 mPDF 通常效果很好。我遇到的一个问题是我需要能够为某些部分的偶数/奇数页面设置单独的标题。我认为该功能运行不正常。这是我的代码:

// load mPDF
// --------------------------------------------------------------------------
include('mpdf.php');
$mpdf = new mPDF();

// generate a lot of content so it spans multiple pages
// --------------------------------------------------------------------------
$i = 0;
$out = '';
while ($i < 300)
{
    $i++;
    $out .= '<p>Lorem Ipsum</p>';
}
// set html header for odd pages, write html and output
// --------------------------------------------------------------------------
$mpdf->SetHTMLHeader('ODD {PAGENO}', 'O');
$mpdf->WriteHTML($out);
$mpdf->Output();

当我运行此代码时,它会将“ODD Page 3”或它所在的任何页面...放在每个页面上。不仅仅是奇数页。出于偶数或奇数的目的,它似乎不会将自然损坏的页面视为新页面,而仅将手动损坏的页面视为新页面。如果我运行 AddPage() 函数,它将认为下一组是偶数页。但这可能是 50 次自动分页之后的事。

关于如何让它为实际偶数/奇数页设置不同的标题,而不仅仅是在手动分页符之后,有什么建议吗?

I'm trying to do some PDF generation in PHP. I found mPDF which generally has worked quite well. One problem I'm having though is that I need to be able to set separate headers for even/odd pages for some sections. I don't think the function is working right. Here's my code:

// load mPDF
// --------------------------------------------------------------------------
include('mpdf.php');
$mpdf = new mPDF();

// generate a lot of content so it spans multiple pages
// --------------------------------------------------------------------------
$i = 0;
$out = '';
while ($i < 300)
{
    $i++;
    $out .= '<p>Lorem Ipsum</p>';
}
// set html header for odd pages, write html and output
// --------------------------------------------------------------------------
$mpdf->SetHTMLHeader('ODD {PAGENO}', 'O');
$mpdf->WriteHTML($out);
$mpdf->Output();

When I run this code it puts "ODD Page 3" or whatever page it's on... on every page. Not just the odd pages. It seems to not consider naturally broken pages as new pages for the purposes of even or odd, only manually broken ones. If I run the AddPage() function it will consider the next group an even page. But that could be 50 auto page breaks later.

Any suggestions on how to get it to set different headers for actual even/odd pages, not just after manual page breaks?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情独悲 2024-10-28 01:54:22

您忘记写

$mpdf->mirrorMargins = 1;

这是您的工作示例

<?php
include("../mpdf.php");
$mpdf=new mPDF('utf-8','A4'); 
$mpdf->debug = true;
$mpdf->mirrorMargins = 1 ;// Use different Odd/Even headers and footers and mirror margins

// generate a lot of content so it spans multiple pages
// --------------------------------------------------------------------------
$i = 0;
$out = '';
while ($i < 300)
{
    $i++;
    $out .= '<p>Lorem Ipsum</p>';
}
// set html header for odd pages, write html and output
// --------------------------------------------------------------------------
$mpdf->SetHTMLHeader('{PAGENO}/{nb}', 'O',true);
/* Note: SetHTMLHeader() and SetHTMLFooter() without a side(2nd argument) 
       - sets ODD page header/footer only as default..so you can also write just 
$mpdf->SetHTMLHeader('{PAGENO}/{nb}'); */
$mpdf->WriteHTML($out);
$mpdf->Output();
exit;
?>

参考:

setHTMLHeader

your forget to write

$mpdf->mirrorMargins = 1;

here is your working example

<?php
include("../mpdf.php");
$mpdf=new mPDF('utf-8','A4'); 
$mpdf->debug = true;
$mpdf->mirrorMargins = 1 ;// Use different Odd/Even headers and footers and mirror margins

// generate a lot of content so it spans multiple pages
// --------------------------------------------------------------------------
$i = 0;
$out = '';
while ($i < 300)
{
    $i++;
    $out .= '<p>Lorem Ipsum</p>';
}
// set html header for odd pages, write html and output
// --------------------------------------------------------------------------
$mpdf->SetHTMLHeader('{PAGENO}/{nb}', 'O',true);
/* Note: SetHTMLHeader() and SetHTMLFooter() without a side(2nd argument) 
       - sets ODD page header/footer only as default..so you can also write just 
$mpdf->SetHTMLHeader('{PAGENO}/{nb}'); */
$mpdf->WriteHTML($out);
$mpdf->Output();
exit;
?>

Reference:

setHTMLHeader

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文