Skip to content

Commit 36a8fe4

Browse files
authored
Merge 6e183b8 into 393580e
2 parents 393580e + 6e183b8 commit 36a8fe4

7 files changed

+19
-4
lines changed

CHANGELOG.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ in order to get warned about deprecated features used in your code.
1717
This can also be enabled programmatically with `warnings.simplefilter('default', DeprecationWarning)`.
1818

1919
## [2.7.4] - Not released yet
20-
2120
### Added
2221
- documentation on how to embed `graphs` and `charts` generated using `Pygal` lib: [documentation section](https://pyfpdf.github.io/fpdf2/Maths.html#using-pygal) - thanks to @ssavi-ict
22+
### Changed
23+
- [`FPDF.write_html()`](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.write_html) does not render the top row as a header, in bold with a line below, when no `<th>` are used, in order to be more backward-compatible with earlier versions of `fpdf2` - _cf._ [#740](https://github.com/PyFPDF/fpdf2/issues/740)
2324

2425
## [2.7.3] - 2023-04-03
2526
### Fixed
@@ -29,7 +30,6 @@ This can also be enabled programmatically with `warnings.simplefilter('default',
2930
### Fixed
3031
- custom fonts can be used with `FPDF.table()` without triggering a `TypeError: cannot pickle 'dict_keys' object` - thanks @aeris07 for the bug report
3132
- the SVG parser now accepts `<rect>` with `width` / `height` defined as percents
32-
3333
### Added
3434
- documentation on how to generate Code128 barcodes using the `python-barcode` lib: [documentation section](https://pyfpdf.github.io/fpdf2/Barcodes.html#Code128)
3535

fpdf/html.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
from html.parser import HTMLParser
1111

1212
from .enums import TextEmphasis, XPos, YPos
13+
from .errors import FPDFException
1314
from .fonts import FontFace
14-
from .table import Table
15+
from .table import Table, TableBordersLayout
1516

1617
import re
1718

@@ -447,13 +448,23 @@ def handle_starttag(self, tag, attrs):
447448
)
448449
self.pdf.ln()
449450
if tag == "tr":
451+
if not self.table:
452+
raise FPDFException("Invalid HTML: <tr> used outside any <table>")
450453
self.tr = {k.lower(): v for k, v in attrs.items()}
451454
self.table_row = self.table.row()
452455
if tag in ("td", "th"):
456+
if not self.table_row:
457+
raise FPDFException(f"Invalid HTML: <{tag}> used outside any <tr>")
453458
self.td_th = {k.lower(): v for k, v in attrs.items()}
454459
if tag == "th":
455460
self.td_th["align"] = "CENTER"
456461
self.td_th["b"] = True
462+
elif len(self.table.rows) == 1 and not self.table_row.cells:
463+
# => we are in the 1st <tr>, and the 1st cell is a <td>
464+
# => we do not treat the first row as a header
465+
# pylint: disable=protected-access
466+
self.table._borders_layout = TableBordersLayout.NONE
467+
self.table._first_row_as_headings = False
457468
if "height" in attrs:
458469
LOGGER.warning(
459470
'Ignoring unsupported height="%s" specified on a <%s>',

fpdf/table.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,11 @@ def get_cell_border(self, i, j):
186186
def _render_table_row(self, i, fill=False, **kwargs):
187187
row = self.rows[i]
188188
lines_heights_per_cell = self._get_lines_heights_per_cell(i)
189-
row_height = max(sum(lines_heights) for lines_heights in lines_heights_per_cell)
189+
row_height = (
190+
max(sum(lines_heights) for lines_heights in lines_heights_per_cell)
191+
if lines_heights_per_cell
192+
else 0
193+
)
190194
j = 0
191195
while j < len(row.cells):
192196
cell_line_height = row_height / len(lines_heights_per_cell[j])

test/html/html_table_with_img.pdf

14.4 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
1.01 KB
Binary file not shown.

0 commit comments

Comments
 (0)