打印时防止表格分页

发布于 2024-11-06 11:08:07 字数 705 浏览 0 评论 0原文

我有一个页面正在尝试设置用于打印。该页面包含大量单独的表格。这些表格的大小各不相同,但一般来说,我可以在每页上放置 2.5 到 3 个表格。我希望能够防止表格因分页符而损坏。知道我怎样才能做到这一点吗?

我尝试了这个:

.reportTable {
    page-break-inside: avoid;
}

不幸的是, page-break-inside 似乎仅在 Opera 中受支持(根据 W3Schools - 我证实这在 Firefox 4.0.1 中不起作用)。

我可以这样做,以在每个表格之后强制分页:

.reportTable {
    page-break-after: always;
}

这可以插入分页符,并且似乎在所有主要浏览器中都受支持,但它在打印文档上留下了大量浪费的空间(大约每个表格的一半)页面为空白)。如果整个下一个表格不适合本页,我真的只需要分页符。

我知道我的用户使用 Internet Explorer、Firefox 和 Safari,所以我真的很想尽可能地支持这些产品。找到同样适用于 Chrome 和 Opera 的东西将是一个非常好的奖励。

有什么想法吗?

I have a page that I'm trying to set up for printing. This page contains a large number of individual tables. The tables are of varying size but, in general, I can fit 2.5 to 3 tables on each page. I'd like to be able to prevent the tables from being broken by a page break. Any idea how I can accomplish that?

I tried this:

.reportTable {
    page-break-inside: avoid;
}

Unfortunately, page-break-inside only seems to be supported in Opera (according to W3Schools - I verified that this doesn't work in Firefox 4.0.1).

I can do this to force a page break before after every single table:

.reportTable {
    page-break-after: always;
}

This works to insert the page breaks and seems to be supported in all major browsers, but it leaves me with tons of wasted space on the printed documents (roughly half of each page is blank). I really only want a page break if the entire next table won't fit on this page.

I know that I have users utilizing Internet Explorer, Firefox, and Safari so I'd really like to support those as much as possible. Finding something that would also work in Chrome and Opera would be a very nice bonus.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深海蓝天 2024-11-13 11:08:07

我也一直在寻找这个问题的答案。我最接近的是知道一个页面上可以容纳多少行输出,然后计算该页面有多少行输出。在你的情况下:
1)计算出一页上可以容纳多少行输出。
2) 通过显示第一个表格来跟踪您已经使用了多少行。
3)计算表2需要多少行。将其添加到表 1 的行中,看看您是否仍低于近似阈值。如果是,则显示该表格,如果不是,则放置一个带有分页符的 div:始终在其中以强制创建一个新表格。

这将为您提供大约您正在寻找的内容,但它不会是完美的。每隔一段时间,您就会有一个“可以”适合上一页的表格,但只是没有完全满足要求,因为您必须估计一页上适合多少行。

然而,我还没有找到一种方法来考虑单元格内的内容或类似内容在被挤入打印输出页面时是否会换行成新行。

希望这能激发您的灵感。

I've also been looking for an answer to this. The closest I came is to knowing approximately how many lines of output would fit on a page, then calculating how many lines of output the page had. In your case:
1) figure out how many lines of output you can fit on a page.
2) keep track of how many lines you've used already by displaying your first table.
3) calculate how many lines table 2 will take. Add it to table 1's lines and see if you're still below your approximate threshold. If you are, display the table, if not, put a div down with the page-break:always in it to force a new table.

This would give you approximately what you are looking for, but it won't be perfect. every once and a while, you'll have a table that "could" have fit on the previous page, but just didn't quite make the cutoff because you have to be on the low side of estimating how many lines fit on a page.

I haven't however figured out a way to facter in if the content inside a cell or something like that will wrap around into a new line when smushed into a printout page.

Hope that sparks an idea for you.

顾北清歌寒 2024-11-13 11:08:07

