Search code examples
csshtmlpdfwkhtmltopdf

How to not display a box twice, when printing with Chrome a div box with background that does not fit before page break?


Have a look at the following image (page print dialog in Chrome):

enter image description here

The DIV with grey background does not fit on the first page anymore, so the browser decides to move the whole div to a new page (which is what I want).

What I however want to get rid of, is the empty box with background color on the first page (which originates from the very same DIV).

Is there a way to wrap the complete box to the next page? And not display it on the first page at all (if it does not fit).


In the end I'm not interested in printing the page, but rather use wkhtml2pdf to generate a PDF from this HTML. But it is the same issue.

So a webkit only solution would be fine for me.

Here is the HTML / CSS code, if you like to try what could be seen in the image above. Just open the print dialog in Chrome and you should see the same result:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>PP-00000074</title>
    <style>
        html {
            margin: 0;
            padding: 0;
            align-self: center;
        }

        body {
            padding: 0;
            align-self: center;
            margin: 0;
        }

        table {
            width: 100%;
            border-spacing: 0;
            border-collapse: collapse;
            font-size: 13px;
            margin-bottom: 45px;
        }

        table tr {
            page-break-inside: avoid;
        }

        thead {
            width: 100%;
            align-self: center;
        }

        thead tr {
            border-bottom: 15px solid white;
        }

        th {
            color: #000000;
            font-weight: normal;
        }

        td {
            color: #7a7a7a;
            word-break: break-word;
        }

        tr.spaceAbove>td {
            padding-top: 20px;
        }

        .nowrap {
            white-space: nowrap;
        }

        .centerBodyBox {
            margin: 0 100px;
        }

        .invoiceTotal {
            page-break-inside: avoid;
            background-color: #eeeeee;
            font-size: 13px;
            line-height: 1.56;
            text-align: right;
            color: #000000;
        }

        .invoiceTotalColumn1 {
            font-size: 12px;
            color: #7a7a7a;
            padding: 35px 0;
            vertical-align: bottom;
        }

        .invoiceTotalColumn2 {
            width: 25%;
            padding: 35px 0;
            vertical-align: top;
        }

        .invoiceTotalColumn3 {
            width: 15%;
            padding: 35px 0;
            vertical-align: top;
        }

        .text-left {
            text-align: left;
            padding-right: 10px;
        }

        .text-right {
            text-align: right;
            padding-left: 10px;
        }

        .align-top {
            vertical-align: top;
        }
    </style>
