node-html-pdf icon indicating copy to clipboard operation
node-html-pdf copied to clipboard

page-break-inside: avoid is erratic

Open sjohnson32 opened this issue 7 years ago • 21 comments

I have a complex html template that I am converting to a pdf. It has icons created from several absolute positioned elements. I do not want the page to break in the middle of these icons. I am using css to tag the icon container with "page-break-inside: avoid". When I print the html from the browser, the page breaks work correctly so I'm confident the css is correct. But when the pdf is rendered this css is not honored and the page breaks split the icons. image

I am using the same css on another container that contains only text and the page breaks properly around it. So my question: is there anything in node-html-pdf that would keep certain elements from honoring the css? I have tried adjusting the page height, format, borders, css and have seen no change to the icon issue.

sjohnson32 avatar Jan 20 '17 16:01 sjohnson32

I'm using those css rules page-break-inside: avoid and page-break-before: always but It does not seems to work at all. I need to handle the page break mostly for big tables. Is there a solution for that ?

zedissime avatar Jan 23 '17 14:01 zedissime

Also unable to get custom fonts to work. Looks like we will be switching to a different tool unless these issue can be fixed.

sjohnson32 avatar Mar 01 '17 22:03 sjohnson32

@sjohnson32 I ended up using wkhtmltopdf. There is an npm package which make the interface between the binary and your node module. I can use custom font, and the page breaks (in or out of table) work like a charm. I don't know about the absolute elements, but you can give it a try.

zedissime avatar Mar 02 '17 07:03 zedissime

lol. I initially used this module because I wasn't able to get wkhtmltopdf to work at all. this module should work with custom fonts though.

Regarding the page-break-inside issue, you can try to use position: relative on the parent element and on the element you have the css rule. Apparently that worked for some people: https://github.com/ariya/phantomjs/issues/13524#issuecomment-269029693

Sorry for the issues. I can't do much to fix those. Maybe we can put together a list with all the issues, so people are aware of them when they try to use this module or another one based on phantomjs.

marcbachmann avatar Mar 02 '17 07:03 marcbachmann

@marcbachmann In fact the linked module is only an interface between the binary and the node application. You have two options, install wkhtmltopdf on your environment, or define the path of the binary to use. In my case it was the second option, as I'm making a nwjs application which must embed the binary. Anyway what you are proposing could also be a great option, in my case it wasn't really what I needed.

zedissime avatar Mar 02 '17 09:03 zedissime

image For me, it breaks in the middle of line 14, I have no idea to prevent this although I also tried with page-break-inside or page-break-after. It's better if the line 14 will be in next page. Any idea?

ducdev avatar Mar 02 '17 17:03 ducdev

It just ignores the page-break-inside property....

gur111 avatar Oct 17 '17 18:10 gur111

I have the same issue with WKHTMLTOPDF 0.12.4, regardless of browser used. in a webapp. Even when I say media="print" the print-window/-console does not adhere to the setting, in any browser on Windows. Very frustrating, even making all elements display:block; (which is a prerequisite). It does not matter if I try this on a Hx, p, table, tr, td or div, on all HTML-tags this CSS-setting is ignored. Anyone having this working anywhere?

The settings page-break-before and page-break-after at least are working in the print-window/-console. But not the -inside setting :-(

UPDATE: found a solution/workaround and posted it here: https://github.com/wkhtmltopdf/wkhtmltopdf/issues/2982

sebgmc avatar Oct 20 '17 12:10 sebgmc

Has anybody found a solution to this issue?

molerat619 avatar Feb 05 '18 09:02 molerat619

I solved it by adding the following css class to the element you prefer not to break (say the inner-most div you wish to keep in tact):

.no-page-break {
    page-break-inside: avoid;
}

I have seen it in this StackOverflow post . just make sure that the parent element is not flexbox.

rmiller-sgntr avatar Jul 31 '18 18:07 rmiller-sgntr

In my case, the problem was a parent element with display: -webkit-box;. It seems page-break-inside is incompatible with a flexbox layout, and it started working as intended after I changed the parent to display: block;.

cmmcneill avatar Sep 24 '19 20:09 cmmcneill

I had many tables in the page and I solved it by using

table {
        page-break-inside: avoid;
        display: block
    }

See image below: image

vdkkia avatar Feb 27 '20 13:02 vdkkia

Has anybody found a solution to this issue?

This problem is still relevant.

GvadimI avatar Mar 11 '20 13:03 GvadimI

Has anybodoy another working solution?

augustosnk12 avatar May 03 '21 20:05 augustosnk12

@augustosnk12 I noticed that flex-direction: column was causing me this type of problem. I removed all the flex columns and now it respects the page-break-inside: avoid;

vasco3 avatar May 03 '21 22:05 vasco3

@vasco3 Thanks for the answer but I'm not using any flexbox in my code... :'(

augustosnk12 avatar May 04 '21 11:05 augustosnk12

@augustosnk12 are you using grid?

vasco3 avatar May 04 '21 14:05 vasco3

@vasco3 I'm using tables

augustosnk12 avatar May 05 '21 11:05 augustosnk12

Avoid to use thead. It work to me while text inside thead become double at the end of page and the top of page below.

AimanMRT avatar Jul 29 '21 14:07 AimanMRT

I've tested the html in chrome print preview, the css is correct but node-html-pdf seems to break it

conor909 avatar Jul 17 '22 11:07 conor909

I had height property on an ancestor of the elements I wanted to avoid page breaks on. As soon as I removed them, the elements were no longer being split accross pages. I'll see if I can come up with solid repro steps.

saalihou avatar Aug 16 '22 13:08 saalihou