From dcbed07c952f380566996f38f3955a257c5a62db Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 18 Dec 2019 22:28:35 +0530 Subject: [PATCH] PDF Output: When an inline image is placed alone inside a block tag, ensure that it is not split over two pages. --- resources/pdf-preprint.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/resources/pdf-preprint.js b/resources/pdf-preprint.js index 5037db298e..a78aec270c 100644 --- a/resources/pdf-preprint.js +++ b/resources/pdf-preprint.js @@ -11,19 +11,25 @@ // wrap up long words that dont fit in the page document.body.style.overflowWrap = 'break-word'; - var block_styles = { + var break_avoid_block_styles = { "run-in":1, "block":1, "table-row-group":1, "table-column":1, "table-column-group":1, "table-header-group":1, "table-footer-group":1, "table-row":1, "table-cell":1, - "table-caption":1, "inline-block:":1 + "table-caption":1, // page-break-avoid does not work for inline-block either }; + + function avoid_page_breaks_inside(node) { + node.style.pageBreakInside = 'avoid'; + node.style.breakInside = 'avoid'; + } + for (const img of document.images) { var style = window.getComputedStyle(img); if (style.maxHeight === 'none') img.style.maxHeight = '100vh'; if (style.maxWidth === 'none') img.style.maxWidth = '100vw'; - if (block_styles[style.display]) { - img.style.pageBreakInside = 'avoid'; - img.style.breakInside = 'avoid'; - } + + var is_block = break_avoid_block_styles[style.display]; + if (is_block) avoid_page_breaks_inside(img); + else if (img.parentNode && img.parentNode.childElementCount === 1) avoid_page_breaks_inside(img.parentNode); } })();