Why doesn't wkhtmltopdf page-break-after have any effect?
Asked Answered
G

6

46

I'm using wkhtmltopdf 0.10.0 rc2 for Mac

I have an html like this one :

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <link href="print.css" rel="stylesheet">
    <style type="text/css" media="screen,print">
      .break{
        display: block;
        clear: both;
        page-break-after: always;
        border :1px solid red
      }
      .page-breaker {
      display: block;
      page-break-after: always;
      border :1px solid red
      }
    </style>
  </head>
  <body>
    <div class="container break">
      page 1
    </div>
    <div class="page-breaker"></div>
    <div class="container">
      page 2
    </div>
  </body>
</html>

I simply try :

wkhtmltopdf test.html test.pdf

But it didn't produce a page-break, I doing something wrong ?

Gober answered 3/12, 2012 at 21:38 Comment(11)
I'm using wkhtmltopdf 0.9.9 on OSX, and for me, your markup produces a 3-page PDF. Page 1 is the words 'page 1' surrounded by a red box, page 2 is a plain red line (empty div page-breaker), and page 3 is the words 'page 2' with no outline.Copyist
You're right it work correctly with wkhtmltopdf 0.9.9Gober
Working correctly with wkhtmltopdf 0.11.0 rc2 here as well (Win 7).Knorring
Doesn't work in 0.12.0Grandfatherly
@Nenotlep I don't see 0.11.0 RC2 or 0.12.0, links?Chishima
@PhillPafford There is some kind of strange issue with the version numbers. When I get wkhtmltox-0.11.0_rc1-installer.exe From code.google.com/p/wkhtmltopdf and install it and then run wkhtmltopdf.exe --version I get wkhtmltopdf 0.11.0 rc2. So that's the version I meant. Sorry for the confusion, but I don't know what to call the version :)Knorring
As for 0.12.0, this is the first I have heard of one and I don't know where to search for it either.Knorring
Works with wkhtmltopdf 0.11.0 rc1 on Debian GNU/Linux.Mcgraw
I had similar issues. Got it to work by using page-break-before:always instead. You could try to apply it to the second page and see if it helps.Olcott
Same issue with 0.12.0 but worked fine 0.11.0 rc1Andante
Doesn´t work in 0.9.2 but works in 0.12.2.1Rosemarierosemary
C
47

Possibly unrelated as your pdf generated ok with an earlier version of wkhtmltopdf. Either way, I had similar issues with page breaks not being applied correctly. My problem was parent elements of the page-breaked element having an overflow other than visible. This fixed my issue:

* {
  overflow: visible !important;
}

Of course, you can be more specific about the tags this applies to ;)

Coan answered 5/2, 2014 at 10:4 Comment(5)
Thanks to add more info to this. ;)Gober
You're a true hero :)Lucarne
Floats can also cause this problem, which can be fixed with float: none.Armalla
I cannot believe that this is what it took to make it happen. Thanks!Qualify
You are a man!!! thanks a lot. If I can give you gold or something, reputation let me know.Anderlecht
P
31

try using as follows

 <div style="page-break-before:always;">
   //your content
</div>

this should work.

Plosion answered 8/6, 2015 at 17:6 Comment(2)
This is what worked for me. Using CSS did not work, but put right in the first div of the new page worked.Haywire
this worked for me, using the latest wkhtmltopdf 0.12. My guess is it doesn't fully render the CSS before it decides where the page breaks are, kind of annoying.Wellintentioned
D
15

I am usinf wkhtmltopdf 0.12.3.2

For me page-break-after works when a border is set, and when the breaker div is an immediate child of body.

.page-breaker {
    clear: both;
    display: block;
    border :1px solid transparent;
    page-break-after: always;
}

break-break-before does not work.

--print-media-type not needed.

Dubious answered 16/9, 2016 at 8:2 Comment(1)
This worked for me, I didn't need the border but I had to ensure that the element I was using for the break was an immediate child of the bodyDistaste
D
4

I am using version wkhtmltopdf 0.12.0

For me, page breaks ONLY work with --print-media-type. Without it, page break protection for images works, but not page-break-after or before.

I had to make a special css file for print media to get it work.

Setting the paper size to 'A3' or using the 'overflow: visible' didn't make any difference.

Also see WKHTMLTOPDF with pdfkit on Rails ignoring table page breaks

Danseuse answered 21/2, 2015 at 18:45 Comment(0)
A
1

It is working fine after remove media print

Before:

@media print {
    .page-break { height:0;page-break-after: always; margin:0; border-top:none;}
}

above code not working in new version.

Now

.page-break { height:0;page-break-after: always; margin:0; border-top:none;}
Applicator answered 2/1, 2018 at 11:38 Comment(0)
H
1

Update the wkhtmltopdf to version 0.12.5. Page break issue not occuring for me after updating.

Use --disable-smart-shrinking to avoid empty white space ( If you have any)

Use --zoom <value> to avoid page page (If entire page not showing)

Hepzi answered 13/12, 2019 at 12:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.