3

I am using wkhtmltopdf to convert my HTML to PDF. My concern is regarding the page-break-* : ignore; directives. I am trying to prevent the PDF from cutting to a new page right after a heading (among others), so the title and the content always stick together. Somehow, it really doesn't seem to work! I have tried in many different ways but the result is always the same, ignoring the CSS... any of the page-break-inside: avoid !important;, page-break-after: avoid !important; or page-break-before: avoid !important; seem to work.

Edit:

A sample of my code would be:

index.html

...

<h2>HEADING</h2>
<div class="toctree-wrapper compound"></div>
<p>content content content content content content content 
content content content content content content content content 
content content content content content content content content 
content content content content content content content content 
content content content content content content content content 
content content content </p>

...

style.css

...

p{
    background: green !important;  /* color to see where it breaks */
    page-break-before: avoid !important;
}

h1, h2, h3, h4, h5, h6{
    background: blue !important;  /* color to see where it breaks */
    page-break-after: avoid !important;
    page-break-inside: avoid !important;
}

.toctree-wrapper.compound{
    background: pink !important;  /* color to see where it breaks */
    page-break-after: avoid !important;
    page-break-inside: avoid !important;
    page-break-before: avoid !important;
}

Gives the following result:

enter image description here

Any ideas? Thanks!

  • put your code here – Manthan Patel Mar 20 '19 at 10:00
  • @ManthanPatel I just added a sample, thanks! – Begoña Álvarez de la Cruz Mar 20 '19 at 11:13
  • I think you go to wrong way, don't avoid pagebreak after heading but choose another way do pagebreak before heading so heading come to second page and content also their so they come together. [link](https://stackoverflow.com/q/1664049/7408350) I think the problem is on the first page you have 90 % another data with heading tag then you want to add 40% more data which is space is not their so it is not allowed you forcefully to avoid pagebreak because there is no space remaining. – Manthan Patel Mar 21 '19 at 18:51
  • The thing is that I cannot force a page break always before a heading, each page can have multiple headings, depends on the content. My issue is separating the heading from its first paragraph, that is what looks weird. Therefore, I need to **avoid** a page break after a heading, somehow, tell the document that if the heading doesnt fit with its first paragraph, better jumping to the next page so all stays together. – Begoña Álvarez de la Cruz Mar 22 '19 at 13:55
  • 1
    This is the best solution I found: https://stackoverflow.com/a/53742871/1927583 – ba_ul Jul 30 '21 at 16:48
  • @ba_ul that is indeed what I am using atm, a bit hacky but at least works ¯\_(ツ)_/¯ – Begoña Álvarez de la Cruz Jul 31 '21 at 17:59

0 Answers0