mPDF 在长文本的表格行中自动分页

问题描述 投票:0回答:1

我想从 TCPDF 切换到 mPDF。在我的文档中,我使用两列布局,它本身包含可能具有不同高度的子表,因此它看起来像流体布局。使用 TCPDF,它看起来像:

enter image description here

此布局的基本语法是:

<table cellpadding="0" cellspacing="0" border="0">
    <tr>
        <td style="width: 50%;">
            <table cellpadding="0" cellspacing="0" border="0">
                <tr>
                    <td style="width: 40%;">Label ABC</td>
                    <td>Value XYZ</td>
                </tr>
            </table>
            <table cellpadding="0" cellspacing="0" border="0">
                <tr>
                    <td style="width: 40%;">Label ABC</td>
                    <td>Value XYZ</td>
                </tr>
            </table>
            [...]
        </td>
        <td style="width: 50%;">
            <table cellpadding="0" cellspacing="0" border="0">
                <tr>
                    <td style="width: 40%;">Label ABC</td>
                    <td>Value XYZ</td>
                </tr>
            </table>
            <table cellpadding="0" cellspacing="0" border="0">
                <tr>
                    <td style="width: 40%;">Label ABC</td>
                    <td>Value XYZ</td>
                </tr>
            </table>
            [...]
        </td>
    </tr>
</table>

使用 mPDF,它尝试缩小表格,这不是我想要的。所以我用

$pdf->shrink_tables_to_fit = 1;
禁用了收缩。 我可以在 mPDF 中实现类似的行为,以便包含长文本的表格行自动换行到新页面吗?

编辑:只是为了澄清,我希望唯一的外部

<tr>
能够打破内容。外表的两列包含许多细长的嵌套表(参见上面的代码和图像)。由于布局流畅,我将表格嵌套在一个包含两列的外部
<tr>
中。

php tcpdf mpdf
1个回答
0
投票

我建议您使用 colspan 和 rowspan 将布局重新排列为单个表格。还使用 colgroup 样式来强制执行各种宽度。

通过避免嵌套表,TCPDF 将能够更轻松地分页 - 基本上是因为每一行都是可破坏的。

为此,您需要计算出整个工作表上有多少个垂直单元格分区,并决定将它们放入哪一列。

在极端情况下,您可以有 1px 列,但代码将非常难以维护。这是使用这三个标签的表格示例。

<table>
    <caption>School Sports Team Participation</caption>
    <colgroup>
        <col style="width: 20%;" span="1">
        <col style="width: 20%;" span="3">
        <col style="width: 40%;">
    </colgroup>
    <thead>
        <tr>
            <th rowspan="2">Sport</th>
            <th colspan="3">Team Composition</th>
            <th rowspan="2">Additional Notes</th>
        </tr>
        <tr>
            <th>Boys</th>
            <th>Girls</th>
            <th>Total Players</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Basketball</td>
            <td>15</td>
            <td>12</td>
            <td>27</td>
            <td rowspan="3">These teams compete in local school leagues</td>
        </tr>
        <tr>
            <td>Volleyball</td>
            <td>8</td>
            <td>14</td>
            <td>22</td>
        </tr>
        <tr>
            <td>Soccer</td>
            <td>18</td>
            <td>10</td>
            <td>28</td>
        </tr>
        <tr>
            <td colspan="5">Total Sports Teams: 3</td>
        </tr>
    </tbody>
</table>

您还可以为 TCPDF 添加一些 CSS 样式来改善外观。

<style>
    table {
        width: 100%;
        border-collapse: collapse;
        font-family: Arial, sans-serif;
    }
    th, td {
        border: 1px solid #ddd;
        padding: 8px;
        text-align: center;
    }
    th {
        background-color: #f2f2f2;
    }
    caption {
        font-weight: bold;
        margin-bottom: 10px;
    }
</style>
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.