diff --git a/CHANGELOG.md b/CHANGELOG.md index e58921fa4..89d268173 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,9 +17,10 @@ in order to get warned about deprecated features used in your code. This can also be enabled programmatically with `warnings.simplefilter('default', DeprecationWarning)`. ## [2.7.4] - Not released yet - ### Added - 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 +### Changed +- [`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 `` are used, in order to be more backward-compatible with earlier versions of `fpdf2` - _cf._ [#740](https://github.com/PyFPDF/fpdf2/issues/740) ## [2.7.3] - 2023-04-03 ### Fixed @@ -29,7 +30,6 @@ This can also be enabled programmatically with `warnings.simplefilter('default', ### Fixed - custom fonts can be used with `FPDF.table()` without triggering a `TypeError: cannot pickle 'dict_keys' object` - thanks @aeris07 for the bug report - the SVG parser now accepts `` with `width` / `height` defined as percents - ### Added - documentation on how to generate Code128 barcodes using the `python-barcode` lib: [documentation section](https://pyfpdf.github.io/fpdf2/Barcodes.html#Code128) diff --git a/fpdf/html.py b/fpdf/html.py index 6ad4a8954..be6545b25 100644 --- a/fpdf/html.py +++ b/fpdf/html.py @@ -10,8 +10,9 @@ from html.parser import HTMLParser from .enums import TextEmphasis, XPos, YPos +from .errors import FPDFException from .fonts import FontFace -from .table import Table +from .table import Table, TableBordersLayout import re @@ -447,13 +448,23 @@ def handle_starttag(self, tag, attrs): ) self.pdf.ln() if tag == "tr": + if not self.table: + raise FPDFException("Invalid HTML: used outside any ") self.tr = {k.lower(): v for k, v in attrs.items()} self.table_row = self.table.row() if tag in ("td", "th"): + if not self.table_row: + raise FPDFException(f"Invalid HTML: <{tag}> used outside any ") self.td_th = {k.lower(): v for k, v in attrs.items()} if tag == "th": self.td_th["align"] = "CENTER" self.td_th["b"] = True + elif len(self.table.rows) == 1 and not self.table_row.cells: + # => we are in the 1st , and the 1st cell is a
+ # => we do not treat the first row as a header + # pylint: disable=protected-access + self.table._borders_layout = TableBordersLayout.NONE + self.table._first_row_as_headings = False if "height" in attrs: LOGGER.warning( 'Ignoring unsupported height="%s" specified on a <%s>', diff --git a/fpdf/table.py b/fpdf/table.py index 44a812745..ec132df88 100644 --- a/fpdf/table.py +++ b/fpdf/table.py @@ -186,7 +186,11 @@ def get_cell_border(self, i, j): def _render_table_row(self, i, fill=False, **kwargs): row = self.rows[i] lines_heights_per_cell = self._get_lines_heights_per_cell(i) - row_height = max(sum(lines_heights) for lines_heights in lines_heights_per_cell) + row_height = ( + max(sum(lines_heights) for lines_heights in lines_heights_per_cell) + if lines_heights_per_cell + else 0 + ) j = 0 while j < len(row.cells): cell_line_height = row_height / len(lines_heights_per_cell[j]) diff --git a/test/html/html_table_with_img.pdf b/test/html/html_table_with_img.pdf new file mode 100644 index 000000000..75ab7a983 Binary files /dev/null and b/test/html/html_table_with_img.pdf differ diff --git a/test/html/html_table_with_img_without_explicit_dimensions.pdf b/test/html/html_table_with_img_without_explicit_dimensions.pdf new file mode 100644 index 000000000..75ab7a983 Binary files /dev/null and b/test/html/html_table_with_img_without_explicit_dimensions.pdf differ diff --git a/test/html/html_table_with_imgs_captions_and_colspan.pdf b/test/html/html_table_with_imgs_captions_and_colspan.pdf new file mode 100644 index 000000000..dc352bb9e Binary files /dev/null and b/test/html/html_table_with_imgs_captions_and_colspan.pdf differ diff --git a/test/html/html_table_with_only_tds.pdf b/test/html/html_table_with_only_tds.pdf new file mode 100644 index 000000000..5778c2f77 Binary files /dev/null and b/test/html/html_table_with_only_tds.pdf differ