简体   繁体   English

浮动的HTML / CSS打印分页符不起作用(例如:引导程序)

[英]HTML/CSS printing page-break with float not working (eg: bootstrap)

NOTE: Asking and answering it myself because I've spent 3 days trying different methods to find a solution, hoping this helps someone. 注意:我自己问和回答,因为我花了3天的时间尝试各种方法来找到解决方案,希望这对某人有所帮助。 The question has been asked before but they are old and the answers are unclear or unsatisfactory. 之前曾有人问过这个问题,但它们太旧了,答案还不清楚或不令人满意。

Trying to add a page-break when printing with elements that have float, eg: using bootstrap grid, doesn't work. 当使用具有浮点的元素进行打印时,尝试添加分页符,例如:使用引导网格,将不起​​作用。 The page-break is ignored. 分页符将被忽略。

<div style="float:right;">floating div</div>

<div class="col-xs-12">
  <div class="row">
    <div class="col-xs-12 break-after">
      Add page break after this element when printing
    </div>
    <div class="col-xs-12">
      This should be printed on next page
    </div>
  </div>
</div>

<style type="text/css">
.break-after {
  page-break-after: always;
}
</style>

Firstly, page-break doesn't work inside absolutely positioned elements so make sure your page-break isn't inside one. 首先,分页符不能在绝对定位的元素中使用,因此请确保分页符不在一个元素中。

Now, since float is causing the page-break to be ignored, we need to clear the float. 现在,由于浮动导致分页符被忽略,因此我们需要清除浮动。

The problem is that clear: both; 问题很clear: both; doesn't work if the element with page-break-after: always; 如果带有page-break-after: always;的元素page-break-after: always;不起作用page-break-after: always; is a float (eg: .col-xs-12 in bootstrap). 是一个浮点数(例如:bootstrap中的.col-xs-12 )。

The trick is to add 2 new divs with the clear and page-break after the div you want to add the page-break to: 诀窍是要在要向其添加分页符的div之后添加带有clearpage-break符的2个新div:

<div class="page-break-clear"></div>
<div class="page-break">&nbsp;</div>

<style type="text/css">
.page-break-clear { 
  clear: both;
}
.page-break {
  page-break-after: always; /* depreciating, use break-after */
  break-after: page;
  height: 0px;
  display: block!important;
}
</style>

Putting it together: 把它放在一起:

<div style="float:right;">floating div</div>

<div class="col-xs-12">
  <div class="row">
    <div class="col-xs-12 break-after">
      Add page break after this element when printing
    </div>
    <!-- These 2 new added divs will clear float and add the page break -->
    <div class="page-break-clear"></div>
    <div class="page-break">&nbsp;</div>

    <div class="col-xs-12">
      This should be printed on next page
    </div>
  </div>
</div>

<style type="text/css">
.page-break-clear { 
  clear: both;
}
.page-break {
  page-break-after: always; /* depreciating, use break-after */
  break-after: page;
  height: 0px;
  display: block!important;
}
</style>

as a side-note: I recommend removing the page-break-after css for .break-after class that was supplied in the question to prevent doubling up on page-breaks incase the floats are removed and it actually works. 作为旁注:我建议删除问题中提供的.break-after类的page-break-after css,以防万一删除浮点数并且它确实有效时,分页符增加一倍。

Also, to make your life easier if you are using jquery and don't want to manually add those 2 page-break divs, just run the following code which will automatically add them after all elements with the class .break-after : 另外,如果您使用的是jquery并且不想手动添加这两个分页.break-after div,则可以使您的生活更加轻松,只需运行以下代码即可自动将它们添加到所有带有.break-after类的元素.break-after

$('.break-after').after('<div class="page-break-clear"></div><div class="page-break">&nbsp;</div>');

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

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