php表中断页面,下一页没有行

时间:2017-01-19 14:36:19

标签: php html mpdf

我有一个php表,其中包含有关某些号码计费的信息。生成此表后,我使用mpdf将其导出为pdf。 如果表具有一定数量的行,则会以奇怪的方式破坏页面,导致标题在下一页上重复而没有行。 这是表的格式:

该表格最多包含<thead>个标记内的3个标题,如下所示:

<table class="items" width="100%" style="font-size: 9pt; border-collapse: collapse;" cellpadding="8">
    <thead>
        <tr>
            <td width="30.3%">Forbrug</td>
            <td width="23.1%">Periode</td>
            <td width="10.76%">Tid/MB</td>
            <td width="9.28%">Enheder</td>
            <td width="12.9%" align="right">I alt</td>
            <td width="13.3%" align="right">I alt+<b>moms</b></td>
        </tr>
    </thead>
以这种方式从数据库中添加

行。没有什么特别的

<tr>
    <td>'.$produktNameFromDatabase.'</td>
    <td>'.$periodeFromDatabase.'</td>
    <td>'.$tidMbFromDatabase.'</td>
    <td>'.$enhederFromDatabase.'</td>
    <td align="right">'.number_format((float)$produktpris, 2, ',', '.').' kr.</td>
    <td align="right"><b>'.number_format((float)$produktpris*(1 + (0.25 * $moms)) - $momsDiscount, 2, ',', '.').' kr.</b></td>
</tr>

总行在最后

        <tr>
            <td class="blanktotal" colspan="1" rowspan="6"></td>
            <td class="blanktotal" colspan="1" rowspan="6"></td>
            <td class="totals" colspan="2">Subtotal:</td>
            <td class="totals" colspan="2">'.number_format((float)$pris1, 2, ',', '.').' kr.</td>
        </tr>
        <tr>
            <td class="totals1" colspan="2">Moms:</td>
            <td class="totals1" colspan="2">'.number_format((float)$pris1*(0.25 * $moms) - $momsTotal, 2, ',', '.').' kr.</td>
        </tr>
        <tr>
            <td class="totals1" colspan="2"><b>TOTAL:</b></td>
            <td class="totals1" colspan="2"><b>'.number_format((float)$pris1*(1 + (0.25 * $moms)) - $momsTotal, 2, ',', '.').' kr.</b></td>
        </tr>
    </tbody>
</table>

!!!你现在看到的一切,包括总数都是表格的一部分!

以下是我的mpdf设置:

在PhP中:

$mpdf=new mPDF('win-1252','A4','','',20,15,48,25,10,10);
$mpdf->useOnlyCoreFonts = true;    // false is default
$mpdf->SetProtection(array('print'));
$mpdf->SetTitle("Suggestive Title");
$mpdf->SetAuthor("Author");
$mpdf->SetWatermarkText("Faktura");
$mpdf->showWatermarkText = true;
$mpdf->watermark_font = 'DejaVuSansCondensed';
$mpdf->watermarkTextAlpha = 0.1;
$mpdf->SetDisplayMode('fullpage');

在HTML中:

$html .= '<!--mpdf
    <htmlpageheader name="myheader">
        <table width="100%"><tr>
            <td width="50%" style="color:#000000;">
                <span style="font-weight: bold; font-size: 14pt;">
                    Company Name
                </span>
                Company Information
            </td>
            <td width="50%" style="text-align: right;">
                <img src="image.jpg" />
                    Invoice Information <br />Side: {PAGENO} af {nb}
                </td>
        </tr></table>
    </htmlpageheader>

    <htmlpagefooter name="myfooter">
        <div style="border-top: 1px solid #000000; font-size: 9pt; text-align: center; padding-top: 3mm; ">
            Footer Stuff
        </div>
        <div style="font-size: 9pt; text-align: center;">
            Other Footer Stuff
        </div>
    </htmlpagefooter>

    <sethtmlpageheader name="myheader" value="on" show-this-page="1" />
    <sethtmlpagefooter name="myfooter" value="on" />
