From 4237fd71cd09711db4452146bd671ff901ca264e Mon Sep 17 00:00:00 2001 From: Lucas Cimon <925560+Lucas-C@users.noreply.github.com> Date: Tue, 11 Jul 2023 00:30:17 +0200 Subject: [PATCH] Preserving the initial text color when using write_html() - fix #846 --- CHANGELOG.md | 1 + fpdf/html.py | 2 +- test/html/html_preserve_initial_text_color.pdf | Bin 0 -> 1031 bytes test/html/test_html.py | 9 +++++++++ 4 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 test/html/html_preserve_initial_text_color.pdf diff --git a/CHANGELOG.md b/CHANGELOG.md index f9e5a5a95..fda90ac23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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"` diff --git a/fpdf/html.py b/fpdf/html.py index 0e6ea2765..db678cb32 100644 --- a/fpdf/html.py +++ b/fpdf/html.py @@ -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 diff --git a/test/html/html_preserve_initial_text_color.pdf b/test/html/html_preserve_initial_text_color.pdf new file mode 100644 index 0000000000000000000000000000000000000000..49c907349fb1aa410c453c69ae5c8017682b6a3d GIT binary patch literal 1031 zcmah|J!lj`6b6aSD5#BSBTq?4Qsic5@AglQ;Qg(MiR8SC3Z4NscawAD-fY;N3zrlM z3Swa}AXc#o*k~2760`^+79l8tAPOQ#DnTnFD#4liF^egjW_G_f?|bik@4cRU)*c|S zI3l2cKRJz(NrW?gopB%tkGPc(N^xN-Odx!kR$M3TH=v{lM6$5f|VB6(G!0MhS2&C(%Gw0Vi;4wxszE`iY794_1 zuh0pSak%68)kqkb;&%3oQ^rF`w0!bTAbvXx1seMGpiD#1qm(jugipG5_-xnXG!VIZ z4yRdK_A3fA!Ze6+baC+_2XY*p$^7Fl4XTE>iRji#E8M<3pSUK7MXz|eP zd)=$0FE^_%j%@Xfd^oqRPoG`>@%q?<+|tIUeeZt{9M*pRsy(@5U3+T0+|uWz;_W=z z+G~*_(oRTiU&w%ms5eWwTXqo6F;R@ms({bCEX6{%10YkpvU>mcbX#%#5S)v+wA{eoD zzEh2zSi;!ps~KV@|KZbhu{a%!2afB}pt*2GcbRj v5W^%XGhw8Nsi-MMOUI3bp0Ukb#?&l*@c%EGDu>(&cvB`rC8(z-H)f+hyl*X7 literal 0 HcmV?d00001 diff --git a/test/html/test_html.py b/test/html/test_html.py index 637cf87c1..040aa83dc 100644 --- a/test/html/test_html.py +++ b/test/html/test_html.py @@ -449,3 +449,12 @@ def test_html_custom_pre_code_font(tmp_path): # issue 770 pdf.add_page() pdf.write_html(" Cześć! ", 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 two three") + assert_pdf_equal(pdf, HERE / "html_preserve_initial_text_color.pdf", tmp_path)