Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write_html(): homogenization in handling of tag_styles + new optional arg font_family #1217

Merged
merged 9 commits into from
Jul 5, 2024
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',
* feature to identify the Unicode script of the input text and break it into fragments when different scripts are used, improving [text shaping](https://py-pdf.github.io/fpdf2/TextShaping.html) results
* [`FPDF.image()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.image): now handles `keep_aspect_ratio` in combination with an enum value provided to `x`
* [`FPDF.write_html()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.write_html): now supports CSS page breaks properties : [documentation](https://py-pdf.github.io/fpdf2/HTML.html#page-breaks)
* [`FPDF.write_html()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.write_html): new optional `font_family` parameter to set the default font family
* [`FPDF.write_html()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.write_html): spacing before lists can now be adjusted via the `tag_styles` attribute - thanks to @lcgeneralprojects
* file names are mentioned in errors when `fpdf2` fails to parse a SVG image
### Fixed
Expand Down
25 changes: 23 additions & 2 deletions docs/HTML.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ pdf.write_html("""
pdf.output("html.pdf")
```


### Styling HTML tags globally

_New in [:octicons-tag-24: 2.7.9](https://github.com/py-pdf/fpdf2/blob/master/CHANGELOG.md)_
Expand Down Expand Up @@ -132,10 +131,32 @@ pdf.output("html_dd_indented.pdf")
and that some [`FontFace`](https://py-pdf.github.io/fpdf2/fpdf/fonts.html#fpdf.fonts.FontFace) or [`TextStyle`](https://py-pdf.github.io/fpdf2/fpdf/fonts.html#fpdf.fonts.TextStyle) properties may not be honored.
However, **Pull Request are welcome** to implement missing features!

### Default font

_New in [:octicons-tag-24: 2.7.10](https://github.com/py-pdf/fpdf2/blob/master/CHANGELOG.md)_

The default font used by [`FPDF.write_html()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.write_html) is **Times**.

You can change this default font by passing `font_family` to this method:
```python
from fpdf import FPDF

pdf = FPDF()
pdf.add_page()
pdf.write_html("""
<h1>Big title</h1>
<section>
<h2>Section title</h2>
<p>Hello world!</p>
</section>
""", font_family="Helvetica")
pdf.output("html_helvetica.pdf")
```


## Supported HTML features

* `<h1>` to `<h8>`: headings (and `align` attribute)
* `<h1>` to `<h6>`: headings (and `align` attribute)
* `<p>`: paragraphs (and `align`, `line-height` attributes)
* `<br>` & `<hr>` tags
* `<b>`, `<i>`, `<u>`: bold, italic, underline
Expand Down
16 changes: 12 additions & 4 deletions fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,12 @@ def __init__(
# Graphics state variables defined as properties by GraphicsStateMixin.
# We set their default values here.
self.font_family = "" # current font family
self.font_style = "" # current font style
# current font style (BOLD/ITALICS - does not handle UNDERLINE):
Lucas-C marked this conversation as resolved.
Show resolved Hide resolved
self.font_style = ""
self.underline = False # underlining flag
self.font_size_pt = 12 # current font size in points
self.font_stretching = 100 # current font stretching
self.char_spacing = 0 # current character spacing
self.underline = False # underlining flag
self.current_font = None # None or an instance of CoreFont or TTFFont
self.draw_color = self.DEFAULT_DRAW_COLOR
self.fill_color = self.DEFAULT_FILL_COLOR
Expand Down Expand Up @@ -410,11 +411,18 @@ def _set_min_pdf_version(self, version):
self.pdf_version = max(self.pdf_version, version)

@property
def is_ttf_font(self):
def emphasis(self) -> TextEmphasis:
"The current text emphasis: bold, italics and/or underlined."
return TextEmphasis.coerce(
f"{self.font_style}U" if self.underline else self.font_style
)

@property
def is_ttf_font(self) -> bool:
return self.current_font and self.current_font.type == "TTF"

@property
def page_mode(self):
def page_mode(self) -> PageMode:
return self._page_mode

@page_mode.setter
Expand Down
Loading
Loading