mpdf-->';

我使用的一些样式

body {font-family: sans-serif;
    font-size: 10pt;
}
p { margin: 0pt;
}
td { vertical-align: top; }
.items td {
    border-left: 0.1mm solid #000000;
    border-right: 0.1mm solid #000000;
}
table thead td { background-color: #d4ffaa;
    border: 0.1mm solid #000000;
}
.items td.blanktotal {
    background-color: #FFFFFF;
    border: 0mm none #000000;
    border-top: 0.1mm solid #000000;
    border-right: 0.0mm solid #000000;
}
.items td.totals {
    text-align: right;
    border-top: 0.1mm solid #000000;
    border-right: 0.0mm solid #000000;
    border-left: 0.0mm solid #000000;
}
.items td.totals1 {
    text-align: right;
    border-top: 0.0mm solid #000000;
    border-right: 0.0mm solid #000000;
    border-left: 0.0mm solid #000000;
}

结果如下所示:

enter image description here

当行 填充页面 时,会出现 此问题。如果它溢出则不是主要问题,因为表头仍然在下一页(不幸的是,所有这些,不仅是最后一个),其余的行被进一步添加。

如何摆脱那些空行, 在这种特殊情况下

谢谢!

!!编辑: 添加了更多信息,代码和更好的案例形象。很抱歉没有第一次这样做!

!!编辑#2: 我刚观察了其他有超过1页行的示例<thead> s在下一页保持其位置。它一直在发生。有没有办法禁用它,以便其他<thead>只会从页面顶部的下一页继续而不记住上一页的位置?

2 个答案:

答案 0 :(得分:0)

您需要在单个字符串中设置标题(和页脚),如下所示:

require_once __DIR__ . '/../vendor/autoload.php';

$stylesheet = '
.table-header{
    border: none;
    background-color: yellow;
    width: 100%;
}
.table-header td{
    border-right: 1px solid;
}
.table-body{
    border: none;
    background-color: transparent;
    width: 100%;
}
.table-body td{
    border-right: 1px solid;
    border-bottom: 1px solid;
}
.table-footer{
    border: none;
    background-color: transparent;
    width: 100%;
}
.table-footer .td-number{
    text-align: right;
}
';

/* Sample data */
$html = '<table class="table-body">';
for ($i = 1; $i <= 100; $i++) {
    $html.='<tr>
    <td width="30.3%">Forbrug '.$i.'</td>
    <td width="23.1%">Periode '.$i.'</td>
    <td width="10.76%">'.$i.' MB</td>
    <td width="9.28%">Val '.$i.'</td>
    <td width="12.9%" align="right">I alt '.$i.'</td>
    <td width="13.3%" align="right"><b>moms</b> '.$i.'</td>
    </tr>';
}
$html.= '</table>';

$header = '<table class="table-header"><tr>
<td width="30.3%">Forbrug</td>
<td width="23.1%">Periode</td>
<td width="10.76%">Tid/MB</td>
<td width="9.28%">Enheder</td>
<td width="12.9%" align="right">I alt</td>
<td width="13.3%" align="right">I alt+<b>moms</b></td>
</tr></table>';

$footer = '<table class="table-footer"><tr>
<td>Footer here</td>
<td class="td-number"><b>Page N. {PAGENO}</b></td>
</tr></table>';

$mpdf = new mPDF('c','A4','','',15,15,18,15,10,5);
$mpdf->mirrorMargins = 1;
/* Declare headers */
$mpdf->SetHTMLHeader($header);
$mpdf->SetHTMLHeader($header,'E');
/* Declare footers */
$mpdf->SetHTMLFooter($footer);
$mpdf->SetHTMLFooter($footer,'E');
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($html);
$mpdf->Output();

答案 1 :(得分:0)

我最终找到了解决方案。摆脱这些事情的最佳方法是用<thead>替换<th>标签。 <thead>具有预定义的样式,使得表头在每个页面上重复。 <th>会使标题停止在下一页重复(幸运或不幸),但它会消除由<thead>的样式引起的空间

相关问题