Skip to content

Commit 843a091

Browse files
committed
Using new Table rendering in write_html()
1 parent aaac62f commit 843a091

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+429
-596
lines changed

.github/workflows/continuous-integration-workflow.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ jobs:
7474
sed -i "s/author:.*/author: v$(python setup.py -V 2>/dev/null)/" mkdocs.yml
7575
cp tutorial/notebook.ipynb docs/
7676
mkdocs build
77-
pdoc --html -o public/ fpdf --config "git_link_template='https://github
78-
.com/PyFPDF/fpdf2/blob/{commit}/{path}#L{start_line}-L{end_line}'"
77+
pdoc --html -o public/ fpdf --config "git_link_template='https://github.com/PyFPDF/fpdf2/blob/{commit}/{path}#L{start_line}-L{end_line}'"
7978
cd contributors/ && PYTHONUNBUFFERED=1 ./build_contributors_html_page.py PyFPDF/fpdf2
8079
cp -t ../public/ contributors.html contributors-map-small.png
8180
- name: Deploy documentation 🚀

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ This can also be enabled programmatically with `warnings.simplefilter('default',
2727
- unicode (non limited to ASCII) text can now be provided as metadata [#685](https://github.com/PyFPDF/fpdf2/issues/685)
2828
- all `TitleStyle` constructor parameters are now effectively optional
2929
### Changed
30+
* [`FPDF.write_html()`](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.write_html) now uses the new [`FPDF.table()`](https://pyfpdf.github.io/fpdf2/Tables.html) method to render `<table>` tags. As a consequence, vertical space before `<table>` tags has sometimes been reduced.
3031
- vector images parsing is now more robust: `fpdf2` can now embed SVG files without `viewPort` or no `height` / `width`
3132
- bitonal images are now encoded using `CCITTFaxDecode`, reducing their size in the PDF document - thanks to @eroux
3233
- when possible, JPG and group4 encoded TIFFs are now embedded directly without recompression - thanks to @eroux
34+
### Removed
35+
* [`FPDF.write_html()`](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.write_html) now uses the new [`FPDF.table()`](https://pyfpdf.github.io/fpdf2/Tables.html) method to render `<table>` tags. As a consequence, it does not support the `height` attribute defined on `<td>` / `<th>` tags anymore, nor `height` / `width` attributes defined on `<img>` tags inside cells, nor `width` attributes defined on `<thoot>` / `<tfoot>` tags.
3336

3437
## [2.6.1] - 2023-01-13
3538
### Added

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pip install git+https://github.com/PyFPDF/fpdf2.git@master
6464
* Embedding images, including transparency and alpha channel
6565
* Arbitrary path drawing and basic [SVG](https://pyfpdf.github.io/fpdf2/SVG.html) import
6666
* Embedding [barcodes](https://pyfpdf.github.io/fpdf2/Barcodes.html), [charts & graphs](https://pyfpdf.github.io/fpdf2/Maths.html), [emojis, symbols & dingbats](https://pyfpdf.github.io/fpdf2/EmojisSymbolsDingbats.html)
67-
* [Cell / multi-cell / plaintext writing](https://pyfpdf.github.io/fpdf2/Text.html), with [automatic page breaks](https://pyfpdf.github.io/fpdf2/PageBreaks.html), line break and text justification
67+
* [Tables](https://pyfpdf.github.io/fpdf2/Tables.html) and also [cell / multi-cell / plaintext writing](https://pyfpdf.github.io/fpdf2/Text.html), with [automatic page breaks](https://pyfpdf.github.io/fpdf2/PageBreaks.html), line break and text justification
6868
* Choice of measurement unit, page format & margins. Optional page header and footer
6969
* Basic [conversion from HTML to PDF](https://pyfpdf.github.io/fpdf2/HTML.html)
7070
* A [templating system](https://pyfpdf.github.io/fpdf2/Templates.html) to render PDFs in batchs

docs/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Go try it **now** online in a Jupyter notebook: [![Open In Colab](https://colab.
3030
* Embedding images, including transparency and alpha channel, using [Pillow (Python Imaging Library)](https://pillow.readthedocs.io/en/stable/)
3131
* Arbitrary path drawing and basic [SVG](SVG.md) import
3232
* Embedding [barcodes](Barcodes.md), [charts & graphs](Maths.md), [emojis, symbols & dingbats](EmojisSymbolsDingbats.md)
33-
* [Cell / multi-cell / plaintext writing](Text.md), with [automatic page breaks](PageBreaks.md), line break and text justification
33+
* [Tables](Tables.md), and also [cell / multi-cell / plaintext writing](Text.md), with [automatic page breaks](PageBreaks.md), line break and text justification
3434
* Choice of measurement unit, page format & margins. Optional page header and footer
3535
* Basic [conversion from HTML to PDF](HTML.md)
3636
* A [templating system](Templates.md) to render PDFs in batchs

fpdf/enums.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,13 @@ def style(self):
212212

213213
@classmethod
214214
def coerce(cls, value):
215-
if value.upper() == "BOLD":
216-
return cls.B
217-
if value.upper() == "ITALICS":
218-
return cls.I
219-
if value.upper() == "UNDERLINE":
220-
return cls.U
215+
if isinstance(value, str):
216+
if value.upper() == "BOLD":
217+
return cls.B
218+
if value.upper() == "ITALICS":
219+
return cls.I
220+
if value.upper() == "UNDERLINE":
221+
return cls.U
221222
return super(cls, cls).coerce(value)
222223

223224

@@ -236,6 +237,9 @@ class TableBordersLayout(CoerciveEnum):
236237
MINIMAL = intern("MINIMAL")
237238
"Draw only the top horizontal border, below the headings, and internal vertical borders"
238239

240+
HORIZONTAL_LINES = intern("HORIZONTAL_LINES")
241+
"Draw only horizontal lines"
242+
239243
NO_HORIZONTAL_LINES = intern("NO_HORIZONTAL_LINES")
240244
"Draw all cells border except horizontal lines, after the headings"
241245

fpdf/fonts.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Definition of the character widths of all PDF standard fonts.
33
"""
4+
from dataclasses import dataclass, replace
45
from typing import Optional, Union
56

67
from .drawing import DeviceGray, DeviceRGB
@@ -2605,21 +2606,22 @@
26052606
}
26062607

26072608

2609+
@dataclass
26082610
class FontStyle:
2611+
family: Optional[str]
2612+
emphasis: Optional[TextEmphasis]
2613+
size_pt: Optional[int]
2614+
# Colors are single number grey scales or (red, green, blue) tuples:
2615+
color: Optional[Union[int, tuple, DeviceGray, DeviceRGB]]
2616+
fill_color: Optional[Union[int, tuple, DeviceGray, DeviceRGB]]
2617+
26092618
def __init__(
2610-
self,
2611-
family: Optional[str] = None,
2612-
emphasis: Optional[Union[str, TextEmphasis]] = None,
2613-
size_pt: Optional[int] = None,
2614-
color: Optional[
2615-
Union[int, tuple, DeviceGray, DeviceRGB]
2616-
] = None, # grey scale or (red, green, blue)
2617-
fill_color: Optional[
2618-
Union[int, tuple, DeviceGray, DeviceRGB]
2619-
] = None, # grey scale or (red, green, blue)
2619+
self, family=None, emphasis=None, size_pt=None, color=None, fill_color=None
26202620
):
26212621
self.family = family
26222622
self.emphasis = TextEmphasis.coerce(emphasis) if emphasis else None
26232623
self.size_pt = size_pt
26242624
self.color = color
26252625
self.fill_color = fill_color
2626+
2627+
replace = replace

fpdf/fpdf.py

+3
Original file line numberDiff line numberDiff line change
@@ -4512,6 +4512,9 @@ def _use_title_style(self, title_style: TitleStyle):
45124512

45134513
@contextmanager
45144514
def use_font_style(self, font_style: FontStyle):
4515+
if not font_style:
4516+
yield
4517+
return
45154518
prev_font = (self.font_family, self.font_style, self.font_size_pt)
45164519
self.set_font(
45174520
font_style.family or self.font_family,

0 commit comments

Comments
 (0)