table 内的 HTML2PDF 意外换行符

HTML2PDF unexpected line-breaks within table

我正在使用 HTML2PDF 从 HTML 字符串生成 pdf 文件。

一切都很好很容易,直到我遇到一个非常奇怪的错误。 td 中间有一个分页符。它不仅没有带走 css ,而且它决定中断的地方也没有任何意义。这个句子比它上面的句子短,而上面的句子只占 1 行。这意味着带有中断的句子将在线上,相反,它决定使用 4 行。这听起来可能令人困惑,所以这里是 html.

的示例
<table>
    <tr>
        <td>Some text longer than the text below.</td>
        <th>Apples</th>
    </tr>
    <tr>
        <td>Text shorter than text above</td>
        <th>Bananas</th>
    </tr>
</table>

如您所见,第一个 td 比第二个 td 长,因此您认为如果第二个 td 需要换行符,那么第一个需要一个。不幸的是,html2pdf 决定第二个 td 需要 3 个换行符,而第一个有 none.

这弄错了它下面的 table 行,其中 th 与每一行的 td 重叠。

我查看了代码,我可以肯定地说:这就是 html。没有分页符,什么都没有。

如果我将输出写在 html 中,它会是什么样子? 这不是给 pdf 的实际 html! 这只是输出,写成 html。

<table>
    <tr>
        <td>Some text longer than the text below.</td>
        <th>Apples</th>
    </tr>
    <tr>
        <td>Text shorter <br>than <br>text <br>above</td>
        <th>Bananas</th>
    </tr>
</table>

备注:

  1. 这个 table 在我页面的底部,所以换行符会导致非常错误的分页。
  2. 我尝试在每个元素上使用 page-break-inside: avoid;(甚至同时使用),但似乎没有任何效果。

我找到了答案。显然,html2pdf 不支持 thead 和 tfoot。 html2pdf把一个table放在一个数组里,没有把tfoot和thead算进去,但是输出了。

他做的就是这个。你有这个代码:

<table>
    <thead>
        header
    </thead>
    <tr>
        <td>Some text longer than the text below.</td>
        <th>Apples</th>
    </tr>
    <tr>
        <td>Text shorter <br>than <br>text <br>above</td>
        <th>Bananas</th>
    </tr>
    <tfoot>
        footer
    </tfoot>
</table>

他把每一行都放在一个数组中,像这样:

array (
0 => array (
    0 => "thead"
    1 => ""
1 => array (
    0 => "Some text longer than the text below."
    1 => "Apples"
2 => array (
    0 => "Text shorter than text above"
    1 => "Bananas"
3 => array (
    0 => "footer"
    1 => "")

然而,他只会看到 2 行,所以算作 0 和 1。他会输出 array(0) 和 array(1)。在那之后,他会找到 table 的其余部分,只是 post 它就好像它只是一个带有标签的字符串,而实际上它是一个 table 行。

我的解释可能有误,这就是我的理解方式。