This particular issue cannot be solved using a code snippet. Instead, I will outline my approach to resolving this problem.
Following the occurrence of the print event (whether triggered by a button click or automated process)...
- I would determine the current height of each component (as you indicated there are between 4 to 12 components).
const element = document.getElementById('element');
element.offsetHeight // height
- To address the issue, I would create a placeholder component (in the form of a div) that can be used to fill any vertical gaps between the components being printed.
const Placeholder = ({height}) => (
<div style={{height: `${height}px` }}> </div>
)
- Next, I would begin iterating through all the components until their combined height reaches 900px. At this point, I would insert the placeholder component to ensure that any remaining components are moved to a new page.
For example:
Component 1 is 300px
Component 2 is 300px
Component 3 is 700px
Component 4 is 300px
loop {
// first page
<Component1 />
<Component2 />
<Placeholder height={300} />
// second page
<Component3 />
<Placeholder height={200} />
<Component4 />
<Placeholder height={600} />
}