Skip to content

Commit

Permalink
Preserving the initial text color when using write_html() - fix #846
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-C committed Jul 10, 2023
1 parent f3e981a commit 4237fd7
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ This can also be enabled programmatically with `warnings.simplefilter('default',
### Fixed
- [`FPDF.table()`](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.table): the `colspan` setting has been fixed - [documentation](https://pyfpdf.github.io/fpdf2/Tables.html#column-span)
- [`FPDF.image()`](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.image): allowing images path starting with `data` to be passed as input
- the initial text color is preserved when using `FPDF.write_html()` - _cf._ [issue #846](https://github.com/PyFPDF/fpdf2/issues/846)
### Deprecated
- the `center` optional parameter of [`FPDF.cell()`](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.cell) is **no more** deprecated, as it allows for horizontal positioning, which is different from text alignment control with `align="C"`

Expand Down
2 changes: 1 addition & 1 deletion fpdf/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def __init__(
self.bullet = []
self.font_size = pdf.font_size_pt
self.set_font(pdf.font_family or "times", size=self.font_size)
self.font_color = 0, 0, 0 # initialize font color, r,g,b format
self.font_color = tuple((255 * v for v in pdf.text_color.colors))
self.heading_level = None
self.heading_sizes = dict(**DEFAULT_HEADING_SIZES)
self.heading_above = 0.2 # extra space above heading, relative to font size
Expand Down
Binary file added test/html/html_preserve_initial_text_color.pdf
Binary file not shown.
9 changes: 9 additions & 0 deletions test/html/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,3 +449,12 @@ def test_html_custom_pre_code_font(tmp_path): # issue 770
pdf.add_page()
pdf.write_html("<code> Cześć! </code>", pre_code_font="DejaVuSansMono")
assert_pdf_equal(pdf, HERE / "html_custom_pre_code_font.pdf", tmp_path)


def test_html_preserve_initial_text_color(tmp_path): # issue 846
pdf = FPDF()
pdf.add_page()
pdf.set_text_color(200, 50, 50)
pdf.set_font(family="Helvetica", size=13)
pdf.write_html("one <font size=8>two</font> three")
assert_pdf_equal(pdf, HERE / "html_preserve_initial_text_color.pdf", tmp_path)

0 comments on commit 4237fd7

Please sign in to comment.