marcbachmann / node-html-pdf

This repo isn't maintained anymore as phantomjs got dreprecated a long time ago. Please migrate to headless chrome/puppeteer.
MIT License
3.56k stars 544 forks source link

Syntax for forcing a page break #700

Open marioperna opened 1 year ago

marioperna commented 1 year ago

Hello,

I am generating a pdf starting from an HTML template containing a table.

Currently, my issue is about how to interrupt the table row before the end of the page or In general how to interrupt the page and go to the next.

I've already tried to use some CSS selectors such as page-break-before, page-break-after Etc... but none of them seem to work well. Is there any special syntax for this package to force the breaking?

I know this package is deprecated, but I hope you can help me out. Thanks a lot

drickchote commented 1 year ago

I got a great result by placing a margin-bottom of 2000px, as the margin does not pass to the next page, it worked for me.

drickchote commented 1 year ago

I noticed that the margin was going to the pages towards the end of the file. To resolve this, I calculated the number of pixels each page occupies. Based on the number of elements on my table.

This is my code


let html = generatehtml()

 const MAX_ITEMS_ON_PAGE = 18
 const inicialMarginHeight = items.length <= MAX_ITEMS_ON_PAGE ? 430 : 800

 let tableHeaderHeight = items.length > 0 ? 60 : 0
 let itemsHeight = (items.length % MAX_ITEMS_ON_PAGE + 1) * 20 
 let tableMargin = inicialMarginHeight - tableHeaderHeight - itemsHeight

html += `<div style='margin-bottom:${tableMargin}px; background-color: red'/>`

The red background helped me to know the right fit