From 92b948654584611d22af72e72d4c1604d4f3f0a2 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 15 Apr 2016 21:40:27 +0530 Subject: [PATCH] PDF Output: Make fix for blank page at end of section also work when headers/footers are specified --- src/calibre/ebooks/pdf/render/engine.py | 22 ++++++++++++++++++---- src/calibre/ebooks/pdf/render/from_html.py | 2 +- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/calibre/ebooks/pdf/render/engine.py b/src/calibre/ebooks/pdf/render/engine.py index cb4a910808..c0700cbcfc 100644 --- a/src/calibre/ebooks/pdf/render/engine.py +++ b/src/calibre/ebooks/pdf/render/engine.py @@ -59,7 +59,7 @@ class PdfEngine(QPaintEngine): def __init__(self, file_object, page_width, page_height, left_margin, top_margin, right_margin, bottom_margin, width, height, errors=print, debug=print, compress=True, - mark_links=False): + mark_links=False, opts=None): QPaintEngine.__init__(self, self.FEATURES) self.file_object = file_object self.compress, self.mark_links = compress, mark_links @@ -89,6 +89,10 @@ class PdfEngine(QPaintEngine): self.current_page_inited = False self.content_written_to_current_page = False self.qt_hack, err = plugins['qt_hack'] + self.has_footers = opts is not None and (opts.pdf_page_numbers or opts.pdf_footer_template is not None) + self.has_headers = opts is not None and opts.pdf_header_template is not None + self.header_height = (opts.margin_top or 0) if opts else 0 + self.footer_height = (opts.margin_bottom) or 0 if opts else 0 if err: raise RuntimeError('Failed to load qt_hack with err: %s'%err) @@ -251,7 +255,6 @@ class PdfEngine(QPaintEngine): @store_error def drawTextItem(self, point, text_item): - self.content_written_to_current_page = 'drawTextItem' # return super(PdfEngine, self).drawTextItem(point, text_item) self.apply_graphics_state() gi = GlyphInfo(*self.qt_hack.get_glyphs(point, text_item)) @@ -281,6 +284,17 @@ class PdfEngine(QPaintEngine): glyphs.append((x-last_x, last_y - y, glyph_index)) last_x, last_y = x, y + if not self.content_written_to_current_page: + ypositions = [y for x, y in gi.positions] + miny = min(ypositions or (0,)) + maxy = max(ypositions or (self.pixel_height,)) + page_top = self.header_height if self.has_headers else 0 + page_bottom = self.pixel_height - (self.footer_height if self.has_footers else 0) + if page_top <= miny <= page_bottom or page_top <= maxy <= page_bottom: + self.content_written_to_current_page = 'drawTextItem' + else: + self.debug('Text in header/footer: miny=%s maxy=%s page_top=%s page_bottom=%s'% ( + miny, maxy, page_top, page_bottom)) self.pdf.draw_glyph_run([gi.stretch, 0, 0, -1, 0, 0], gi.size, metrics, glyphs) @@ -326,7 +340,7 @@ class PdfDevice(QPaintDevice): # {{{ def __init__(self, file_object, page_size=A4, left_margin=inch, top_margin=inch, right_margin=inch, bottom_margin=inch, xdpi=1200, ydpi=1200, errors=print, debug=print, - compress=True, mark_links=False): + compress=True, mark_links=False, opts=None): QPaintDevice.__init__(self) self.xdpi, self.ydpi = xdpi, ydpi self.page_width, self.page_height = page_size @@ -338,7 +352,7 @@ class PdfDevice(QPaintDevice): # {{{ left_margin, top_margin, right_margin, bottom_margin, self.width(), self.height(), errors=errors, debug=debug, compress=compress, - mark_links=mark_links) + mark_links=mark_links, opts=opts) self.add_outline = self.engine.add_outline self.add_links = self.engine.add_links diff --git a/src/calibre/ebooks/pdf/render/from_html.py b/src/calibre/ebooks/pdf/render/from_html.py index c87061b286..5cc54aff1d 100644 --- a/src/calibre/ebooks/pdf/render/from_html.py +++ b/src/calibre/ebooks/pdf/render/from_html.py @@ -182,7 +182,7 @@ class PDFWriter(QObject): top_margin=0, right_margin=mr, bottom_margin=0, xdpi=xdpi, ydpi=ydpi, errors=self.log.error, debug=self.log.debug, compress=not - opts.uncompressed_pdf, + opts.uncompressed_pdf, opts=opts, mark_links=opts.pdf_mark_links) self.footer = opts.pdf_footer_template if self.footer: