Alternative for page-break-inside: avoid

2020-08-09 11:29发布

I have a page which generates coupons. Each coupon is a div with a height varying depending on the content. I want to print as many coupons on each page as possible, but I do not want the coupons to be split out over several pages. The CSS property page-break-inside does exactly what I need. However, I need this to work for Firefox and/or Chrome. And this is not supported. Two years and one year ago the same question was asked, and there was no good alternative for this. We are a lot of CSS3/HTML5/overall browser development further... is there an alternative to get this working?

Example is here: http://jsfiddle.net/e3U66/2/

Assume that a page, when printed, measures 1000px. I want the second DIV to appear on the second page, because otherwise it is split out over the first and second. This code works in Opera, but not in FF or Chrome.

4条回答
相关推荐>>
2楼-- · 2020-08-09 11:57

Below a solution made with the help of Prototype (1.7) library

<!DOCTYPE HTML>
<html>
<head>
<title>Title of the document</title>
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript">
//page height in px
//thisPageTotal is the total of pixels on the current page, in px
pageHeight = 1000;
thisPageTotal = 0;

Event.observe(window, 'load', function(){
    $$('.coupon').each(function(el){
        var layout = el.getLayout();
        thisPageTotal += parseInt(layout.get('margin-box-height'));
        if(thisPageTotal > pageHeight) {
            thisPageTotal = parseInt(layout.get('margin-box-height'));
            var pageBreak = new Element('div', {
                'class': 'pagebreak'
            });
            el.insert({before: pageBreak});
        }
        //this shows the current amount of px on the current page
        el.update(thisPageTotal);
    });
});
</script>

<style type="text/css">
div {
    border: 1px solid #000;
    margin: 30px;
}

.pagebreak {
    page-break-after: always;   
}
</style>
</head>

<body>
    <div id="div_1" class="coupon" style="height: 500px"></div>
    <div id="div_2" class="coupon" style="height: 200px"></div>
    <div id="div_3" class="coupon" style="height: 500px"></div>
    <div id="div_4" class="coupon" style="height: 200px"></div>
    <div id="div_5" class="coupon" style="height: 200px"></div>
    <div id="div_6" class="coupon" style="height: 400px"></div>
    <div id="div_7" class="coupon" style="height: 300px"></div>
    <div id="div_8" class="coupon" style="height: 400px"></div>
    <div id="div_9" class="coupon" style="height: 500px"></div>
    <div id="div_10" class="coupon" style="height: 200px"></div>
</body>
</html> 

Maybe it helps

查看更多
Deceive 欺骗
3楼-- · 2020-08-09 12:01

Honestly I would just advise creating images of the actually coupons or generating a pdf. I'm assuming you are probably generating barcodes for all the coupons already, so generating the actually images shouldn't be to hard using php (or whatever the code choice might be).

Here is some info on php image creation, but SO would probably be a better source for examples.

http://php.net/manual/en/function.imagecreate.php

Then you could just list the images.

<img src>
<img src>
<img src>
...

There's no sense it recreating the wheel.

查看更多
别忘想泡老子
4楼-- · 2020-08-09 12:08

set float left for these div and set width as 100%. i wont tryed it ., it's may work.

查看更多
一纸荒年 Trace。
5楼-- · 2020-08-09 12:13

Why not, after the page is loaded with your content, use js to scroll through the content and add up the height of the divs.

Once you've reached 1000px or whatever you've determined to be the page height, then insert a blank div styled with page-break-before before the latest div.

查看更多
登录 后发表回答