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

Increase flexibility of TableBordersLayout #1005

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ This can also be enabled programmatically with `warnings.simplefilter('default',
* SVG importing now supports clipping paths, and `defs` tags anywhere in the SVG file
* [`TextColumns()`](https://py-pdf.github.io/fpdf2/TextColumns.html) can now have images inserted (both raster and vector).
* [`TextColumns()`](https://py-pdf.github.io/fpdf2/TextColumns.html) can now advance to the next column with the new `new_column()` method or a FORM_FEED character (`\u000c`) in the text.
* [`TableBordersLayout`](https://py-pdf.github.io/fpdf2/fpdf/table.html#fpdf.table.TableBordersLayout) is now a non-enum class that supports customizable layouts. The members of the old enum class are now static members of the class and thus can be used as standard options using the same syntax as previously.
### Fixed
* Previously set dash patterns were not transferred correctly to new pages.
* Inserted Vector images used to ignore the `keep_aspect_ratio` argument.
Expand Down
31 changes: 31 additions & 0 deletions docs/Tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,37 @@ Result:

![](table_with_single_top_line_layout.jpg)

It is also possible to create a custom border layout, controlling thickness, color, and dash pattern:
```python
from fpdf.table import TableBordersLayout, TableBorderStyle, TableCellStyle

gray = (150, 150, 150)
red = (255, 0, 0)
custom_layout = TableBordersLayout(
cell_style_getter=lambda row_num, col_num, num_heading_rows, num_rows, num_cols: TableCellStyle(
left=(
True if col_num == 0
else TableBorderStyle(color=(150, 150, 150), dash=2) if col_num == 2
else False
), bottom=True if row_num == num_rows - 1 else False,
right=True if col_num == num_cols - 1 else False,
top=(
True if row_num == 0
else TableBorderStyle(thickness=1) if row_num == num_heading_rows
else TableBorderStyle(color=red, dash=2)
),
)
)

with pdf.table(borders_layout=custom_layout) as table:
...
```

Result:

![](table-with-custom-border-layout.jpg)


All the possible layout values are described
there: [`TableBordersLayout`](https://py-pdf.github.io/fpdf2/fpdf/enums.html#fpdf.enums.TableBordersLayout).

Expand Down
Binary file added docs/table-with-custom-border-layout.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 0 additions & 25 deletions fpdf/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,31 +274,6 @@ class MethodReturnValue(CoerciveIntFlag):
"The method will return how much vertical space was used"


class TableBordersLayout(CoerciveEnum):
"Defines how to render table borders"

ALL = intern("ALL")
"Draw all table cells borders"

NONE = intern("NONE")
"Draw zero cells border"

INTERNAL = intern("INTERNAL")
"Draw only internal horizontal & vertical borders"

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"

SINGLE_TOP_LINE = intern("SINGLE_TOP_LINE")
"Draw only the top horizontal border, below the headings"


class TableCellFillMode(CoerciveEnum):
"Defines which table cells to fill"

Expand Down