简体   繁体   English

在HTML中创建分页符

[英]Creating a Page Break in HTML

I am creating a resume using html and some elements have multiple lines. 我正在使用html创建一个简历,一些元素有多行。 For example, an education can have the institute, the date attended, and the degree received on separate lines. 例如,教育可以拥有学院,参加的日期和分开的学位。 However, the institute, the date, and the degree are all part of one record. 但是,学院,日期和学位都是一个记录的一部分。

I am trying to create a page break when I print using html. 我正在尝试使用html打印时创建分页符。 I only want the page break to be inserted if the number of lines in the next element is greater than the number of lines remaining on the 8.5 x 11 page. 如果下一个元素中的行数大于8.5 x 11页上剩余的行数,我只希望插入分页符。 Using the example earlier, I want all of the lines in the education record all on one page. 使用前面的示例,我希望教育记录中的所有行都在一个页面上。 The data is being passed in, so the page breaks can vary from resume to resume. 数据正在传入,因此分页符随着从简历到恢复而变化。

I have found code to create the page break: @media print {footer {page-break-after: always;}} . 我找到了创建分页@media print {footer {page-break-after: always;}}代码: @media print {footer {page-break-after: always;}}

I have also found code that can get the line height of a div: 我还找到了可以获得div的行高的代码:

var element = document.getElementById('content'); document.defaultView.getComputedStyle(element,null).getPropertyValue("lineHeight");

I need to count the number of lines remaining on the 8x11 page and compare that height to the height of the next element. 我需要计算8x11页面上剩余的行数,并将该高度与下一个元素的高度进行比较。 Instead of using page breaks, another option would be to just add enough empty lines to move the element down enough for it to be all on one page, but I still need to be able to count the remaining lines. 而不是使用分页符,另一种选择是添加足够的空行来将元素向下移动到足以使它全部在一个页面上,但我仍然需要能够计算剩余的行数。 I would appreciate any suggestions. 我将不胜感激任何建议。 Thanks. 谢谢。

EDIT: Here is my code so far: 编辑:这是我的代码到目前为止:

var totalHeight = 1056;
var divHeight = document.getElementById('element').offsetHeight;
totalHeight = totalHeight - divHeight;
if(totalHeight < 0)
{
    document.write("<style>");
    document.write("@media print {#element {page-break-after: always;}}");
    document.write("</style>");
}

However, when I print out the total height it only returns a number slightly over 200. Instead, the number should exceed 1056. Does the offsetHeight method only return the size of the text itself and not the spacing around it or is there another problem that could be causing the drastic difference? 但是,当我打印出总高度时,它只返回一个略超过200的数字。相反,数字应该超过1056.farkationHeight方法只返回文本本身的大小而不是它周围的间距或是否有另一个问题可能会造成巨大的差异? Thanks. 谢谢。

I think the trickiest part here is dealing with whatever margin the browser will put on the page when it goes to print. 我认为这里最棘手的部分是处理浏览器打印时页面上的任何边距。 Not sure if you can control that. 不确定你是否可以控制它。 But if you can, try using in as your sizing unit instead of something like px or em . 但是,如果可以,请尝试使用in作为您的大小单元而不是pxem Then, if you calculate that you're going to go over 11in with what you've got, add your page break. 然后,如果你计算出你已经超过了11 11in ,那么添加你的分页符。

Here's a pseudo-code algorithm: 这是一个伪代码算法:

var totalHeight = page margin

foreach div:
    totalHeight += calculated height in inches

    if totalHeight > 11in:
        insert page break

rinse and repeat for further pages

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

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