// Printable content height inside a page (A4 minus vertical padding) const PAGE_CONTENT = 1027; // 1123 − 56 − 40 const TITLE_SPACE = 64; // page-1 title block allowance let pageNum = 0, page = null, used = 0; let isFirstPage = true; // Pre-scan for the first PDF page number to start numbering correctly if (respectBreaks) { for (const b of blocks) { if (b.classList.contains('reh-block-pagebreak')) { const i = parseInt(b.dataset.index); const l = rehState.lines[i]; if (l && l.page) { // If the pagebreak is for page N, the content before it is N or N-1 depending on how breaks are placed. // Usually a page break marker MEANS "Start of Page N". // If the first element is NOT a pagebreak, it means the content belongs to the page BEFORE the first pagebreak. // We'll just set pageNum to l.page - 1 (or 1 if it's 1). pageNum = Math.max(0, l.page - (b === blocks[0] ? 0 : 1)); } break; } } } const newPage = (forcedNum) => { if (forcedNum !== undefined) pageNum = forcedNum; else pageNum++; page = document.createElement('div'); page.className = 'reh-paper'; const num = document.createElement('div'); num.className = 'reh-paper-num'; num.textContent = pageNum + '.'; page.appendChild(num); used = 0; if (isFirstPage && titleEl) { const t = titleEl.cloneNode(true); t.style.display = ''; page.appendChild(t); used += TITLE_SPACE; } isFirstPage = false; wrap.appendChild(page); }; newPage(); for (const block of blocks) { // PDF page-break marker: always start a new page here if (respectBreaks && block.classList.contains('reh-block-pagebreak')) { const i = parseInt(block.dataset.index); const l = rehState.lines[i]; newPage(l && l.page ? l.page : undefined); continue; // don't move the marker div into the page } // Measure block height (must be in DOM to measure → append then check) page.appendChild(block); const cs = getComputedStyle(block); const h = block.offsetHeight + (parseFloat(cs.marginTop) || 0) + (parseFloat(cs.marginBottom) || 0); // If adding this block overflows the page (and page already has content), move to a new page if (!respectBreaks && used + h > PAGE_CONTENT && used > (isFirstPage ? TITLE_SPACE : 0)) { newPage(); page.appendChild(block); } used += h; }