【问题标题】:Forcing a page break only when necessary during print仅在打印期间必要时强制分页
【发布时间】:2015-06-30 15:16:52
【问题描述】:

我目前正在尝试避免在 div 中间出现分页符。我希望它仅在分页符发生在 div 中间时才中断。但是我已经尝试过 page-break-inside: Avoid 并且它什么都不做。我不能在 div 之前强制分页,因为我不知道页面会有多长时间。任何帮助或其他想法都会很好。

这是我试图避免分页符的潜水:

<div style="page-break-inside: avoid;">
        <h2 style="height:18px;">
            <span>
                <div style="width:52%; float:left;">**Delinquency Notice** as of 12/31/13</div>
                <div style="width:48%; float:left;">Recent Account History</div>
                <div styly="clear:both;"/>
            </span>
        </h2>
        <div>
            <div class="leftColumn">
                <div class="small_padding">
                    <span class="bold_text">
                        You are late on your mortgage payments.
                    </span>
                    As of 4/17/2015, you are 106 days late on your mortgage payments. 
                    Failure to bring your loan current may result in fees and 
                    foreclosure -- the loss of your home.
                    <div class="small_top_padding">
                        If you are experiencing financial difficulty, HUD (U.S. Dept 
                        of Housing and Urban Development) approved counseling agents 
                        are available to provide mortgage and foreclosure counseling 
                        assistance. Refer to the HUD disclosure section on the 
                        statement front for contact information.
                    </div>
                </div>
            </div>
            <div class="rightColumn">
                <div class="recent_history_body">
                    <ul style="list-style-type: disc;">
                        <li>Payment due 12/01/2014: Fully paid on 11/30/2014</li>
                        <li>Payment due 1/01/2015: Unpaid balance of $1,290.02</li>
                        <li>Payment due 2/01/2015: Fully paid on 1/31/2015</li>
                        <li>Payment due 3/01/2015: Fully paid on 2/28/2015</li>
                        <li>Payment due 4/01/2015: Fully paid on 3/17/2015</li>
                        <li>Current payment due 5/01/2015: $1,290.02</li>
                    </ul>
                    <div class="recent_history_total">
                        Total: $6,495.86 due. You must pay this amount to bring your loan current.
                    </div>
                </div>
            </div>
        </div>
    </div>

这是在谷歌浏览器中打印的屏幕截图。

【问题讨论】:

    标签: html css printing


    【解决方案1】:

    试试

    @media print  
    {
       div{
           page-break-inside: avoid;
        }
    }
    

    或者这个(取决于浏览器)

    @media print
    {
        div.pad * { 
            page-break-after:avoid;
            page-break-before:avoid;
        }
    }
    

    chrome 试试这个

      div.page
      {
        page-break-after: always;
        page-break-inside: avoid;
      }
    

    对于 IE11 似乎是 div 的问题,请尝试用 p 环绕

    page-break-after 不适用于 div 标签。尝试使用 p 或 br 标记

     <p style="page-break-before: always">&nbsp;</p>
    

    【讨论】:

    • 这不起作用。我收到相同的结果。我也有
      .. 为什么这不起作用?
    • 有些地方有用
    • 我认为浏览器总比没有好。我认为这对你很有用。
    • 没有这样的东西不要在这个里面打破一个页面......但是如果它比整个页面长,好的..继续打破它命令。大多数格式化程序只是前进。所以它会尝试按照你的规则放置内容......不要打破。它到达页面末尾并将所有内容移动到下一页并继续......如果它再次到达页面末尾,它将将该页面作为空白并继续移动并破坏您的条件,因为它无法满足。所以如果你不能保证一个div不会比整个页面长,那也没关系。
    • 查看这个相关问题...stackoverflow.com/questions/30756982/…
    【解决方案2】:

    发现 page-break-inside:avoid 不支持嵌入 div。为了完成我想做的事情,我需要将我的 html 转换为表格并用一个 div 包围它。这也适用于 FF(38.0.5)、IE(11) 和 Chrome(43.0.2357.130 m)

    <div style="page-break-inside: avoid;">
       <table style="width:100%;">
           <tbody>
           <tr>
                <td style="width:400px;">
                     <h2>**Delinquency Notice** as of 12/31/13</h2>
                </td>
                <td style="width:400px;">
                     <h2>Recent Account History</h2>
                </td>
            </tr>
            <tr>
                <td class="padding"><b>You are late on your mortgage payments.</b> 
                        As of 4/17/2015, you are 106 days late on your mortgage payments. 
                        Failure to bring your loan current may result in fees and 
                        foreclosure -- the loss of your home.<br/><br/>If you are experiencing financial difficulty, HUD
                        (U.S. Dept 
                        of Housing and Urban Development) approved counseling agents 
                        are available to provide mortgage and foreclosure counseling 
                        assistance. Refer to the HUD disclosure section on the 
                        statement front for contact information.
                </td>
                <td class="padding">
                    <ul style="list-style-type: disc;">
                        <li>Payment due 12/01/2014: Fully paid on 11/30/2014</li>
                        <li>Payment due 1/01/2015: Unpaid balance of $1,290.02</li>
                        <li>Payment due 2/01/2015: Fully paid on 1/31/2015</li>
                        <li>Payment due 3/01/2015: Fully paid on 2/28/2015</li>
                        <li>Payment due 4/01/2015: Fully paid on 3/17/2015</li>
                        <li>Current payment due 5/01/2015: $1,290.02</li>
                    </ul>
                    <br/><b>Total: $6,495.86 due. You must pay this amount to bring your loan current</b></td>
            </tr>
            </tbody>
        </table>
    </div>
    

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签