Skip to content

Commit

Permalink
Fix #632: <img> without "height" attribute fails to leave enough blan…
Browse files Browse the repository at this point in the history
…k space for itself (#634)

Fixes #632
  • Loading branch information
Bubbu0129 committed Dec 27, 2022
1 parent a961d74 commit 64d8482
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This can also be enabled programmatically with `warnings.simplefilter('default',

## [2.6.1] - not released yet
### Added
* ensured support for Python 3.11
* the `x` parameter of [`FPDF.image()`](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.image) now accepts a value of `"C"` / `Align.C` / `"R"` / `Align.R` to horizontally position the image centered or aligned right
* support for `[]()` links when `markdown=True`
* support for `line-height` attribute of paragraph (`<p>`) in `write_html()` - thanks to @Bubbu0129
Expand All @@ -28,6 +29,7 @@ This can also be enabled programmatically with `warnings.simplefilter('default',
### Fixed
* a `ValueError: Incoherent hierarchy` could be raised when using `write_html()` with some headings hierarchy
* performance issue with adding large images with `FlateDecode` image filter
* image (`<img>`) without `height` attribute overlaps with the following content [#632](https://github.com/PyFPDF/fpdf2/issues/632)

## [2.6.0] - 2022-11-20
### Added
Expand Down
4 changes: 3 additions & 1 deletion fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3585,7 +3585,7 @@ def image(
if link:
self.link(x, y, w, h, link)

return info
return {**info, "rendered_width": w, "rendered_height": h}

def _vector_image(
self,
Expand Down Expand Up @@ -3647,6 +3647,8 @@ def _vector_image(
if link:
self.link(x, y, w, h, link)

return {"rendered_width": w, "rendered_height": h}

def _downscale_image(self, name, img, info, w, h):
width_in_pt, height_in_pt = w * self.k, h * self.k
lowres_name = f"lowres-{name}"
Expand Down
4 changes: 3 additions & 1 deletion fpdf/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,9 +636,11 @@ def handle_starttag(self, tag, attrs):
width,
height,
)
self.pdf.image(
image_info = self.pdf.image(
self.image_map(attrs["src"]), x, y, width, height, link=self.href
)
width = image_info["rendered_width"]
height = image_info["rendered_height"]
self.pdf.set_x(x + width)
if self.table_col_index is not None:
# <img> in a <td>: we grow the cell height according to the image height:
Expand Down
15 changes: 15 additions & 0 deletions test/html/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,21 @@ def test_html_custom_line_height(tmp_path):
assert_pdf_equal(pdf, HERE / "html_custom_line_height.pdf", tmp_path)


def test_img_not_overlapping(tmp_path):
pdf = FPDF()
pdf.add_page()
pdf.write_html(
"""<img src="test/image/png_images/affc57dfffa5ec448a0795738d456018.png"/>
<p>text</p>
"""
)
assert_pdf_equal(
pdf,
HERE / "test_img_not_overlapping.pdf",
tmp_path,
)


def test_warn_on_tags_not_matching(caplog):
pdf = FPDF()
pdf.add_page()
Expand Down
Binary file not shown.
Binary file added test/html/test_img_not_overlapping.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion test/image/test_load_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_load_invalid_base64_data():


# ensure memory usage does not get too high - this value depends on Python version:
@memunit.assert_lt_mb(140)
@memunit.assert_lt_mb(141)
def test_share_images_cache(tmp_path):
images_cache = {}

Expand Down
2 changes: 1 addition & 1 deletion test/test_perfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@pytest.mark.timeout(40)
# ensure memory usage does not get too high - this value depends on Python version:
@memunit.assert_lt_mb(178)
@memunit.assert_lt_mb(179)
def test_intense_image_rendering():
png_file_paths = []
for png_file_path in (HERE / "image/png_images/").glob("*.png"):
Expand Down

0 comments on commit 64d8482

Please sign in to comment.