简体   繁体   English

将页面打印/另存为PDF,但是如果某个元素被剪切掉,请在其之前添加一个分页符

[英]Print/save page as PDF, but if an element would get cut off then add a page break before it

I have a page that I need to print as a PDF (either save as a PDF or just use the browser print dialog), similar to this: 我有一个页面需要打印为PDF(另存为PDF或仅使用浏览器打印对话框),类似于以下内容:

 div { outline: red solid 1px; } body { margin-left: 30vw; margin-right: 30vw; } 
 <div> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. </div> <br> <div> It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). </div> <br> <div> It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). </div> <br> <div> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. </div> 

The contents of the site is longer that would fit in a single page, so it gets cut off when I try to print it. 该网站的内容较长,无法容纳在单个页面中,因此当我尝试打印该页面时,它会被切断。 I want there to be a page break inserted before each segment if it would get cut in the middle, so that there are only page breaks in between the segments. 我希望在每个片段之前插入一个分页符,如果它在中间被切开,以便在片段之间只有分页符。 I'm currently trying to use html2pdf.js with the avoid-all page break mode: 我目前正在尝试将html2pdf.js与避免所有分页html2pdf.js模式一起使用:

html2pdf().set({
  pagebreak: { mode: ['avoid-all', 'css', 'legacy'] }
});

But this doesn't seem to do anything. 但这似乎无济于事。 I could use the manual tagging (adding the class html2pdf__page-break to elements that should be before a page break), but I modify this site frequently, and I don't want to have to go back every time I make a change to the site that makes elements longer or shorter. 我可以使用手动标记(将html2pdf__page-break类添加到应该在分页html2pdf__page-break之前的元素中),但是我经常修改此网站,并且我不想每次更改使元素变长或变短的站点。

Have you tried using regular CSS and setting the 您是否尝试过使用常规CSS并设置

div {page-break-inside: avoid}

property for your divs to see if it avoids creating a page break inside them? div的属性,以查看是否避免在其中创建分页符?

https://www.w3schools.com/cssref/pr_print_pagebi.asp https://www.w3schools.com/cssref/pr_print_pagebi.asp

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

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