diff --git a/resources/pdf-preprint.js b/resources/pdf-preprint.js new file mode 100644 index 0000000000..5037db298e --- /dev/null +++ b/resources/pdf-preprint.js @@ -0,0 +1,31 @@ +/* vim:fileencoding=utf-8 + * + * Copyright (C) 2019 Kovid Goyal + * + * Distributed under terms of the GPLv3 license + */ +/*jshint esversion: 6 */ + +(function() { +"use strict"; + // wrap up long words that dont fit in the page + document.body.style.overflowWrap = 'break-word'; + + var 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 + }; + 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'; + } + } + +})(); + + diff --git a/src/calibre/ebooks/pdf/html_writer.py b/src/calibre/ebooks/pdf/html_writer.py index 8fca067aab..b386b48f7c 100644 --- a/src/calibre/ebooks/pdf/html_writer.py +++ b/src/calibre/ebooks/pdf/html_writer.py @@ -60,6 +60,13 @@ def data_as_pdf_doc(data): return ans +def preprint_js(): + ans = getattr(preprint_js, 'ans', None) + if ans is None: + ans = preprint_js.ans = P('pdf-preprint.js', data=True).decode('utf-8') + return ans + + def last_tag(root): return tuple(root.iterchildren('*'))[-1] @@ -196,6 +203,9 @@ class Renderer(QWebEnginePage): pass def print_to_pdf(self): + self.runJavaScript(preprint_js(), self.start_print) + + def start_print(self, *a): self.printToPdf(self.printing_done, self.page_layout) def printing_done(self, pdf_data):