TCPDF 中的手动分页符

发布于 2024-08-08 02:30:26 字数 200 浏览 7 评论 0 原文

我正在使用 TCPDF 在我的一个项目中生成 PDF。我只是创建一个 HTML 文件并将其交给 TCPDF 来处理 PDF 生成。但现在我有一些 HTML,其中多个证书被一个接一个地添加,并且我想在其中有一个分页符。分页符应该由 HTML 决定,即我想知道 HTML 中是否有 TCPDF 可以理解的标识符,然后相应地在生成的 PDF 中添加分页符。

我怎么能这样做呢?

I am using TCPDF to generate the PDF in one of my projects. I simply create a HTML file and give it to the TCPDF to handle the PDF generation. But now I have some HTML where multiple certificates are added one after the other and I want to have a page break in it. Page Break should be decided by HTML i.e. I want to know if there is any identifier in HTML which TCPDF understands and then accordingly adds a page break into the generated PDF.

How could I do this?

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

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

发布评论

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

评论(9

格子衫的從容 2024-08-15 02:30:26

我正在使用

查找方法writeHTML和代码

if ($dom[$key]['tag'] AND isset($dom[$key]['attribute']['pagebreak'])) {
    // check for pagebreak
    if (($dom[$key]['attribute']['pagebreak'] == 'true') OR ($dom[$key]['attribute']['pagebreak'] == 'left') OR ($dom[$key]['attribute']['pagebreak'] == 'right')) {
        // add a page (or trig AcceptPageBreak() for multicolumn mode)
        $this->checkPageBreak($this->PageBreakTrigger + 1);
    }
    if ((($dom[$key]['attribute']['pagebreak'] == 'left') AND (((!$this->rtl) AND (($this->page % 2) == 0)) OR (($this->rtl) AND (($this->page % 2) != 0))))
            OR (($dom[$key]['attribute']['pagebreak'] == 'right') AND (((!$this->rtl) AND (($this->page % 2) != 0)) OR (($this->rtl) AND (($this->page % 2) == 0))))) {
        // add a page (or trig AcceptPageBreak() for multicolumn mode)
        $this->checkPageBreak($this->PageBreakTrigger + 1);
    }
}

I'm using <br pagebreak="true"/>.

Find method writeHTML and code

if ($dom[$key]['tag'] AND isset($dom[$key]['attribute']['pagebreak'])) {
    // check for pagebreak
    if (($dom[$key]['attribute']['pagebreak'] == 'true') OR ($dom[$key]['attribute']['pagebreak'] == 'left') OR ($dom[$key]['attribute']['pagebreak'] == 'right')) {
        // add a page (or trig AcceptPageBreak() for multicolumn mode)
        $this->checkPageBreak($this->PageBreakTrigger + 1);
    }
    if ((($dom[$key]['attribute']['pagebreak'] == 'left') AND (((!$this->rtl) AND (($this->page % 2) == 0)) OR (($this->rtl) AND (($this->page % 2) != 0))))
            OR (($dom[$key]['attribute']['pagebreak'] == 'right') AND (((!$this->rtl) AND (($this->page % 2) != 0)) OR (($this->rtl) AND (($this->page % 2) == 0))))) {
        // add a page (or trig AcceptPageBreak() for multicolumn mode)
        $this->checkPageBreak($this->PageBreakTrigger + 1);
    }
}
假扮的天使 2024-08-15 02:30:26

您可以将 TCPDF 的 AddPage() 方法与 explode() 和合适的分隔符结合使用:

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8',
                 false);

// TCPDF initialization code (...)

$delimiter = '<h1>';
$html      = file_get_contents('./test.html');
$chunks    = explode($delimiter, $html);
$cnt       = count($chunks);

for ($i = 0; $i < $cnt; $i++) {
    $pdf->writeHTML($delimiter . $chunks[$i], true, 0, true, 0);

    if ($i < $cnt - 1) {
        $pdf->AddPage();
    }
}

// Reset pointer to the last page
$pdf->lastPage();

// Close and output PDF document
$pdf->Output('test.pdf', 'I');

You might use TCPDF's AddPage() method in combination with explode() and a suitable delimiter:

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8',
                 false);

// TCPDF initialization code (...)

$delimiter = '<h1>';
$html      = file_get_contents('./test.html');
$chunks    = explode($delimiter, $html);
$cnt       = count($chunks);

for ($i = 0; $i < $cnt; $i++) {
    $pdf->writeHTML($delimiter . $chunks[$i], true, 0, true, 0);

    if ($i < $cnt - 1) {
        $pdf->AddPage();
    }
}

// Reset pointer to the last page
$pdf->lastPage();

// Close and output PDF document
$pdf->Output('test.pdf', 'I');
醉生梦死 2024-08-15 02:30:26

我尝试使用

<br pagebreak="true" />

<tcpdf method="AddPage" />

每个都导致没有在页面顶部开始新页面,而是在 HTML 文本之间添加完整的 A4 页空白。因此,如果文本在页面中间结束,然后插入分页符,则新文本将从下一页的中间写入。这是我不想要的。

有效的是这个(在这里找到它 TCPDF 强制创建新页面< /a>):

$pdf->writeHTML($content, true, 0, true, 0);

$pdf->AddPage();
$pdf->setPage($pdf->getPage());  

现在从在页面顶部写入文本开始。

I tried using

<br pagebreak="true" />

or

<tcpdf method="AddPage" />

each of them resulted not in starting new page at the top of the page but adding the full A4-page empty space in between HTML text. So if text ended in the middle of the page and then page break was inserted, the new text was written from the middle of the next page. Which I didn't want.

What worked was this (found it here TCPDF forcing a new page):

$pdf->writeHTML($content, true, 0, true, 0);

$pdf->AddPage();
$pdf->setPage($pdf->getPage());  

This now starts with writing text on top of the page.

人生戏 2024-08-15 02:30:26

TCPDF 支持 HTML 标签的“pagebreak”属性以及 CSS 属性“page-break-before”和“page-break-after”。
例如,您可以使用

请查看官方 http://www.tcpdf.org 网站和论坛以获取更多信息。

TCPDF support the 'pagebreak' attribute for HTML tags and CSS properties 'page-break-before' and 'page-break-after'.
For example you can use <br pagebreak="true" />.

Check the official http://www.tcpdf.org website and forums for further information.

三生一梦 2024-08-15 02:30:26

对于分页 TCPDF 库仍然遇到相同问题的人,

您可以使用

< ;br pagebreak="true"/>
在 HTML 内容中手动中断页面。

使用 $tcpdf->AddPage() 在代码中手动分页。

当您设置 SetAutoPageBreak(TRUE, 10); 时,意味着:当文档高度达到 (bottom - 10) 时,将光标移动到新页面。所以如果你想要更多的空间,只需将数字减少到0。它会绘制到文档的末尾,底部没有任何边距。

