Skip to content

Commit

Permalink
Using new Table rendering in write_html()
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-C committed Feb 24, 2023
1 parent aaac62f commit 8c730bd
Show file tree
Hide file tree
Showing 45 changed files with 425 additions and 596 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/continuous-integration-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ jobs:
sed -i "s/author:.*/author: v$(python setup.py -V 2>/dev/null)/" mkdocs.yml
cp tutorial/notebook.ipynb docs/
mkdocs build
pdoc --html -o public/ fpdf --config "git_link_template='https://github
.com/PyFPDF/fpdf2/blob/{commit}/{path}#L{start_line}-L{end_line}'"
pdoc --html -o public/ fpdf --config "git_link_template='https://github.com/PyFPDF/fpdf2/blob/{commit}/{path}#L{start_line}-L{end_line}'"
cd contributors/ && PYTHONUNBUFFERED=1 ./build_contributors_html_page.py PyFPDF/fpdf2
cp -t ../public/ contributors.html contributors-map-small.png
- name: Deploy documentation 🚀
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ This can also be enabled programmatically with `warnings.simplefilter('default',
- unicode (non limited to ASCII) text can now be provided as metadata [#685](https://github.com/PyFPDF/fpdf2/issues/685)
- all `TitleStyle` constructor parameters are now effectively optional
### Changed
* [`FPDF.write_html()`](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.write_html) now uses the new [`FPDF.table()`](https://pyfpdf.github.io/fpdf2/Tables.html) method to render `<table>` tags. As a consequence, vertical space before `<table>` tags has sometimes been reduced.
- vector images parsing is now more robust: `fpdf2` can now embed SVG files without `viewPort` or no `height` / `width`
- bitonal images are now encoded using `CCITTFaxDecode`, reducing their size in the PDF document - thanks to @eroux
- when possible, JPG and group4 encoded TIFFs are now embedded directly without recompression - thanks to @eroux
### Removed
* [`FPDF.write_html()`](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.write_html) now uses the new [`FPDF.table()`](https://pyfpdf.github.io/fpdf2/Tables.html) method to render `<table>` tags. As a consequence, it does not support the `height` attribute defined on `<td>` / `<th>` tags anymore, nor `height` / `width` attributes defined on `<img>` tags inside cells, nor `width` attributes defined on `<thoot>` / `<tfoot>` tags.

## [2.6.1] - 2023-01-13
### Added
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pip install git+https://github.com/PyFPDF/fpdf2.git@master
* Embedding images, including transparency and alpha channel
* Arbitrary path drawing and basic [SVG](https://pyfpdf.github.io/fpdf2/SVG.html) import
* Embedding [barcodes](https://pyfpdf.github.io/fpdf2/Barcodes.html), [charts & graphs](https://pyfpdf.github.io/fpdf2/Maths.html), [emojis, symbols & dingbats](https://pyfpdf.github.io/fpdf2/EmojisSymbolsDingbats.html)
* [Cell / multi-cell / plaintext writing](https://pyfpdf.github.io/fpdf2/Text.html), with [automatic page breaks](https://pyfpdf.github.io/fpdf2/PageBreaks.html), line break and text justification
* [Tables](https://pyfpdf.github.io/fpdf2/Tables.html) and also [cell / multi-cell / plaintext writing](https://pyfpdf.github.io/fpdf2/Text.html), with [automatic page breaks](https://pyfpdf.github.io/fpdf2/PageBreaks.html), line break and text justification
* Choice of measurement unit, page format & margins. Optional page header and footer
* Basic [conversion from HTML to PDF](https://pyfpdf.github.io/fpdf2/HTML.html)
* A [templating system](https://pyfpdf.github.io/fpdf2/Templates.html) to render PDFs in batchs
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Go try it **now** online in a Jupyter notebook: [![Open In Colab](https://colab.
* Embedding images, including transparency and alpha channel, using [Pillow (Python Imaging Library)](https://pillow.readthedocs.io/en/stable/)
* Arbitrary path drawing and basic [SVG](SVG.md) import
* Embedding [barcodes](Barcodes.md), [charts & graphs](Maths.md), [emojis, symbols & dingbats](EmojisSymbolsDingbats.md)
* [Cell / multi-cell / plaintext writing](Text.md), with [automatic page breaks](PageBreaks.md), line break and text justification
* [Tables](Tables.md), and also [cell / multi-cell / plaintext writing](Text.md), with [automatic page breaks](PageBreaks.md), line break and text justification
* Choice of measurement unit, page format & margins. Optional page header and footer
* Basic [conversion from HTML to PDF](HTML.md)
* A [templating system](Templates.md) to render PDFs in batchs
Expand Down
16 changes: 10 additions & 6 deletions fpdf/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,13 @@ def style(self):

@classmethod
def coerce(cls, value):
if value.upper() == "BOLD":
return cls.B
if value.upper() == "ITALICS":
return cls.I
if value.upper() == "UNDERLINE":
return cls.U
if isinstance(value, str):
if value.upper() == "BOLD":
return cls.B
if value.upper() == "ITALICS":
return cls.I
if value.upper() == "UNDERLINE":
return cls.U
return super(cls, cls).coerce(value)


Expand All @@ -236,6 +237,9 @@ class TableBordersLayout(CoerciveEnum):
MINIMAL = intern("MINIMAL")
"Draw only the top horizontal border, below the headings, and internal vertical borders"

HORIZONTAL_LINES = intern("HORIZONTAL_LINES")
"Draw only horizontal lines"

NO_HORIZONTAL_LINES = intern("NO_HORIZONTAL_LINES")
"Draw all cells border except horizontal lines, after the headings"

Expand Down
22 changes: 12 additions & 10 deletions fpdf/fonts.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Definition of the character widths of all PDF standard fonts.
"""
from dataclasses import dataclass, replace
from typing import Optional, Union

from .drawing import DeviceGray, DeviceRGB
Expand Down Expand Up @@ -2605,21 +2606,22 @@
}


@dataclass
class FontStyle:
family: Optional[str]
emphasis: Optional[TextEmphasis]
size_pt: Optional[int]
# Colors are single number grey scales or (red, green, blue) tuples:
color: Optional[Union[int, tuple, DeviceGray, DeviceRGB]]
fill_color: Optional[Union[int, tuple, DeviceGray, DeviceRGB]]

def __init__(
self,
family: Optional[str] = None,
emphasis: Optional[Union[str, TextEmphasis]] = None,
size_pt: Optional[int] = None,
color: Optional[
Union[int, tuple, DeviceGray, DeviceRGB]
] = None, # grey scale or (red, green, blue)
fill_color: Optional[
Union[int, tuple, DeviceGray, DeviceRGB]
] = None, # grey scale or (red, green, blue)
self, family=None, emphasis=None, size_pt=None, color=None, fill_color=None
):
self.family = family
self.emphasis = TextEmphasis.coerce(emphasis) if emphasis else None
self.size_pt = size_pt
self.color = color
self.fill_color = fill_color

replace = replace
3 changes: 3 additions & 0 deletions fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4512,6 +4512,9 @@ def _use_title_style(self, title_style: TitleStyle):

@contextmanager
def use_font_style(self, font_style: FontStyle):
if not font_style:
yield
return
prev_font = (self.font_family, self.font_style, self.font_size_pt)
self.set_font(
font_style.family or self.font_family,
Expand Down
Loading

0 comments on commit 8c730bd

Please sign in to comment.