Page break in Html2Pdf
Asked Answered
W

4

5

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 :)

Willy answered 22/1, 2010 at 14:16 Comment(0)
B
2

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.

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

around line 4174, the first thing inside:

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:

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

should be:

 if($this->parsingCss->value['page-break-after'] == "always")
        $this->_setNewPage();
Bray answered 28/8, 2012 at 16:49 Comment(2)
This just threw in some errors: Undefined index: page-break-after in html2pdf.class.php on line 4165Koziel
Changing 3rd party code is not best practice, if there are other ways (see answer by @Andon Totev)Cauthen
H
18

html2pdf supports page tag:

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:

<page orientation="l">
... some content ...
</page>
<page orientation="p">
... some content ...
</page>
Hrvatska answered 14/2, 2014 at 7:38 Comment(2)
This is easily the best answer.Stereochromy
should be set as best answer, it works without changing the codePetcock
N
14

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

Html2Pdf v4.03

For example we want to parse a tag DIV:

html2pdf.class.php line 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:

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

Line 1024 add a new handler to the 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

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

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

Noun answered 4/1, 2013 at 16:43 Comment(4)
after finishing the changes the new page should be start immediately after the last portion but it left's the blank page and after that it startsSinusoid
@ThanhTrung This works, but as mentioned above, does not start at the top of the page. Instead, it leaves an entire blank page between the text (as in, the next page doesn't start until it gets to where the previous page stopped at).Jardena
@ThanhTrung Just found the answer on another post: $this->_setNewPage(null, '', null, $this->_defaultTop); $this->parsingCss->setPosition(); That needs added to the _tag_close_DIV code that was added.Jardena
This should be the correct answer, even header and footer are present on the new page.Man
L
2

You possibly want to use some css, eg:

h1 {page-break-before:always}
Ledbetter answered 22/1, 2010 at 14:24 Comment(0)
B
2

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.

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

around line 4174, the first thing inside:

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:

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

should be:

 if($this->parsingCss->value['page-break-after'] == "always")
        $this->_setNewPage();
Bray answered 28/8, 2012 at 16:49 Comment(2)
This just threw in some errors: Undefined index: page-break-after in html2pdf.class.php on line 4165Koziel
Changing 3rd party code is not best practice, if there are other ways (see answer by @Andon Totev)Cauthen

© 2022 - 2024 — McMap. All rights reserved.