ul_bullet_char (str): bullet character for elements
+ ul_bullet_color (tuple | str | drawing.Device* instance): color of the bullets
heading_sizes (dict): font size per heading level names ("h1", "h2"...)
pre_code_font (str): font to use for
& blocks
warn_on_tags_not_matching (bool): control warnings production for unmatched HTML tags
diff --git a/fpdf/html.py b/fpdf/html.py
index c5e8bd3fa..9f6cb475e 100644
--- a/fpdf/html.py
+++ b/fpdf/html.py
@@ -9,9 +9,10 @@
from html.parser import HTMLParser
import logging, re, warnings
+from .deprecation import get_stack_level
+from .drawing import color_from_hex_string, convert_to_device_color
from .enums import TextEmphasis, XPos, YPos
from .errors import FPDFException
-from .deprecation import get_stack_level
from .fonts import FontFace
from .table import Table
@@ -215,17 +216,7 @@ def color_as_decimal(color="#000000"):
# Checks if color is a name and gets the hex value
hexcolor = COLOR_DICT.get(color.lower(), color)
-
- if len(hexcolor) == 4:
- r = int(hexcolor[1] * 2, 16)
- g = int(hexcolor[2] * 2, 16)
- b = int(hexcolor[3] * 2, 16)
- return r, g, b
-
- r = int(hexcolor[1:3], 16)
- g = int(hexcolor[3:5], 16)
- b = int(hexcolor[5:7], 16)
- return r, g, b
+ return color_from_hex_string(hexcolor).colors255
class HTML2FPDF(HTMLParser):
@@ -241,6 +232,7 @@ def __init__(
dd_tag_indent=10,
table_line_separators=False,
ul_bullet_char=BULLET_WIN1252,
+ ul_bullet_color=(190, 0, 0),
heading_sizes=None,
pre_code_font="courier",
warn_on_tags_not_matching=True,
@@ -255,6 +247,7 @@ def __init__(
dd_tag_indent (int): numeric indentation of elements
table_line_separators (bool): enable horizontal line separators in
ul_bullet_char (str): bullet character for elements
+ ul_bullet_color (tuple | str | drawing.Device* instance): color of the bullets
heading_sizes (dict): font size per heading level names ("h1", "h2"...)
pre_code_font (str): font to use for
& blocks
warn_on_tags_not_matching (bool): control warnings production for unmatched HTML tags
@@ -265,6 +258,11 @@ def __init__(
self.li_tag_indent = li_tag_indent
self.dd_tag_indent = dd_tag_indent
self.ul_bullet_char = ul_bullet_char
+ self.ul_bullet_color = (
+ color_as_decimal(ul_bullet_color)
+ if isinstance(ul_bullet_color, str)
+ else convert_to_device_color(ul_bullet_color).colors255
+ )
self.heading_sizes = dict(**DEFAULT_HEADING_SIZES)
if heading_sizes:
self.heading_sizes.update(heading_sizes)
@@ -519,7 +517,7 @@ def handle_starttag(self, tag, attrs):
self._new_paragraph()
if tag == "li":
self._ln(2)
- self.set_text_color(190, 0, 0)
+ self.set_text_color(*self.ul_bullet_color)
if self.bullet:
bullet = self.bullet[self.indent - 1]
else:
diff --git a/test/html/html_ul_bullet_color.pdf b/test/html/html_ul_bullet_color.pdf
new file mode 100644
index 0000000000000000000000000000000000000000..58219916d16cc36f645d335bf5ecd130710d2d23
GIT binary patch
literal 1066
zcmah|Pe>F|7ze|HPqDxfEUI@%w1dvf?#}E?uEOk}NzvG4m4p_larf;uGW*^(^ETG%
zWI=RMPZ^ZpNje2ZK_GRpqCz@EC7dx2UBs=up{>gzo^e%7a_r34;{oe2UUS}d|
z^hmvaAVCq5$te&D0U<()m_i8%!&b(HDL)G(CxCDPWh^sHN-!ldMAqb9p9`8>4o0>XNlS_D@)pu&RJHLg53|Kok*-qO*~R#Wr$
z-<#`;r(1{L>>qi4y!%*UYN>tq-prGp`GV20J%0bo<&9?>MErbZw{7;&-L=BtH}hdf
z>&a`6=U=?K-Lf-xW_xFBb#Qh)uKZcvYe-$Zd7ZbUDzw@h*>lUV=^}$LpM)_hkKCRl
z$(zjm7-sjia1ILN7S^$A)c_!j7AI-B=e+Z5SSka+IXp&klm8jX=|dsGkRY#|WX1^z
zhonalg(-mzBCCx`OuzYMeR0MlSO1n~i;vD@(F!6dEzoG)09$znaGh-w|5
zqA_b~HK=1+Hgd{ua@4wkm}DVI2!;GrvDYw!vjlQ0_;(bhLb{@hS~Q@GK3^;r42BI&
j78N5bDzc%50|EcQ|4)=%cB$#mvQ0r*0iB((xB>nGp=3CN
literal 0
HcmV?d00001
diff --git a/test/html/test_html.py b/test/html/test_html.py
index 575bc9be7..7d8a0355c 100644
--- a/test/html/test_html.py
+++ b/test/html/test_html.py
@@ -3,6 +3,7 @@
import pytest
from fpdf import FPDF, HTMLMixin, TitleStyle
+from fpdf.drawing import DeviceRGB
from fpdf.errors import FPDFException
from test.conftest import assert_pdf_equal, LOREM_IPSUM
@@ -215,6 +216,25 @@ class CustomPDF(FPDF):
assert_pdf_equal(pdf, HERE / "html_customize_ul.pdf", tmp_path)
+def test_html_ul_bullet_color(tmp_path):
+ html = """
+ - item1
+ - item2
+ - item3
+
"""
+
+ pdf = FPDF()
+ pdf.set_font_size(30)
+ pdf.add_page()
+ pdf.write_html(html, ul_bullet_color=0) # black
+ pdf.ln()
+ pdf.write_html(html, ul_bullet_color="green")
+ pdf.ln()
+ pdf.write_html(html, ul_bullet_color=DeviceRGB(r=0.5, g=1, b=0))
+ pdf.ln()
+ assert_pdf_equal(pdf, HERE / "html_ul_bullet_color.pdf", tmp_path)
+
+
def test_html_align_paragraph(tmp_path):
pdf = FPDF()
pdf.add_page()