简体   繁体   English

打印带有分页符的html页面

[英]Print html pages with page-break

i have data for create table and print it. 我有用于创建表并打印的数据。 The structure is like : 结构如下:

Title Data Sum 标题数据总和

The title must not be at the end of page and the Sum must not be at the start of page when print it. 打印时,标题不得位于页面末尾,总和不得位于页面起始处。 Thant is why i made html table with Divs and give all size with mm. 这就是为什么我用Divs制作html表,并用mm给出所有大小。 but i couldnt colculate when break page. 但我无法收集中断页面时的信息。 couse in different computers height changes. 因为在不同的计算机上高度变化。

here is my javascript code for calculate and break page 这是我的用于计算和中断页面的javascript代码

$(document).ready(function(){
        var mydiv ='<div class="page-break"></div>';
        var i=0;
        var h = px2cm($("#myinfo").last().height())*10;
        var limit=297;
        var g =0;

        $('#content div.ptr').each(function(){
            g=0;
            h = h + px2cm($(this).height())*10;
            var nexth=px2cm($(this).next().height())*10;
            if(h>=limit-nexth)
            {               
                if($(this).hasClass("ptrtitle")){
                    $(this).before(mydiv);
                    h = px2cm($(this).height())*10;
                    i++;
                    g=1;
                }else{                  
                    if(String($(this).next().attr("class"))==="ptr ptrsum"){
                        if($(this).prev().hasClass("ptrtitle"))
                        {
                            $(this).prev().before(mydiv);
                            h = px2cm($(this).height()+$(this).prev().height())*10;
                        }else{
                            $(this).before(mydiv);
                            h = px2cm($(this).height())*10;
                        }
                        //h=0;
                        i++;
                        g=1;
                    }else{
                            $(this).after(mydiv);
                            h=0;i++;
                    }
                }
            }
        });
    });
    function px2cm(px) {
      var d = $("<div/>").css({ position: 'absolute', top : '-1000cm', left : '-1000cm', height : '1000cm', width : '1000cm' }).appendTo('body');
      var px_per_cm = d.height() / 1000;
      d.remove();
      return px / px_per_cm;
    }

here i add limit=297(for A4 height, it works good on some computers, but doesnt work good on others). 在这里,我添加了limit = 297(对于A4高度,它在某些计算机上效果很好,但在其他计算机上效果不好)。 doesnt work good means that, on the first computer it prints 40 row in one page, but on another some rows go to the next print page 不能正常工作意味着,在第一台计算机上它在一页上打印40行,但是在另一台计算机上,几行转到下一个打印页

How can i normally print data in all computers 我如何正常在所有计算机上打印数据

Try adding css2 property page-break for your class page-break with @media print instead of calculating page break manually. 尝试使用@media print为类page-break @media print添加css2属性page-break @media print而不是手动计算分页@media print Also different scale can be used specific to print if needed. 如果需要,也可以使用不同的比例来专门print

@media print {
    .page-break {page-break-after: always;}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM