Skip to content

Commit bdd4638

Browse files
authored
Fixing image insertion in tables (#773)
1 parent 861db30 commit bdd4638

8 files changed

+33
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This can also be enabled programmatically with `warnings.simplefilter('default',
2222
- documentation on how to use `fpdf2` with [FastAPI](https://fastapi.tiangolo.com/): <https://pyfpdf.github.io/fpdf2/UsageInWebAPI.html#FastAPI> - thanks to @KamarulAdha
2323
- [`FPDF.write_html()`](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.write_html): `<table>` elements can now be aligned left or right on the page using `align=`
2424
### Fixed
25+
- [`FPDF.table()`](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.table): images no more overflow cells
2526
- [`FPDF.table()`](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.table): text overflow in the last cell of the header row is now properly handled
2627
- [`FPDF.table()`](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.table): when `align="RIGHT"` is provided, the page right margin is now properly taken in consideration
2728
### Changed

fpdf/fpdf.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3346,15 +3346,19 @@ def _has_next_page(self):
33463346
@contextmanager
33473347
def _disable_writing(self):
33483348
self._out = lambda *args, **kwargs: None
3349-
self.add_page = lambda *args, **kwargs: None
3350-
self._perform_page_break = lambda *args, **kwargs: None
3351-
prev_x, prev_y = self.x, self.y
3352-
yield
3353-
# restore writing functions:
3354-
del self.add_page
3355-
del self._out
3356-
del self._perform_page_break
3357-
self.set_xy(prev_x, prev_y) # restore location
3349+
prev_page, prev_x, prev_y = self.page, self.x, self.y
3350+
self._push_local_stack()
3351+
try:
3352+
yield
3353+
finally:
3354+
self._pop_local_stack()
3355+
# restore location:
3356+
for p in range(prev_page + 1, self.page + 1):
3357+
del self.pages[p]
3358+
self.page = prev_page
3359+
self.set_xy(prev_x, prev_y)
3360+
# restore writing function:
3361+
del self._out
33583362

33593363
@check_page
33603364
def multi_cell(

fpdf/table.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,15 @@ def _render_table_cell(
219219
row = self.rows[i]
220220
cell = row.cells[j]
221221
col_width = self._get_col_width(i, j, cell.colspan)
222+
img_height = 0
222223
if cell.img:
223224
x, y = self._fpdf.x, self._fpdf.y
224-
self._fpdf.image(
225+
img_height = self._fpdf.image(
225226
cell.img,
226227
w=col_width,
227228
h=0 if cell.img_fill_width else row_height,
228229
keep_aspect_ratio=True,
229-
)
230+
).rendered_height
230231
self._fpdf.set_xy(x, y)
231232
text_align = cell.align or self._text_align
232233
if not isinstance(text_align, (Align, str)):
@@ -255,7 +256,7 @@ def _render_table_cell(
255256
else FontFace(fill_color=self._cell_fill_color)
256257
)
257258
with self._fpdf.use_font_face(style):
258-
page_break, height = self._fpdf.multi_cell(
259+
page_break, cell_height = self._fpdf.multi_cell(
259260
w=col_width,
260261
h=row_height,
261262
txt=cell.text,
@@ -269,7 +270,7 @@ def _render_table_cell(
269270
output=MethodReturnValue.PAGE_BREAK | MethodReturnValue.HEIGHT,
270271
**kwargs,
271272
)
272-
return page_break, height
273+
return page_break, max(img_height, cell_height)
273274

274275
def _get_col_width(self, i, j, colspan=1):
275276
if not self._col_widths:
1 Byte
Binary file not shown.
7 Bytes
Binary file not shown.
73 Bytes
Binary file not shown.

test/table/table_with_qrcode.pdf

2.04 KB
Binary file not shown.

test/table/test_table_with_image.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pathlib import Path
22

3-
import pytest
3+
import qrcode, pytest
44

55
from fpdf import FPDF
66
from test.conftest import assert_pdf_equal, LOREM_IPSUM
@@ -66,11 +66,7 @@ def test_table_with_images_and_img_fill_width(tmp_path):
6666
row.cell(img=datum, img_fill_width=True)
6767
else:
6868
row.cell(datum)
69-
assert_pdf_equal(
70-
pdf,
71-
HERE / "table_with_images_and_img_fill_width.pdf",
72-
tmp_path,
73-
)
69+
assert_pdf_equal(pdf, HERE / "table_with_images_and_img_fill_width.pdf", tmp_path)
7470

7571

7672
def test_table_with_multiline_cells_and_images(tmp_path):
@@ -101,3 +97,15 @@ def test_table_with_images_and_text():
10197
row.cell(datum.name, img=datum)
10298
else:
10399
row.cell(datum)
100+
101+
102+
def test_table_with_qrcode(tmp_path): # issue 771
103+
pdf = FPDF()
104+
pdf.add_page()
105+
pdf.set_font("Times", size=16)
106+
qrCodeGenerated = qrcode.make("https://google.com")
107+
with pdf.table(first_row_as_headings=False) as table:
108+
row = table.row()
109+
row.cell(img=qrCodeGenerated.get_image(), img_fill_width=True)
110+
row.cell("Other field")
111+
assert_pdf_equal(pdf, HERE / "table_with_qrcode.pdf", tmp_path)

0 commit comments

Comments
 (0)