Skip to content

Commit

Permalink
Raising an error when section_title_styles are used with write_html()…
Browse files Browse the repository at this point in the history
… - cf. #1080 (#1083)
  • Loading branch information
Lucas-C authored Jan 2, 2024
1 parent f2496a7 commit 7a84899
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
11 changes: 6 additions & 5 deletions fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4851,13 +4851,14 @@ def start_section(self, name, level=0, strict=True):

@contextmanager
def _use_title_style(self, title_style: TitleStyle):
if title_style.t_margin:
self.ln(title_style.t_margin)
if title_style.l_margin:
self.set_x(title_style.l_margin)
if title_style:
if title_style.t_margin:
self.ln(title_style.t_margin)
if title_style.l_margin:
self.set_x(title_style.l_margin)
with self.use_font_face(title_style):
yield
if title_style.b_margin:
if title_style and title_style.b_margin:
self.ln(title_style.b_margin)

@contextmanager
Expand Down
5 changes: 5 additions & 0 deletions fpdf/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,11 @@ def _write_data(self, data):
self.put_link(data)
else:
if self.heading_level:
if self.pdf.section_title_styles:
raise NotImplementedError(
"Combining write_html() & section styles is currently not supported."
" You can open up an issue on github.com/py-pdf/fpdf2 if this is something you would like to see implemented."
)
self.pdf.start_section(data, self.heading_level - 1, strict=False)
LOGGER.debug(f"write: '%s' h={self.h:.2f}", data)
self._write_paragraph(data)
Expand Down
18 changes: 17 additions & 1 deletion test/html/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from fpdf import FPDF, HTMLMixin
from fpdf import FPDF, HTMLMixin, TitleStyle
from fpdf.errors import FPDFException
from test.conftest import assert_pdf_equal, LOREM_IPSUM

Expand Down Expand Up @@ -525,3 +525,19 @@ def test_html_ln_outside_p(tmp_path):
"""
)
assert_pdf_equal(pdf, HERE / "html_ln_outside_p.pdf", tmp_path)


def test_html_and_section_title_styles(): # issue 1080
pdf = FPDF()
pdf.add_page()
pdf.set_font("Helvetica", size=10)
pdf.set_section_title_styles(TitleStyle("Helvetica", "B", 20, (0, 0, 0)))
with pytest.raises(NotImplementedError):
pdf.write_html(
"""
<h1>Heading One</h1>
<p>Just enough text to show how bad the situation really is</p>
<h2>Heading Two</h2>
<p>This will not overflow</p>
"""
)

0 comments on commit 7a84899

Please sign in to comment.