44

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 ?

mmcglynn
  • 6,876
  • 15
  • 45
  • 73
Awea
  • 3,128
  • 8
  • 37
  • 59

6 Answers6

45

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 ;)

David Martin
  • 646
  • 8
  • 10
30

try using as follows

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

this should work.

shana
  • 1,747
  • 1
  • 20
  • 17
  • 1
    This is what worked for me. Using CSS did not work, but put right in the first div of the new page worked. – Robb Sadler Nov 20 '15 at 00:51
  • 2
    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. – myrcutio Jan 13 '16 at 18:23
14

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.

bref27
  • 141
  • 1
  • 2
  • 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 body – TDH Feb 06 '19 at 15:17
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

Community
  • 1
  • 1
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;}
Manivasagan
  • 197
  • 1
  • 5
  • 16
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)

Malavan P
  • 68
  • 6