Мне не удалось решить проблему с помощью таблицы, поэтому я решил свою проблему с помощью divs. Я отслеживал случайную высоту div-ов на каждой странице и назначал оставшееся место для нижнего колонтитула. У моих нижних колонтитулов есть ограничение минимальной высоты, чтобы на каждой странице был нижний колонтитул. Содержимое каждой страницы помещается в другой раздел, чтобы дать мне больше контроля над стилем каждой страницы; например: разрыв страницы после и переполнение . Решение не идеальное и надеюсь, что кто-то придет с лучшим подходом, но оно удовлетворяет мою потребность на данный момент. Ниже вы можете найти обновленный код:
const main = document.getElementById("main");
const newDiv = document.createElement("div");
const paperHeight = 11 // paper height in inch
const paperMargin = 1; // sum of top and bottom paper margins in inch
const overflow = 0.11; // used to protect page overflow to the next page
const dpi = 96; // Display dots per inch (DPI)
const maxHeight = (paperHeight - paperMargin) * dpi; // max page height
const footerMinHeight = 40; // Min height of footer
const headerHeight = 100; // Height of header in px
const numOfItems = 20; // Number of items
let j = 0;
let items = '';
let pageCount = 1;
do {
items += `<div class="page" style="max-height: ${paperHeight - paperMargin - overflow}in"><div class="box header" style="height: ${headerHeight}px">Page ${pageCount} - Header</div>`;
let pageHeight = headerHeight;
do {
const elementHeight = getRandomIntInclusive(60, 250);
if (elementHeight + pageHeight > maxHeight - footerMinHeight || j === numOfItems) {
items += `<div class="box footer" style="height: ${maxHeight - pageHeight}px">Footer</div></div>`;
break;
} else {
pageHeight += elementHeight;
j++;
}
items += `<div class="box" style="height: ${elementHeight}px">Item ${j} </div>`;
} while (j <= numOfItems)
pageCount++;
} while (j < numOfItems)
newDiv.innerHTML = items;
main.appendChild(newDiv);
document.getElementById("print").addEventListener("click", () => {
window.print();
});
function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
* {
box-sizing: border-box;
}
.page {
border: 1px dashed green;
page-break-after: always;
overflow: hidden;
}
.box {
border-bottom: 1px solid red;
}
.header {
background: #e6ffe6
}
.footer {
background: #fdfed6;
}
@media print {
@page {
size: letter;
margin: 0.5in;
}
#print {
display: none;
}
}
<button id='print'>Print</button>
<div id='main'></div>