Skip to content

Commit

Permalink
Fix erroneous page breaks that occured for full-width / full-height i…
Browse files Browse the repository at this point in the history
…mages - cf. #166 (#168)
  • Loading branch information
Lucas-C authored Jun 12, 2021
1 parent 68da1a6 commit 2f2351b
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/),
and [PEP 440](https://www.python.org/dev/peps/pep-0440/).

## [2.4.1] - not released yet
### Fixed
- erroneous page breaks occured for full-width / full-height images

## [2.4.0] - 2021-06-11
### Changed
- now `fpdf2` uses the newly supported `DCTDecode` image filter for JPEG images,
Expand Down
30 changes: 17 additions & 13 deletions fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1749,28 +1749,32 @@ def _markdown_parse(self, txt):

def _perform_page_break_if_need_be(self, h):
if (
self.y + h >= self.page_break_trigger
self.y + h > self.page_break_trigger
and not self.in_footer
and self.accept_page_break
):
LOGGER.debug(
"Page break on page %d at y=%d for element of height %d",
"Page break on page %d at y=%d for element of height %d > %d",
self.page,
self.y,
h,
self.page_break_trigger,
)
x, ws = self.x, self.ws
if ws > 0:
self.ws = 0
self._out("0 Tw")
self.add_page(same=True)
self.x = x # restore x but not y after drawing header
if ws > 0:
self.ws = ws
self._out(f"{ws * self.k:.3f} Tw")
self._perform_page_break()
return True
return False

def _perform_page_break(self):
x, ws = self.x, self.ws
if ws > 0:
self.ws = 0
self._out("0 Tw")
self.add_page(same=True)
self.x = x # restore x but not y after drawing header
if ws > 0:
self.ws = ws
self._out(f"{ws * self.k:.3f} Tw")

@check_page
def multi_cell(
self,
Expand Down Expand Up @@ -3253,12 +3257,12 @@ def unbreakable(self):
LOGGER.debug("Starting unbreakable block")
yield recorder
y_scroll = recorder.y - prev_y + (recorder.page - prev_page) * self.eph
if prev_y + y_scroll >= self.page_break_trigger:
if prev_y + y_scroll > self.page_break_trigger or recorder.page > prev_page:
LOGGER.debug("Performing page jump due to unbreakable height")
recorder.rewind()
# pylint: disable=protected-access
# Performing this call through .pdf so that it does not get recorded & replayed:
assert recorder.pdf._perform_page_break_if_need_be(y_scroll)
recorder.pdf._perform_page_break()
recorder.replay()
LOGGER.debug("Ending unbreakable block")

Expand Down
Binary file added test/image/full_height_image.pdf
Binary file not shown.
Binary file added test/image/full_width_image.pdf
Binary file not shown.
26 changes: 26 additions & 0 deletions test/image/test_full_page_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from pathlib import Path

import fpdf
from test.conftest import assert_pdf_equal


HERE = Path(__file__).resolve().parent
IMAGE_PATH = HERE / "png_images/ba2b2b6e72ca0e4683bb640e2d5572f8.png"


def test_full_width_image(tmp_path):
img = fpdf.image_parsing.get_img_info(IMAGE_PATH)
pdf = fpdf.FPDF(format=(img["w"], img["h"]))
pdf.set_margin(0)
pdf.add_page()
pdf.image(IMAGE_PATH, w=img["w"])
assert_pdf_equal(pdf, HERE / "full_width_image.pdf", tmp_path)


def test_full_height_image(tmp_path):
img = fpdf.image_parsing.get_img_info(IMAGE_PATH)
pdf = fpdf.FPDF(format=(img["w"], img["h"]))
pdf.set_margin(0)
pdf.add_page()
pdf.image(IMAGE_PATH, h=img["h"])
assert_pdf_equal(pdf, HERE / "full_height_image.pdf", tmp_path)
1 change: 0 additions & 1 deletion tutorial/gif2pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

pdf = FPDF(format=size)
pdf.set_margin(0)
pdf.set_auto_page_break(False)
duration_in_secs = 0
for img in imgs:
pdf.add_page(duration=duration_in_secs)
Expand Down

0 comments on commit 2f2351b

Please sign in to comment.