Skip to content

Commit c55f1e3

Browse files
authored
Introducing pre_code_font optional parameter for FPDF.write_html() (#777)
1 parent bdd4638 commit c55f1e3

File tree

6 files changed

+14
-3
lines changed

6 files changed

+14
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ This can also be enabled programmatically with `warnings.simplefilter('default',
2121
- 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
2222
- documentation on how to use `fpdf2` with [FastAPI](https://fastapi.tiangolo.com/): <https://pyfpdf.github.io/fpdf2/UsageInWebAPI.html#FastAPI> - thanks to @KamarulAdha
2323
- [`FPDF.write_html()`](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.write_html): `<table>` elements can now be aligned left or right on the page using `align=`
24+
- [`FPDF.write_html()`](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.write_html): a custom font can now be specified for `<code>` & `<pre>` elements, using the new optional parameter `pre_code_font`
2425
### Fixed
2526
- [`FPDF.table()`](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.table): images no more overflow cells
2627
- [`FPDF.table()`](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.table): text overflow in the last cell of the header row is now properly handled

fpdf/html.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ def __init__(
206206
table_line_separators=False,
207207
ul_bullet_char=BULLET_WIN1252,
208208
heading_sizes=None,
209+
pre_code_font="courier",
209210
warn_on_tags_not_matching=True,
210211
**_,
211212
):
@@ -244,6 +245,7 @@ def __init__(
244245
self.heading_below = 0.2 # extra space below heading, relative to font size
245246
if heading_sizes:
246247
self.heading_sizes.update(heading_sizes)
248+
self.pre_code_font = pre_code_font
247249
self.warn_on_tags_not_matching = warn_on_tags_not_matching
248250
self._tags_stack = []
249251
# <table>-related properties:
@@ -375,10 +377,10 @@ def handle_starttag(self, tag, attrs):
375377
self.pdf.add_page(same=True)
376378
if tag == "code":
377379
self.font_stack.append((self.font_face, self.font_size, self.font_color))
378-
self.set_font("courier", 11)
380+
self.set_font(self.pre_code_font, 11)
379381
if tag == "pre":
380382
self.font_stack.append((self.font_face, self.font_size, self.font_color))
381-
self.set_font("courier", 11)
383+
self.set_font(self.pre_code_font, 11)
382384
self.pre_formatted = True
383385
if tag == "blockquote":
384386
self.pdf.set_text_color(100, 0, 45)

test/fonts/DejaVuSansMono.ttf

327 KB
Binary file not shown.
88.4 KB
Binary file not shown.
7.87 KB
Binary file not shown.

test/html/test_html.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def test_html_justify_paragraph(tmp_path):
232232

233233
def test_issue_156(tmp_path):
234234
pdf = FPDF()
235-
pdf.add_font("Roboto", style="B", fname="test/fonts/Roboto-Bold.ttf")
235+
pdf.add_font("Roboto", style="B", fname=HERE / "../fonts/Roboto-Bold.ttf")
236236
pdf.set_font("Roboto", style="B")
237237
pdf.add_page()
238238
with pytest.raises(FPDFException) as error:
@@ -440,3 +440,11 @@ def test_html_unorthodox_headings_hierarchy(tmp_path): # issue 631
440440
<h5>H5</h5>"""
441441
)
442442
assert_pdf_equal(pdf, HERE / "html_unorthodox_headings_hierarchy.pdf", tmp_path)
443+
444+
445+
def test_html_custom_pre_code_font(tmp_path): # issue 770
446+
pdf = FPDF()
447+
pdf.add_font(fname=HERE / "../fonts/DejaVuSansMono.ttf")
448+
pdf.add_page()
449+
pdf.write_html("<code> Cześć! </code>", pre_code_font="DejaVuSansMono")
450+
assert_pdf_equal(pdf, HERE / "html_custom_pre_code_font.pdf", tmp_path)

0 commit comments

Comments
 (0)