forked from reingart/pyfpdf
-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
110 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from numbers import Number | ||
|
||
|
||
class Table: | ||
def __init__(self, fpdf): | ||
self.fpdf = fpdf | ||
self.set_col_widths = None | ||
self.rows = [] | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, exc_type=None, exc_val=None, exc_tb=None): | ||
line_height = 2 * self.fpdf.font_size | ||
for i, row in enumerate(self.rows): | ||
for j, cell in enumerate(row.cells): | ||
col_width = self._get_col_width(i, j) | ||
self.fpdf.multi_cell( | ||
col_width, | ||
line_height, | ||
cell, | ||
border=1, | ||
new_x="RIGHT", | ||
new_y="TOP", | ||
max_line_height=self.fpdf.font_size, | ||
) | ||
self.fpdf.ln(line_height) | ||
|
||
def _get_col_width(self, i, _): | ||
if not self.set_col_widths: | ||
cols_count = len(self.rows[i].cells) | ||
return self.fpdf.epw / cols_count | ||
if isinstance(self.set_col_widths, Number): | ||
return self.set_col_widths | ||
# pylint: disable=unsubscriptable-object | ||
return self.set_col_widths[i] | ||
|
||
def row(self): | ||
row = Row(self) | ||
self.rows.append(row) | ||
return row | ||
|
||
|
||
class Row: | ||
def __init__(self, table): | ||
self.table = table | ||
self.cells = [] | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, exc_type=None, exc_val=None, exc_tb=None): | ||
pass | ||
|
||
def cell(self, text): | ||
self.cells.append(text) |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from pathlib import Path | ||
|
||
from fpdf import FPDF | ||
from test.conftest import assert_pdf_equal | ||
|
||
|
||
HERE = Path(__file__).resolve().parent | ||
FONTS_DIR = HERE.parent / "fonts" | ||
|
||
TABLE_DATA = ( | ||
("First name", "Last name", "Age", "City"), | ||
("Jules", "Smith", "34", "San Juan"), | ||
("Mary", "Ramos", "45", "Orlando"), | ||
("Carlson", "Banks", "19", "Los Angeles"), | ||
("Lucas", "Cimon", "31", "Angers"), | ||
) | ||
|
||
|
||
def test_simple_table(tmp_path): | ||
pdf = FPDF() | ||
pdf.add_page() | ||
pdf.set_font("Times", size=16) | ||
with pdf.table() as table: | ||
table.col_widths = pdf.epw / 4 | ||
for data_row in TABLE_DATA: | ||
with table.row() as row: | ||
for datum in data_row: | ||
row.cell(datum) | ||
assert_pdf_equal(pdf, HERE / "simple_table.pdf", tmp_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters