php Html2Pdf 中的分页符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2117788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:06:17  来源:igfitidea点击:

Page break in Html2Pdf

phpmysqlpage-breakhtml2pdf

提问by Greenhorn

I am in the process of generating a dynamic pdf file, which contains data of around 10,000 users, in general the app is developed using MySQL and PHP. The dynamic content is so heavy that, I found it difficult to process with fpdf()class. So I converted my output PHP page as a HTML file using ob_get_clean(). Now the html file is generated successfully and also the pdf file. But i want to leave a page break after each user's data, that is every user's data must start in a fresh page. I couldn't use any HTML tags because, in the dynamically generated HTML file, everything is out of the <html>and </html>tags. Please help me so that some how i make a page break in the pdf file after every user's data... Thanks in advance :)

我正在生成一个动态 pdf 文件,其中包含大约 10,000 个用户的数据,通常该应用程序是使用 MySQL 和 PHP 开发的。动态内容太重了,我发现很难用fpdf()课堂处理。所以我使用ob_get_clean(). 现在成功生成了 html 文件以及 pdf 文件。但我想在每个用户的数据后留下一个分页符,即每个用户的数据必须从一个新页面开始。我不能使用任何 HTML 标签,因为在动态生成的 HTML 文件中,一切都在<html></html>标签之外。请帮助我,以便在每个用户的数据之后我如何在 pdf 文件中进行分页...在此先感谢:)

采纳答案by macdabby

i just figured this out after having the same problem. the parser that they use DOES support the page-break-after tag, but the html2pdf does not work.

我只是在遇到同样的问题后才想到这一点。他们使用的解析器确实支持 page-break-after 标签,但 html2pdf 不起作用。

i think i have it working by doing the following modifications to html2pdf.class:

我想我通过对 html2pdf.class 进行以下修改来让它工作:

around line 4174, the first thing inside:

在第 4174 行附近,里面的第一件事是:

protected function _tag_close_P($param){

should be:

应该:

   if($this->parsingCss->value['page-break-after'] == "always")
        $this->_setNewPage();

around line 2961, the first thing inside:

在第 2961 行附近,里面的第一件事是:

protected function _tag_close_DIV($param, $other='div'){

should be:

应该:

 if($this->parsingCss->value['page-break-after'] == "always")
        $this->_setNewPage();

回答by Andon Totev

html2pdf supports pagetag:

html2pdf 支持页面标签:

protected function _tag_open_PAGE($param) {} 

at line 2229. You can see there what attributes are supported. For example following creates one page in landscape and one in portrait mode:

在第 2229 行。您可以在那里看到支持哪些属性。例如,下面创建一个横向页面和一个纵向模式:

<page orientation="l">
... some content ...
</page>
<page orientation="p">
... some content ...
</page>

回答by Thanh Trung

Basing on macdabby's work (which doesn't work). But thanks to him, the idea is correct.

基于 macdabby 的工作(不起作用)。但多亏了他,这个想法是正确的。

Html2Pdf v4.03

html2pdf v4.03

For example we want to parse a tag DIV:

例如我们要解析一个标签 DIV:

html2pdf.class.php line 2948:

html2pdf.class.php 第 2948 行:

protected function _tag_close_DIV($param, $other='div')
{
    if ($this->parsingCss->value['page-break-after'] == "always")
      $this->_setNewPage(null, '', null, $this->_defaultTop);
      $this->parsingCss->setPosition();
    ...
}

parsingCss.class.php Line 114:

parsingCss.class.php 第 114 行:

//add a new style declaration
public function initStyle()
{
    ...
    $this->value['page-break-after'] = null;
}

Line 1024 add a new handler to the switch case:

第 1024 行向 switch case 添加一个新的处理程序:

case 'page-break-after':
    $this->value[$nom] = $val;
    break;

And then for it to work, your html content should contain the break element

然后为了让它工作,你的 html 内容应该包含 break 元素

 <div style="page-break-after:always; clear:both"></div>

Watch out for case sensitive style, not sure if the plugin handle it

注意区分大小写的样式,不确定插件是否处理它

回答by graphicdivine

You possibly want to use some css, eg:

您可能想使用一些 css,例如:

h1 {page-break-before:always}