请记住,TCPDF 只接受标记属性的双引号 (")。请勿对标记使用单引号 (')。

=> 错误

=> 正确

因为这个问题:(

For someone who still has the same problem with page-break TCPDF library

You can use <div style="page-break-before:always"></div> OR <br pagebreak="true"/>
to break page manually in your HTML content.

Use $tcpdf->AddPage() to break page manually in your code.

When you set SetAutoPageBreak(TRUE, 10); that means: when the height of document reach to (bottom - 10) then move the cursor to new page. So if you want to have more space, just reduce the number into 0. It will draw until the end of the document without any margin from bottom.

Remember that TCPDF only accept the double quote (") for attributes of tags. Don't use single quote (') for your tag.

<div style='page-break-before:always' => WRONG

<div style="page-break-before:always" => RIGHT

It takes 8 hours from me because this issue :(

只是我以为 2024-08-15 02:30:26

在 2011 年 12 月 23 日的 5.9.142 版本中,我们可以使用 page-break-before、page-break-inside css 属性,如下所示:

<div style="page-break-inside:avoid;">
some non breakable text
</div>

With version 5.9.142 from 2011-12-23 we could use the page-break-before, page-break-inside css properties, like this:

<div style="page-break-inside:avoid;">
some non breakable text
</div>
地狱即天堂 2024-08-15 02:30:26

根据 http://www.tcpdf.org/examples/example_049.phps 你可以使用类似的内容

$html .= '<tcpdf method="AddPage" /><h2>Graphic Functions</h2>';

您需要验证 TCPDF 配置文件中的参数 K_TCPDF_CALLS_IN_HTML 是否为 true。

According to http://www.tcpdf.org/examples/example_049.phps you can use something like this

$html .= '<tcpdf method="AddPage" /><h2>Graphic Functions</h2>';

You need to verify that parameter K_TCPDF_CALLS_IN_HTML in TCPDF configuration file is true.

开始看清了 2024-08-15 02:30:26

您也可以按照此方法来完成您的需求:

$htmlcontent1="CERTIFICATE NUMBER 1 IMAGE HERE";

// output the HTML content
$pdf->writeHTML($htmlcontent1, true, 0, true, 0);

// reset pointer to the last page
$pdf->lastPage();

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Print a table

// add a page
$pdf->AddPage();

$htmlcontent1="CERTIFICATE NUMBER 1 IMAGE HERE";

// output the HTML content
$pdf->writeHTML($htmlcontent1, true, 0, true, 0);
// reset pointer to the last page
$pdf->lastPage();

// ---------------------------------------------------------

//Close and output PDF document
$pdf->Output('textcertificate.pdf', 'D');

希望它可以帮助某人:)

谢谢

You can also follow this method to accomplish your needs:

$htmlcontent1="CERTIFICATE NUMBER 1 IMAGE HERE";

// output the HTML content
$pdf->writeHTML($htmlcontent1, true, 0, true, 0);

// reset pointer to the last page
$pdf->lastPage();

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Print a table

// add a page
$pdf->AddPage();

$htmlcontent1="CERTIFICATE NUMBER 1 IMAGE HERE";

// output the HTML content
$pdf->writeHTML($htmlcontent1, true, 0, true, 0);
// reset pointer to the last page
$pdf->lastPage();

// ---------------------------------------------------------

//Close and output PDF document
$pdf->Output('textcertificate.pdf', 'D');

Hopes it helps someone :)

Thanks

不奢求什么 2024-08-15 02:30:26

通过 CSS 为元素提供 page-break-afterpage-break-beforepage-break-inside 属性将应用属性 <在 TCPDF 运行时,将 code>pagebreak 或 pagebreakafter 添加到 html 标记。

// page-break-inside
if (isset($dom[$key]['style']['page-break-inside']) AND ($dom[$key]['style']['page-break-inside'] == 'avoid')) {
    $dom[$key]['attribute']['nobr'] = 'true';
}
// page-break-before
if (isset($dom[$key]['style']['page-break-before'])) {
    if ($dom[$key]['style']['page-break-before'] == 'always') {
        $dom[$key]['attribute']['pagebreak'] = 'true';
    } elseif ($dom[$key]['style']['page-break-before'] == 'left') {
        $dom[$key]['attribute']['pagebreak'] = 'left';
    } elseif ($dom[$key]['style']['page-break-before'] == 'right') {
        $dom[$key]['attribute']['pagebreak'] = 'right';
    }
}
// page-break-after
if (isset($dom[$key]['style']['page-break-after'])) {
    if ($dom[$key]['style']['page-break-after'] == 'always') {
        $dom[$key]['attribute']['pagebreakafter'] = 'true';
    } elseif ($dom[$key]['style']['page-break-after'] == 'left') {
        $dom[$key]['attribute']['pagebreakafter'] = 'left';
    } elseif ($dom[$key]['style']['page-break-after'] == 'right') {
        $dom[$key]['attribute']['pagebreakafter'] = 'right';
    }
}

Giving your element the page-break-after, page-break-before or page-break-inside property via CSS will apply the attribute pagebreak or pagebreakafter to the html tag during TCPDF runtime.

// page-break-inside
if (isset($dom[$key]['style']['page-break-inside']) AND ($dom[$key]['style']['page-break-inside'] == 'avoid')) {
    $dom[$key]['attribute']['nobr'] = 'true';
}
// page-break-before
if (isset($dom[$key]['style']['page-break-before'])) {
    if ($dom[$key]['style']['page-break-before'] == 'always') {
        $dom[$key]['attribute']['pagebreak'] = 'true';
    } elseif ($dom[$key]['style']['page-break-before'] == 'left') {
        $dom[$key]['attribute']['pagebreak'] = 'left';
    } elseif ($dom[$key]['style']['page-break-before'] == 'right') {
        $dom[$key]['attribute']['pagebreak'] = 'right';
    }
}
// page-break-after
if (isset($dom[$key]['style']['page-break-after'])) {
    if ($dom[$key]['style']['page-break-after'] == 'always') {
        $dom[$key]['attribute']['pagebreakafter'] = 'true';
    } elseif ($dom[$key]['style']['page-break-after'] == 'left') {
        $dom[$key]['attribute']['pagebreakafter'] = 'left';
    } elseif ($dom[$key]['style']['page-break-after'] == 'right') {
        $dom[$key]['attribute']['pagebreakafter'] = 'right';
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文