开发者

TCPDF - Keep h1, h2, etc. tags with content

I'm generating a PDF using TCPDF and lots of w开发者_运维百科riteHTML() calls. Sometimes when I have header tags (h1, h2, etc.) in the HTML, the headers are at the bottom of a page and the corresponding content gets broken up and pushed to the next page.

Is there any way to have TCPDF either 1) Have the header "keep with" the following content, or 2) Have headers (and only headers) pushed to the next page if some percent (say 20%) or less of the page space is available?

I've tried breaking up the HTML, but ended up passing unclosed tags to writeHTML() and it died.


There is a function AcceptPageBreak() that is called whenever a page break condition is being tested. It might be possible to write some form of override for this function that activates at a higher bottom margin when headers are involved than not.

Though this would require knowing when a header was involved. Hacking the openHTMLTagHandler() function to monitor header tags during an operation might allow that.

Alternatively, if you can break up the HTML, which shouldn't be too hard if you use an HTML parser, then using writeHTMLCell() to output a given section would result in the whole cell being shifted to the next page, if the page break condition occurred during it.


I implemented Orbling's solution, but couldn't quite get it working. Given that, for the moment I will post my working, but more hackish solution, and try to return to Orbling's if I find the time. I will hold out on accepting either answer for a couple days.

My basic approach was to replace all h1-h6 tags (using a regex), and prepend them with <tcpdf method="MaybeSkipPage" />, which I implemented by extending the TCPDF class.

In code:

class MyPDF extends TCPDF {
  public function MaybeSkipPage() {
    if ($this->y > 0.75*$this->getPageHeight()) {
      $this->AddPage();
    }
  }
}

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

$HTMLcontent = preg_replace('/(<[h|H][1-6])/', '<tcpdf method="MaybeSkipPage" />$1', $HTMLcontent);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