简体   繁体   English

使用page-break-inside时,在分页符时添加标题:避免使用CSS或JavaScript还是从浏览器中删除?

[英]add header when page breaks when using page-break-inside: avoid wth css or javascript or from browser?

i have a page that has sections as divs with inner divs and content. 我有一个页面,其中包含具有内部div和内容的div部分。 The numberof divs varies alot from less than a page to many pages when printing. 打印时div的数量从不到一页到很多页不等。

I am using page-break-inside: avoid on every section div so that all sections are never printed / split accross 2 pages. 我正在使用page-break-inside:避免在每个div分区上使用,以使所有部分都不会打印/拆分为2页。 (This i only get to work in firefox but that whole other story....!). (这只是我在firefox上的工作,但还有其他整个故事……。)。

Problem is i want to add a header image to the top of each page when printed but using the page-break-inside: avoid css property i dont know where to add the headersasthis is worked ou when printing. 问题是我想在打印时在每个页面的顶部添加页眉图像,但要使用page-break-inside:避免使用css属性,我不知道在哪里添加页眉,因为这在打印时有效。

Any one know ow i can acheive this ? 有人知道我能做到吗? can i somehow find out where the page breaks are going to be and add header there ? 我能以某种方式找出分页符的位置并在其中添加标题吗? or is there a way of setting header image in the brower like you can to word documents etc ? 还是有一种在浏览器中设置标题图像的方式,例如可以对文档进行文字处理?

please help thanks alot rick 请帮忙谢谢很多里克

It's impossible to control the printed page from JS/HTML/CSS because you don't have any access to the print driver so you can not know what paper size or margins the user has set. 无法从JS / HTML / CSS控制打印页面,因为您无权访问打印驱动程序,因此您无法知道用户设置的纸张尺寸或边距。

To get around this you could use a component to create a PDF from your HTML so you will have more control of the layout. 为了解决这个问题,您可以使用组件从HTML创建PDF,这样您就可以更好地控制布局。 That would have to be done server-side. 那必须在服务器端完成。

I don't see any way of add header information using CSS2 ( spec ). 我看不到使用CSS2( spec )添加标头信息的任何方式。 There is a way to add TEXT to a "page header" using CSS3 ( link , spec ). 有一种使用CSS3将文本添加到“页面标题”的方法( linkspec )。 I'm not sure if this will also cover images, but it's a start. 我不确定这是否还会涵盖图像,但这只是一个开始。 You will need to consider CSS3 adoption and if it suits your business needs (from wikipedia link it looks like only Opera supports the @page option, but that info could be stale). 您将需要考虑采用 CSS3,如果它适合您的业务需求(从Wikipedia链接看来,只有Opera支持@page选项,但是该信息可能是过时的)。

I don't think you could use javascript to inject headers onto each page when it's being formatted for print. 我认为在格式化打印页面时,您无法使用javascript将标头注入到每个页面上。 I don't see how that could be done (but I could be wrong on this one). 我不知道该怎么做(但是我可能错了)。

What you could do, is define a div that is invisible on a browser page, but shows up on a printed page. 您可以做的是定义一个在浏览器页面上不可见但在打印页面上显示的div。 Something like this should do it: 这样的事情应该做到:

<STYLE type="text/css">
@media print {
div.header {display:block;}
}
@media screen {
div.header {display:none;}
}
</STYLE>

You could then place a div containing your header after a forced page break. 然后,您可以在强制分页符之后放置一个包含标题的div。 This should work for you, further CSS could probably be used to force this div to align with the top of the page etc. Of course, this then also requires you to use forced page breaks, which means you need to know in advance how much data you can fit to a page, not ideal. 这应该为您工作,可能会使用进一步的CSS来迫使该div与页面顶部对齐,等等。当然,这还要求您使用强制分页符,这意味着您需要提前知道多少您可以适合页面的数据,并不理想。

Hope this helps. 希望这可以帮助。

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

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