11

I'm working on Symfony2 project to generate PDF from HTML view. Following is from config.yml

knp_snappy:
pdf:
    enabled:    true
    binary:     /path/to/my/wkhtmltopdf
    options:
        no-stop-slow-scripts: ~
        enable-javascript: ~
        use-xserver: ~
        page-size: A4
        dpi: 300

Now I want to know if the DIV is overlapping on the page edges, if so add a page break before DIV. It is currently displaying as follow.

enter image description here

I've tried to get the height of DIVs and compare it with height of page, but it didn't work.

Is there any solution to know when DIV is overlapping or auto breaking the page when something is overlapping ?

Alok Patel
  • 7,842
  • 5
  • 31
  • 47

2 Answers2

16

Try to add style "page-break-inside" to your div:

<div style="page-break-inside: avoid;">
    ... content ...
</div>

wkhtmltopdf uses webkit engine. This fact allows you to use styles to control the looks of your PDF.

dmnptr
  • 4,258
  • 1
  • 20
  • 19
  • I've already tried this, page-break-inside. But it seems not working, may be it unable to determine when particular DIV is overlapping. – Alok Patel Oct 08 '15 at 18:39
  • 1
    This solved my problem. I was giving 'auto' for page-break-inside, 'avoid' works like a charm! – Alok Patel Oct 10 '15 at 06:01
  • I had been fighting with this for hours!!! this completely made my day. thank you – Splendonia Jun 29 '16 at 19:05
  • FYI I think this will not work if parent has `display: flex` https://stackoverflow.com/questions/34534231/page-break-insideavoid-not-working – Waqleh May 07 '21 at 18:35
7

I use Bootstrap for layout styling. To do page breaks works only (for me):

<p style="page-break-after: always;"/>
<br/>
Damian
  • 219
  • 5
  • 14