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.
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?