目前似乎没有办法强制不支持page-break-inside:void的浏览器这样做。

但是,由于您每页可以容纳 2.5 到 3 个表格,并且不希望使用 page-break-after:always; 只打印单个表格,因此您可以选择插入一个特殊的 div,强制在每两个表格之后分页。

因此,您可以包含

并在屏幕上隐藏它,但在打印时显示它。您可以为其指定 page-break-after:always; 样式。这样,每页至少可以获得两个表格。

另一个建议是让用户决定是否要每页打印一个表格或打印尽可能多的表格(有些表格可能会被拆分到页面上)。您可以切换复选框以将page-break-after:always;样式添加到表格中。

At present, there seems to be no way to force the browsers that don't support page-break-inside: avoid to do so.

However, since you can fit 2.5 to 3 tables on each page and prefer not to print just a single table using page-break-after: always;, you could opt to insert a special div that forces a page break after every two tables.

So you would include <div class="pageBreak"></div> and hide it for the screen but display it for printing. And you would give it a style of page-break-after: always;. In this way, you get at least two tables per page.

Another suggestion would be to let the user decide whether or not he/she wants to print one table per page or as many as can fit (with some possibly being split over the pages). You can toggle a checkbox to add the page-break-after: always; style to the tables.

巷雨优美回忆 2024-11-13 11:08:07

要解决这个问题,只需使
#table{page-break-after:auto;}

to fix this just make
#table{page-break-after:auto;}

长发绾君心 2024-11-13 11:08:07

这是一个非常老的问题,所以只想更新里面的分页符:避免;现在大多数主流浏览器都支持。尽管确保 page-break-xxx 工作有一些怪癖(任何级别的父级都不能有position:fixed,元素和直接父级需要是position:relative和display:block等)。

参考:
https://developer.mozilla.org/en- US/docs/Web/CSS/page-break-inside

https://developer.mozilla.org/en-US/docs/Web/CSS/break-inside

This is a very old question, so just wanted to update that page-break-inside: avoid; is now supported in most major browsers. Though there are some quirks to making sure page-break-xxx works (NO parent at any level can have position: fixed, the element and direct parent need to be position: relative and display: block, etc.).

Reference:
https://developer.mozilla.org/en-US/docs/Web/CSS/page-break-inside

https://developer.mozilla.org/en-US/docs/Web/CSS/break-inside

遮了一弯 2024-11-13 11:08:07

并非所有打印机都是一样的。

您遇到问题是因为打印机不受 Web 浏览器或 html 代码控制。它由打印机附带的打印机驱动程序控制。此功能(及其设置)属于渲染页面的计算机的所有者,而不是您。

您的代码无法提前知道连接到用户系统的打印机可以在一页上打印多少行,或者打印机将如何布局表格。如果使用不同打印机的用户打开页面将会有所不同。就像不同的屏幕分辨率一样,打印机像素分辨率也不同。

因此,适用于不同屏幕的所有规则(及其缺点)也适用于不同的打印机。您不仅无法知道打印机将在何处断页,甚至无法知道打印的页面有多大,即页面上可以容纳多少内容。

要将所有表格(或多个表格)打印到一页上,用户应选择要打印的部分,然后使用打印机对话框中的打印选择。

Not all printers are created equally.

You are having problems because the printer is not controlled by either the web browser or the html code. It is controlled by the printer driver that came with the printer. This function (and its settings) belongs to the owner of the computer rendering the page, not to you.

Your code can not know in advance how many lines the printer attached to the user's system can put on a page, or how the printer will lay out a table. It will be different if a user with a different printer opens the page. Just like different screen resolutions, there are different printer pixel resolutions.

So all of the rules that apply to different screens (and their disadvantages) also apply to different printers. Not only can't you know where the printer will break a page, you can't even know how large the printed page is, in terms of how much content fits on a page.

To get all of a table (or multiple tables) onto a page, the user should select the parts he wants to print, and then use Print Selection on the printer dialog box.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文