dt614037527
dt614037527
2017-01-19 14:36

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

已采纳

I am having a php table which contains information about billing of certain numbers. After generating this table I export it in pdf using mpdf. If the table has a certain number of rows it breaks the page in a weird way, leading the headers repeat on the next page with no rows. Here is the format of the table:

The table contains up to 3 headers inside <thead> tags, like this:

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

rows are added in a while from the database in this way.. nothing special

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

with the total lines at the end

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

!!! EVERYTHING that you have seen by now, including the totals are a part of the table!

Here is the mpdf settings that I have:

In 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');

In 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-->';

Some styling that I use

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;
}

The result looks something like this:

enter image description here

This issue occurs only when the number of rows fills the page. If it overflows it is no major issue, because the table headers are still there on the next page (all of them unfortunately, not only the last one) and the rest of the rows are added further.

How can I get rid of those empty rows, in this special case?

Thank you!

!!EDIT: Added more information, code and a better image of the case. Sorry for not doing it at the first time!

!!EDIT #2: I just observed on other examples that had more than 1 page of rows that the <thead>s keep their position on next pages. It happens all the time. Is there a way to disable that so that other <thead>s will just continue on the next page from the top of the page without remembering the position from the previous page?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • douzhuangna6906 douzhuangna6906 4年前

    I eventually found a solution. Best way to get rid of these things is to replace <thead> tags with <th>. <thead> has a predefined style which makes the table header repeat itself on every page. <th> will make the header stop repeating itself on the next pages (fortunate or unfortunate), but it will eliminate the space caused by the <thead>'s style

    点赞 评论 复制链接分享
  • dongtan4046 dongtan4046 4年前

    You need to set headers (and footers) in individual strings, like this:

    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();
    
    点赞 评论 复制链接分享