最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Printsave page as PDF, but if an element would get cut off then add a page break before it - Stack Overflow

programmeradmin0浏览0评论

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:

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>

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:

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().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.

Share Improve this question asked Jul 24, 2019 at 21:01 Merlin04Merlin04 2,3274 gold badges24 silver badges31 bronze badges 1
  • Have you tried adding the style "page-break-after: always;" to your div element styles? It sounds like this should be added to every div, not just the place where a page break is expected to happen. Found here: stackoverflow./questions/2117788/page-break-in-html2pdf – Joseph Commented Jul 24, 2019 at 21:06
Add a ment  | 

1 Answer 1

Reset to default 9

Have you tried using regular CSS and setting the

div {page-break-inside: avoid}

property for your divs to see if it avoids creating a page break inside them?

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论