Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

page-break-inside: avoid is erratic #223

Open
sjohnson32 opened this issue Jan 20, 2017 · 21 comments
Open

page-break-inside: avoid is erratic #223

sjohnson32 opened this issue Jan 20, 2017 · 21 comments

Comments

@sjohnson32
Copy link

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.

@zedissime
Copy link

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 ?

@sjohnson32
Copy link
Author

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

@zedissime
Copy link

zedissime commented Mar 2, 2017

@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.

@marcbachmann
Copy link
Owner

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: ariya/phantomjs#13524 (comment)

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.

@zedissime
Copy link

@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.

@ducdev
Copy link

ducdev commented Mar 2, 2017

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?

@gur111
Copy link

gur111 commented Oct 17, 2017

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

@sebgmc
Copy link

sebgmc commented Oct 20, 2017

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: wkhtmltopdf/wkhtmltopdf#2982

@molerat619
Copy link

Has anybody found a solution to this issue?

@rmiller-sgntr
Copy link

rmiller-sgntr commented Jul 31, 2018

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.

@cmmcneill
Copy link

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

@vdkkia
Copy link

vdkkia commented Feb 27, 2020

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

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

See image below:
image

@GvadimI
Copy link

GvadimI commented Mar 11, 2020

Has anybody found a solution to this issue?

This problem is still relevant.

@augustosnk12
Copy link

Has anybodoy another working solution?

@vasco3
Copy link

vasco3 commented May 3, 2021

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

@augustosnk12
Copy link

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

@vasco3
Copy link

vasco3 commented May 4, 2021

@augustosnk12 are you using grid?

@augustosnk12
Copy link

@vasco3 I'm using tables

@AimanMRT
Copy link

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.

@conor909
Copy link

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

@saalihou
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests