简体   繁体   English

如何在打印的 HTML 的每一页顶部放置一个空格

[英]How to put a blank space atop of each page of a printed HTML

I've designed an HTML report with several div s and table s.我设计了一个带有几个divtable的 HTML 报告。 Now my client asks me to put a repeating header at the top of each printed page.现在我的客户要求我在每个打印页面的顶部放置一个重复的 header。 This is not possible using plain CSS and HTML as far as I know.据我所知,使用普通的 CSS 和 HTML 是不可能的。 Just as a try, I put header div element inside a thead to which I applied display: table-header-group in order to be displayed, and put all other elements of the report as rows of the main table but no success.作为尝试,我将 header div 元素放在我应用display: table-header-group的thead中以便显示,并将报告的所有其他元素作为主表的行但没有成功。

  1. A workaround is to use @print {.header {position: fixed; top: 10px} }一种解决方法是使用@print {.header {position: fixed; top: 10px} } @print {.header {position: fixed; top: 10px} } in order to repeat.header element at the top of each page. @print {.header {position: fixed; top: 10px} }以重复每个页面顶部的 header 元素。 But for this work, we should put a blank space at the top of each new page;但是对于这项工作,我们应该在每个新页面的顶部放置一个空格; otherwise fixed header element is mixed with other elements at the top of table.否则固定 header 元素与表格顶部的其他元素混合。

  2. As another workaround I can compute elements height at render time and put manual page breaks where needed.作为另一种解决方法,我可以在渲染时计算元素高度,并在需要的地方放置手动分页符。 So I want to know if there is any Javascript library available to execute at page load and computes all render-time heights of div elements of the page and put a zero-height div element with page-break-before: always;所以我想知道是否有任何 Javascript 库可用于在页面加载时执行并计算页面 div 元素的所有渲染时高度,并在page-break-before: always; before each div which exceeds height of an A4 paper.在每个超过 A4 纸高度的 div 之前。 For suppose the following divs result in 14, 10, 8, 9, 6, 13, and 6 centimeter height at render time.假设以下 div 在渲染时的高度分别为 14、10、8、9、6、13 和 6 厘米。 I want the library put a page-break dummy dive element at specified locations:我希望图书馆在指定位置放置一个分页符虚拟潜水元素:

<div id="d1">...</div>

<div id="d2">...</div>

<,-- here, because 14+10+8 exceeds 30cm -->

<div id="d3">...</div>

<div id="d4">...</div>

<div id="d5">...</div>

<,-- here, because 8+9+6+13 exceeds 30cm -->

<div id="d6">...</div>

<div id="d7">...</div>

Have you considered splitting the table over multiple pages?您是否考虑过将表格拆分为多个页面? I've done this in the past by measuring how large a page would be and then writing out the rows to the browser.过去我通过测量页面的大小然后将行写入浏览器来完成此操作。 Each time the end of a page was reached, I'd close the table being written, then start the next one, writing a new DIV etc. You need to keep a track of the max size of each column etc. as you go, but efficiency wise we could write thousands of rows to the screen and would still typically return in well under 60 seconds for big reports.每次到达页面末尾时,我都会关闭正在写入的表,然后开始下一个表,编写一个新的 DIV 等。您需要跟踪每列的最大大小等,就像 go 一样,但在效率方面,我们可以在屏幕上写入数千行,并且通常仍会在 60 秒内返回大型报告。

I also used a CSS orientation feature only available in IE at the time (writing-mode:tb-rl) to mimic the page being rendered in Landscape and the content likewise - it does require a little more thought in how you're writing it, BUT the resulting content looked very professional.我还使用了当时仅在 IE 中可用的 CSS 方向功能(写作模式:tb-rl)来模拟在横向中呈现的页面和内容 - 它确实需要更多地考虑如何编写它, 但是生成的内容看起来很专业。

Here is my final solution.这是我的最终解决方案。 The following code is executed at page load.以下代码在页面加载时执行。 My page was solely composed of a number of divs.我的页面完全由许多 div 组成。 Two divs as headers and the other divs (containing tabular data) as details.两个 div 作为标题,其他 div(包含表格数据)作为详细信息。 I assumed always printing in portrait scheme and also assumed A4 size (= ~21x30cm).我假设总是以纵向方案打印,并且还假设 A4 尺寸(= ~21x30cm)。 I also assumed 4.5cm margins for left-right and top-bottom of the page.我还假设页面左右和上下的边距为 4.5 厘米。 The code first resizes divs to a portrait compatible width (so that all tables are divs are resized automatically).代码首先将 div 的大小调整为纵向兼容的宽度(以便所有表是 div 都会自动调整大小)。 Then I iterate over non-header div elements to add a page break when height exceeds A4 height.然后我遍历非标题 div 元素以在高度超过 A4 高度时添加分页符。 I also cloned and added header elements atop of each page through this code.我还通过此代码在每个页面的顶部克隆并添加了 header 元素。

// page width in pixels
function pw() {
    return cm2px(16.5);
}
// page height in pixels
function ph() {
    return cm2px(25.5);
}
function px2cm(px) {
    return px * 2.54 / 96;
}
function cm2px(cm) {
    return cm * 96 / 2.54;
}

$(function() {
    $('.section > div').each(function(){
        $(this).width(pw() + 'px');
    });
    hh = $('.section > div.heading');
    headingHeight = hh.eq(0).height() + hh.eq(1).height();

    h1 = hh.eq(0).clone();
    h2 = hh.eq(1).clone();

    var h = headingHeight;
    var pageHeight = ph();

    $('.section > div').not('.heading').each(function(){
        if (h + $(this).height() + 14 > pageHeight) {
            h1.css('page-break-before', 'always');
            $(this).before(h1.clone());
            $(this).before(h2.clone());
            h = $(this).height() + 14 + headingHeight;
        } else {
            h += $(this).height() + 14;
        }
    });
});

Try jquery to compute heights of div tags.尝试 jquery 来计算 div 标签的高度。 However, printing of html can usually be accomplished by using some simple php.然而,html的打印通常可以通过使用一些简单的php来完成。

Also the easiest way might be to create a separate css file for your printed page and make the background image of a div the header you want to use for your printed page.此外,最简单的方法可能是为您的打印页面创建一个单独的 css 文件,并将 div 的背景图像设置为您要用于打印页面的 header。 In your regular style sheet that same div could simple be empty and have no height or width.在您的常规样式表中,相同的 div 可以简单地为空并且没有高度或宽度。

This link is some company's explanation of how to use css for printing html此链接是一些公司对如何使用 css 打印 html 的说明

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

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