</head>
<body>
<div class="centerBodyBox">
    <table>
        <thead>
        <tr >
            <th class="text-left">Beschreibung</th>
            <th class="text-right">Preis</th>
            <th class="text-right">Menge</th>
            <th class="text-right">Mwst.</th>
            <th class="text-right">Gesamt</th>
        </tr>
        </thead>
        <tr class="spaceAbove">
            <td class="text-left align-top">magni voluptates</td>
            <td class="nowrap text-right align-top">15,42 EUR</td>
            <td class="nowrap text-right align-top">2 </td>
            <td class="nowrap text-right align-top">19%</td>
            <td class="nowrap text-right align-top">30,84 EUR</td>
        </tr>
        <tr class="spaceAbove">
            <td class="text-left align-top">rerum ipsa</td>
            <td class="nowrap text-right align-top">11,93 EUR</td>
            <td class="nowrap text-right align-top">2 </td>
            <td class="nowrap text-right align-top">19%</td>
            <td class="nowrap text-right align-top">23,86 EUR</td>
        </tr>
        <tr class="spaceAbove">
            <td class="text-left align-top">rem consequuntur</td>
            <td class="nowrap text-right align-top">137,50 EUR</td>
            <td class="nowrap text-right align-top">1 </td>
            <td class="nowrap text-right align-top">19%</td>
            <td class="nowrap text-right align-top">137,50 EUR</td>
        </tr>
        <tr class="spaceAbove">
            <td class="text-left align-top">omnis ut</td>
            <td class="nowrap text-right align-top">44,31 EUR</td>
            <td class="nowrap text-right align-top">3 </td>
            <td class="nowrap text-right align-top">19%</td>
            <td class="nowrap text-right align-top">132,93 EUR</td>
        </tr>
        <tr class="spaceAbove">
            <td class="text-left align-top">omnis ut</td>
            <td class="nowrap text-right align-top">44,31 EUR</td>
            <td class="nowrap text-right align-top">3 </td>
            <td class="nowrap text-right align-top">19%</td>
            <td class="nowrap text-right align-top">132,93 EUR</td>
        </tr>
        <tr class="spaceAbove">
            <td class="text-left align-top">omnis ut</td>
            <td class="nowrap text-right align-top">44,31 EUR</td>
            <td class="nowrap text-right align-top">3 </td>
            <td class="nowrap text-right align-top">19%</td>
            <td class="nowrap text-right align-top">132,93 EUR</td>
        </tr>
        <tr class="spaceAbove">
            <td class="text-left align-top">omnis ut</td>
            <td class="nowrap text-right align-top">44,31 EUR</td>
            <td class="nowrap text-right align-top">3 </td>
            <td class="nowrap text-right align-top">19%</td>
            <td class="nowrap text-right align-top">132,93 EUR</td>
        </tr>
        <tr class="spaceAbove">
            <td class="text-left align-top">omnis ut</td>
            <td class="nowrap text-right align-top">44,31 EUR</td>
            <td class="nowrap text-right align-top">3 </td>
            <td class="nowrap text-right align-top">19%</td>
            <td class="nowrap text-right align-top">132,93 EUR</td>
        </tr>
        <tr class="spaceAbove">
            <td class="text-left align-top">omnis ut</td>
            <td class="nowrap text-right align-top">44,31 EUR</td>
            <td class="nowrap text-right align-top">3 </td>
            <td class="nowrap text-right align-top">19%</td>
            <td class="nowrap text-right align-top">132,93 EUR</td>
        </tr>
        <tr class="spaceAbove">
            <td class="text-left align-top">omnis ut</td>
            <td class="nowrap text-right align-top">44,31 EUR</td>
            <td class="nowrap text-right align-top">3 </td>
            <td class="nowrap text-right align-top">19%</td>
            <td class="nowrap text-right align-top">132,93 EUR</td>
        </tr>
        <tr class="spaceAbove">
            <td class="text-left align-top">omnis ut</td>
            <td class="nowrap text-right align-top">44,31 EUR</td>
            <td class="nowrap text-right align-top">3 </td>
            <td class="nowrap text-right align-top">19%</td>
            <td class="nowrap text-right align-top">132,93 EUR</td>
        </tr>
        <tr class="spaceAbove">
            <td class="text-left align-top">omnis ut</td>
            <td class="nowrap text-right align-top">44,31 EUR</td>
            <td class="nowrap text-right align-top">3 </td>
            <td class="nowrap text-right align-top">19%</td>
            <td class="nowrap text-right align-top">132,93 EUR</td>
        </tr>
        <tr class="spaceAbove">
            <td class="text-left align-top">omnis ut</td>
            <td class="nowrap text-right align-top">44,31 EUR</td>
            <td class="nowrap text-right align-top">3 </td>
            <td class="nowrap text-right align-top">19%</td>
            <td class="nowrap text-right align-top">132,93 EUR</td>
        </tr>
        <tr class="spaceAbove">
            <td class="text-left align-top">omnis ut</td>
            <td class="nowrap text-right align-top">44,31 EUR</td>
            <td class="nowrap text-right align-top">3 </td>
            <td class="nowrap text-right align-top">19%</td>
            <td class="nowrap text-right align-top">132,93 EUR</td>
        </tr>
        <tr class="spaceAbove">
            <td class="text-left align-top">omnis ut</td>
            <td class="nowrap text-right align-top">44,31 EUR</td>
            <td class="nowrap text-right align-top">3 </td>
            <td class="nowrap text-right align-top">19%</td>
            <td class="nowrap text-right align-top">132,93 EUR</td>
        </tr>
        <tr class="spaceAbove">
            <td class="text-left align-top">omnis ut</td>
            <td class="nowrap text-right align-top">44,31 EUR</td>
            <td class="nowrap text-right align-top">3 </td>
            <td class="nowrap text-right align-top">19%</td>
            <td class="nowrap text-right align-top">132,93 EUR</td>
        </tr>
        <tr class="spaceAbove">
            <td class="text-left align-top">omnis ut</td>
            <td class="nowrap text-right align-top">44,31 EUR</td>
            <td class="nowrap text-right align-top">3 </td>
            <td class="nowrap text-right align-top">19%</td>
            <td class="nowrap text-right align-top">132,93 EUR</td>
        </tr>
        <tr class="spaceAbove">
            <td class="text-left align-top">omnis ut</td>
            <td class="nowrap text-right align-top">44,31 EUR</td>
            <td class="nowrap text-right align-top">3 </td>
            <td class="nowrap text-right align-top">19%</td>
            <td class="nowrap text-right align-top">132,93 EUR</td>
        </tr>
        <tr class="spaceAbove">
            <td class="text-left align-top">omnis ut</td>
            <td class="nowrap text-right align-top">44,31 EUR</td>
            <td class="nowrap text-right align-top">3 </td>
            <td class="nowrap text-right align-top">19%</td>
            <td class="nowrap text-right align-top">132,93 EUR</td>
        </tr>
        <tr class="spaceAbove">
            <td class="text-left align-top">omnis ut</td>
            <td class="nowrap text-right align-top">44,31 EUR</td>
            <td class="nowrap text-right align-top">3 </td>
            <td class="nowrap text-right align-top">19%</td>
            <td class="nowrap text-right align-top">132,93 EUR</td>
        </tr>
        <tr class="spaceAbove">
            <td class="text-left align-top">omnis ut</td>
            <td class="nowrap text-right align-top">44,31 EUR</td>
            <td class="nowrap text-right align-top">3 </td>
            <td class="nowrap text-right align-top">19%</td>
            <td class="nowrap text-right align-top">132,93 EUR</td>
        </tr>
        <tr class="spaceAbove">
            <td class="text-left align-top">omnis ut</td>
            <td class="nowrap text-right align-top">44,31 EUR</td>
            <td class="nowrap text-right align-top">3 </td>
            <td class="nowrap text-right align-top">19%</td>
            <td class="nowrap text-right align-top">132,93 EUR</td>
        </tr>
    </table>
</div>
<div class="invoiceTotal">
    <div class="centerBodyBox">
        <table style="margin: 0">
            <tr>
                <td class="invoiceTotalColumn1 text-left">
                    Some text.
                </td>
                <td class="invoiceTotalColumn2 nowrap text-right">
                    <div>Zwischensumme (Brutto):</div>
                    <div>Versandkosten:</div>
                    <div>Zahlungsmethode:
                    </div>
                    <div>Nettobetrag (ohne Mwst):</div>
                    <div>zzgl. 19% Mwst.:
                    </div>
                    <div style="margin-top: 20px;"><b>Gesamtbetrag (Brutto):</b>
                    </div>
                </td>
                <td class="invoiceTotalColumn3 nowrap text-right">
                    <div>325,13 EUR</div>
                    <div>4,47 EUR</div>
                    <div>4,16 EUR</div>
                    <div >280,51 EUR</div>
                    <div>53,25 EUR</div>
                    <div style="margin-top: 20px;"><b>333,76 EUR</b></div>
                </td>
            </tr>
        </table>
    </div>
</div>
</body>
</html>

Solution

  • You use page-break-inside with .invoiceTotal.

    But its the invoiceTotal that is breaking. Add page-break-inside:avoid to the body.