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
13 changed files
with
229 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,88 @@ | ||
from numbers import Number | ||
|
||
|
||
class Table: | ||
def __init__(self, fpdf, line_height=None, width=None): | ||
self.fpdf = fpdf | ||
self.line_height = line_height or 2 * self.fpdf.font_size | ||
self.width = width or fpdf.epw | ||
self.col_widths = None | ||
self.rows = [] | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def row(self): | ||
row = Row(self) | ||
self.rows.append(row) | ||
return row | ||
|
||
def __exit__(self, exc_type=None, exc_val=None, exc_tb=None): | ||
for i, row in enumerate(self.rows): | ||
lines_count_per_cell = self._get_lines_count_per_cell(i) | ||
row_height = max(lines_count_per_cell) * self.line_height | ||
for j in range(len(row.cells)): | ||
cell_line_height = row_height / lines_count_per_cell[j] | ||
self._render_table_cell( | ||
i, j, h=row_height, max_line_height=cell_line_height | ||
) | ||
self.fpdf.ln(row_height) | ||
|
||
def _render_table_cell(self, i, j, h, **kwargs): | ||
row = self.rows[i] | ||
col_width = self._get_col_width(i, j) | ||
return self.fpdf.multi_cell( | ||
w=col_width, | ||
h=h, | ||
txt=row.cells[j], | ||
border=1, | ||
new_x="RIGHT", | ||
new_y="TOP", | ||
**kwargs, | ||
) | ||
|
||
def _get_col_width(self, i, j): | ||
if not self.col_widths: | ||
cols_count = len(self.rows[i].cells) | ||
return self.width / cols_count | ||
if isinstance(self.col_widths, Number): | ||
return self.col_widths | ||
if j >= len(self.col_widths): | ||
raise ValueError( | ||
f"Invalid .col_widths specified: missing width for table() column {j + 1} on row {i + 1}" | ||
) | ||
# pylint: disable=unsubscriptable-object | ||
col_ratio = self.col_widths[j] / sum(self.col_widths) | ||
return col_ratio * self.width | ||
|
||
def _get_lines_count_per_cell(self, i): | ||
row = self.rows[i] | ||
lines_count = [] | ||
for j in range(len(row.cells)): | ||
lines_count.append( | ||
len( | ||
self._render_table_cell( | ||
i, | ||
j, | ||
h=self.line_height, | ||
max_line_height=self.line_height, | ||
split_only=True, | ||
) | ||
) | ||
) | ||
return lines_count | ||
|
||
|
||
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.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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,116 @@ | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
from fpdf import FPDF | ||
from test.conftest import assert_pdf_equal, LOREM_IPSUM | ||
|
||
|
||
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"), | ||
) | ||
MULTILINE_TABLE_DATA = ( | ||
("Extract", "Text length"), | ||
(LOREM_IPSUM[:200], str(len(LOREM_IPSUM[:200]))), | ||
(LOREM_IPSUM[200:400], str(len(LOREM_IPSUM[200:400]))), | ||
(LOREM_IPSUM[400:600], str(len(LOREM_IPSUM[400:600]))), | ||
(LOREM_IPSUM[600:800], str(len(LOREM_IPSUM[600:800]))), | ||
(LOREM_IPSUM[800:1000], str(len(LOREM_IPSUM[800:1000]))), | ||
(LOREM_IPSUM[1000:1200], str(len(LOREM_IPSUM[1000:1200]))), | ||
) | ||
|
||
|
||
def test_table_simple(tmp_path): | ||
pdf = FPDF() | ||
pdf.add_page() | ||
pdf.set_font("Times", size=16) | ||
with pdf.table() as table: | ||
for data_row in TABLE_DATA: | ||
with table.row() as row: | ||
for datum in data_row: | ||
row.cell(datum) | ||
assert_pdf_equal(pdf, HERE / "table_simple.pdf", tmp_path) | ||
|
||
|
||
def test_table_with_fixed_col_width(tmp_path): | ||
pdf = FPDF() | ||
pdf.add_page() | ||
pdf.set_font("Times", size=16) | ||
with pdf.table() as table: | ||
table.col_widths = pdf.epw / 5 | ||
for data_row in TABLE_DATA: | ||
with table.row() as row: | ||
for datum in data_row: | ||
row.cell(datum) | ||
assert_pdf_equal(pdf, HERE / "table_with_fixed_col_width.pdf", tmp_path) | ||
|
||
|
||
def test_table_with_varying_col_widths(tmp_path): | ||
pdf = FPDF() | ||
pdf.add_page() | ||
pdf.set_font("Times", size=16) | ||
with pdf.table() as table: | ||
table.col_widths = (30, 30, 10, 30) | ||
for data_row in TABLE_DATA: | ||
with table.row() as row: | ||
for datum in data_row: | ||
row.cell(datum) | ||
assert_pdf_equal(pdf, HERE / "table_with_varying_col_widths.pdf", tmp_path) | ||
|
||
|
||
def test_table_with_invalid_col_widths(): | ||
pdf = FPDF() | ||
pdf.add_page() | ||
pdf.set_font("Times", size=16) | ||
with pytest.raises(ValueError): | ||
with pdf.table() as table: | ||
table.col_widths = (20, 30, 50) | ||
for data_row in TABLE_DATA: | ||
with table.row() as row: | ||
for datum in data_row: | ||
row.cell(datum) | ||
|
||
|
||
def test_table_with_fixed_row_height(tmp_path): | ||
pdf = FPDF() | ||
pdf.add_page() | ||
pdf.set_font("Times", size=16) | ||
with pdf.table(line_height=2.5 * pdf.font_size) as table: | ||
for data_row in TABLE_DATA: | ||
with table.row() as row: | ||
for datum in data_row: | ||
row.cell(datum) | ||
assert_pdf_equal(pdf, HERE / "table_with_fixed_row_height.pdf", tmp_path) | ||
|
||
|
||
def test_table_with_multiline_cells(tmp_path): | ||
pdf = FPDF() | ||
pdf.add_page() | ||
pdf.set_font("Times", size=16) | ||
with pdf.table() as table: | ||
for data_row in MULTILINE_TABLE_DATA: | ||
with table.row() as row: | ||
for datum in data_row: | ||
row.cell(datum) | ||
assert_pdf_equal(pdf, HERE / "table_with_multiline_cells.pdf", tmp_path) | ||
|
||
|
||
def test_table_with_multiline_cells_and_fixed_row_height(tmp_path): | ||
pdf = FPDF() | ||
pdf.add_page() | ||
pdf.set_font("Times", size=16) | ||
with pdf.table(line_height=2.5 * pdf.font_size) as table: | ||
for data_row in MULTILINE_TABLE_DATA: | ||
with table.row() as row: | ||
for datum in data_row: | ||
row.cell(datum) | ||
assert_pdf_equal( | ||
pdf, HERE / "table_with_multiline_cells_and_fixed_row_height.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