简体   繁体   English

有什么办法用javascript(印刷媒体)进行分页符吗?

[英]Any way to do page breaks with javascript (print media)?

Browser support for page-break-inside: avoid is poor. 浏览器对page-break-inside: avoid支持page-break-inside: avoid不佳。 There are lots of scenarios where it's not applied. 在很多情况下,都没有应用它。 In my case it's a nested flexbox with flex-wrap . 在我的情况下,这是一个带有flex-wrap的嵌套flexbox。

Is there any way to add a page break using javascript? 有没有办法使用JavaScript添加分页符?

I can detect if the browser is in print mode with onbeforeprint event in FF, or window.watchMedia on Chrome, but what next? 我可以通过FF中的onbeforeprint事件或Chrome中的window.watchMedia来检测浏览器是否处于打印模式,但是接下来该怎么办?

I guess I need to find out what's the printed page size in pixels, so I can determine the position where to insert the page break. 我想我需要找出打印的页面大小(以像素为单位),以便确定插入分页符的位置。 But window.screen.availHeight returns the same value and window.print.availHeight does not exist :( 但是window.screen.availHeight返回相同的值,而window.print.availHeight不存在:(

Assuming this is possible somehow, how do I do the page break then? 假设以某种方式可行,那么我该如何分页?

You can try a calculation, A4 has a certain proportion between width and height, so simply said if your print css makes the html/body 900px width. 您可以尝试计算,A4在宽度和高度之间有一定比例,因此简单地说,如果您的打印CSS使html / body的宽度为900px。 The height for each print page can be calculated by using the A4 proportions. 可以使用A4比例计算每个打印页面的高度。

So you can add a print css file that changes the layout so that the content that needs to be on the next page has a margin top. 因此,您可以添加一个可更改布局的打印css文件,以便需要在下一页上显示的内容具有页边距。

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

相关问题 我要打印页面时javascript中断 - javascript breaks when I want to print a page 使用javascript打印时,如果页面中断时如何添加标题? - how to add header when page breaks when print using javascript? 有什么方法可以用javascript清除页面吗? - Is there any way to clear the page with javascript? JavaScript:有没有不用换行符打印输出的方法? - JavaScript: Is there any way to print outputs without newline? 有没有办法通过javascript找出Chrome的媒体设置环境? - Is there any way to figure out Chrome's media settings environment by javascript? 有什么办法用JS和PHP进行“后端媒体查询”? - Any way to do “backend media queries” with JS and PHP? 有什么办法可以防止使用javascript进行页面页面加载? - Is there any way to prevent page load of images with javascript? 有没有办法使用javascript读取页面中的图像 - Is there any way to read the image in the page using javascript 是否有任何方法可以执行与 jquery 中的 method.load() 相同的作用,以获取带有香草 Javascript 的 html 页面的一部分 - Is There any way to do the same role that the method .load() in jquery does to get part of html page with vanilla Javascript JavaScript @media打印样式表不起作用 - JavaScript @media print stylesheet not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM