javascript - Chrome 的分页符解决方法 (javascript)

标签 javascript css google-chrome printing page-break

众所周知,这个很棒的 CSS 属性:

  table { page-break-inside:auto }
  tr    { page-break-inside:avoid; page-break-after:auto }
  thead { display:table-header-group }
  tfoot { display:table-footer-group }

不适用于 Chrome。现在解决方法来了。我有这个表结构(请注意JSP和EL语法)

    <table class="table resultTable">
        <thead class="repeat">
            <tr class="head">
                <th>XPTO HEADER</th>
                <th>XPTO HEADER</th>
            </tr>
        </thead>
        <tbody>
            <c:forEach items="${items}" var="t" varStatus="loop">
                <tr>                        
                    <td class="money">${t.XPTO}</td>
                    <td class="money">${t.XPTO2}</td>
                </tr>
                <c:if test="${(loop.index + 1) % 20 == 0}">
                    <tr class="break-page">
                    </tr>                   
                </c:if>
            </c:forEach>                                        
        </tbody>
    </table>

有两件事需要注意:线程上的“repeat”类20行后带有break-page类的空tr

所以,检查这个 CSS:

    @media print {          
        .break-page {
             display: block; page-break-before: always;
        }
    }

并检查这个 JS:

     var repeat = $('.repeat tr');

     $('.break-page').after(repeat.clone());

太棒了:)这正是我想要的。检查第一页如何:

enter image description here

第二页,您可以看到标题重复,但是,请检查这个巨大的空白:

enter image description here

就是这样。你们能帮我去掉那个巨大的空白吗? 另外,如果有人遇到和我一样的麻烦,我希望这会有所帮助。

谢谢

最佳答案

我所做的就是:使用 div 而不是 table 标签。我还在 chrome 的 20 行后插入了分页符。创建了一个 javascript hack 来再次添加 header 。

注意:这会为每个浏览器创建不同的页面和数据布局。 Chrome 有 3 页,最后一页表格位于中间......而 Firefox 只有两页,非常适合。这是我能做的最好的事情了。

HTML

            <div class="divTable">
                <div class="divHeading">
                    <div class="divCell">${i18n.label.initialValue}</div>
                    <div class="divCell">${i18n.label.additions}</div>
                    <div class="divCell">${i18n.label.discounts}</div>
                    <div class="divCell">${i18n.label.finalValue}</div>
                </div>
                <c:forEach items="${detailedCashFlow.transactions}" var="t" varStatus="loop">
                        <div class="divCell money">${t.initialValue}</div>
                        <div class="divCell money">${t.totalAddition}</div>
                        <div class="divCell money">${t.totalDiscount}</div>
                        <div class="divCell money">${t.value}</div>
                    </div>

                    //THIS IS VERY IMPORTANT
                    <c:if test="${fn:length(detailedCashFlow.transactions) > (loop.index + 1 ) && (loop.index + 1) % 20 == 0}">
                        <div class="break-page"></div>
                    </c:if>
                </c:forEach>                                        
            </div>

CSS

            .divTable
            {
                display: table;
                width: 100%;
                margin-top: 20px;
            }
            .divTitle
            {
                display: table-caption;
                text-align: center;
                font-weight: bold;
                font-size: larger;
            }
            .divHeading
            {
                display: table-row;
                font-weight: bold;
                text-align: center; 
            }
            .divRow
            {
                display: table-row;
            }
            .divCell
            {
                display: table-cell;
                border: solid;
                border-width: thin;
                padding-left: 5px;
                padding-right: 5px;
                text-align: center;         
                font-size: 12px;
            }
            .divRow:nth-child(even){
                background: #aad4ff !important;
            }
            .divRow:nth-child(odd){
                background: #FFF !important;
            }

            @media print {                        
                .divHeading { 
                    display:table-header-group;
                }

                .divRow
                {
                    -webkit-column-break-inside: avoid;
                    webkit-page-break-inside:avoid; 
                    page-break-inside:avoid; page-break-after:auto;    
                }

                .break-page {
                    page-break-before: always;
                }
            }

JS 黑客

     var is_chrome = window.chrome;

     if(is_chrome){
         var repeat = $('.divHeading');

         $('.break-page').after(repeat.clone());             
     }

关于javascript - Chrome 的分页符解决方法 (javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32484703/

相关文章:

html - 媒体查询不仅限于平板电脑和较小的屏幕尺寸

html - IE 和 chrome 的行高不同

php - HTML:单引号在本网站上显示为空格,但仅在某些浏览器中显示(即在桌面上的 Chrome 中,但在 iOS 上的 Safari 中不显示)

javascript - 在 Plotly R 中添加带有选择框 Angular 的永久阴影矩形

javascript - 更改文本框输入的背景颜色在为空时不起作用

javascript - 是否可以将带有参数的函数作为 prop 传递,然后将该函数作为 prop 引用?

html - 为什么在 chrome 中按钮看起来不如在 firefox 中好?

css - Chrome 和 Mozilla 之间的边距不同

google-chrome - 如何在没有扩展程序的情况下在 Chrome 中测量像素?

javascript - Node.JS 作业/后台进程和高可用性