Skip to content

Commit

Permalink
Added tests for table_padding
Browse files Browse the repository at this point in the history
  • Loading branch information
RubendeBruin committed Jul 21, 2023
1 parent 11cb956 commit ad6401b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 99 deletions.
4 changes: 2 additions & 2 deletions fpdf/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def get_padding_tuple(padding: Union[int, float, tuple, list]) -> Padding:
f"padding shall be a number or a sequence of 2, 3 or 4 numbers, got {str(padding)}"
)

def draw_box(pdf, x1, y1, x2, y2, border, fill = None):
def draw_box_borders(pdf, x1, y1, x2, y2, border, fill = None):
"""Draws a box using the provided style - private helper used by table for drawing the cell and table borders.
Difference bewteen this and rect() is that border can be defined as "L,R,T,B" to draw only some of the four borders; compatible with get_border(i,k)
Expand Down Expand Up @@ -401,7 +401,7 @@ def _render_table_cell(
x2 = x1 + col_width # already includes gutter for cells spanning multiple columns
y2 = y1 + cell_height

draw_box(
draw_box_borders(
self._fpdf,
x1, y1, x2, y2, border=self.get_cell_border(i, j),
fill=style.fill_color if fill else None
Expand Down
12 changes: 6 additions & 6 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@ def assert_pdf_equal(
actual_hash = hashlib.md5(actual_pdf_path.read_bytes()).hexdigest()
expected_hash = hashlib.md5(expected_pdf_path.read_bytes()).hexdigest()

if actual_hash != expected_hash:
# open both
import subprocess
subprocess.Popen(f'explorer "{actual_pdf_path}"')
subprocess.Popen(f'explorer "{expected_pdf_path}"')

# trick to open both files in explorer if they are different
# if actual_hash != expected_hash:
# # open both
# import subprocess
# subprocess.Popen(f'explorer "{actual_pdf_path}"')
# subprocess.Popen(f'explorer "{expected_pdf_path}"')

assert actual_hash == expected_hash, f"{actual_hash} != {expected_hash}"

Expand Down
Binary file added test/image/image_types/pythonknight.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
136 changes: 45 additions & 91 deletions test/table/table_padding.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,34 @@
from test.conftest import assert_pdf_equal, LOREM_IPSUM


# Failing
#
# Images only
# test_valign_per_cell
#
# All of them are about horizontal positioning
HERE = Path(__file__).resolve().parent

def show(pdf):
pdf.output(r"c:\data\temp.pdf")
import subprocess
def run_comparison(pdf, name, tmp_path):
filename = HERE / f"{name}.pdf"

subprocess.Popen(r'explorer "c:\data\temp.pdf"')
try:
assert_pdf_equal(pdf, filename, tmp_path, generate=False)
except AssertionError as e:
print(e)
pdf.output(r"c:\data\test.pdf")
import subprocess

subprocess.Popen(rf'explorer "{str(filename)}"')
subprocess.Popen(rf'explorer "c:\data\test.pdf"')


# Debugging on windows
# if filename.exists():
# raise(ValueError(f"File {filename} already exists"))
#
# pdf.output(filename)
# import subprocess
#
# subprocess.Popen(rf'explorer "{str(filename)}"')

HERE = Path(__file__).resolve().parent
IMG_DIR = HERE.parent / "image"

IMAGE = IMG_DIR / "png_images/ba2b2b6e72ca0e4683bb640e2d5572f8.png"
IMAGE = IMG_DIR / "image_types/pythonknight.png"

IMAGES_DATA = (
(IMAGE, IMAGE, IMAGE, IMAGE),
Expand Down Expand Up @@ -74,25 +84,17 @@ def show(pdf):
TWO_LINE_TEXT = "Monty Python\nKiller Sheep"


def test_multicell_with_padding():
def test_multicell_with_padding(tmp_path):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", size=16)
pdf.multi_cell(0, 5, LONG_TEXT, border = 1, padding = (10, 20, 30, 40))

# pdf.x = 0
# pdf.y = 0
# pdf.multi_cell(150, 5, SHORT_TEXT, border = 1, padding = (5, 5, 5, 5))
run_comparison(pdf,"multicell_with_padding",tmp_path)

show(pdf)

# pdf.output(HERE / "multicell_with_padding.pdf")
#
# import subprocess
# subprocess.Popen('explorer "' + str(HERE / "multicell_with_padding.pdf") + '"' )


def test_multicell_with_padding_check_input():
def test_multicell_with_padding_check_input(tmp_path):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", size=16)
Expand All @@ -103,7 +105,7 @@ def test_multicell_with_padding_check_input():



def test_multicell_return_value():
def test_multicell_return_value(tmp_path):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", size=16)
Expand Down Expand Up @@ -170,11 +172,7 @@ def test_multicell_return_value():

# assert pdf.y == old_y + height_with_padding

pdf.output(HERE / "table_with_padding.pdf")

import subprocess

subprocess.Popen('explorer "' + str(HERE / "table_with_padding.pdf") + '"')
run_comparison(pdf, "table_with_padding.pdf",tmp_path)



Expand Down Expand Up @@ -203,9 +201,8 @@ def test_table_with_multiline_cells_and_images_padding_and_pagebreak(tmp_path):
row.cell(img=datum, img_fill_width=False, style=deathstyle)
else:
row.cell(datum)
# assert_pdf_equal(pdf, HERE / "table_with_multiline_cells_and_images.pdf", tmp_path)
show(pdf)

run_comparison(pdf, "table_with_multiline_cells_and_images_padding_and_pagebreak",tmp_path)

def test_table_with_only_images(tmp_path):
pdf = FPDF()
Expand All @@ -224,7 +221,7 @@ def test_table_with_only_images(tmp_path):
for datum in data_row:
row.cell(img=datum)

show(pdf)
run_comparison(pdf, "table_with_only_images", tmp_path)


def test_table_vertical_alignment(tmp_path):
Expand All @@ -249,7 +246,7 @@ def test_table_vertical_alignment(tmp_path):
else:
row.cell(datum)

show(pdf)
run_comparison(pdf, "table_vertical_alignment", tmp_path)


def test_padding_per_cell(tmp_path):
Expand All @@ -272,8 +269,8 @@ def test_padding_per_cell(tmp_path):
)
else:
row.cell(datum)
# assert_pdf_equal(pdf, HERE / "table_simple.pdf", tmp_path)
show(pdf)

run_comparison(pdf, "table_padding_per_cell", tmp_path)


def test_valign_per_cell(tmp_path):
Expand Down Expand Up @@ -306,17 +303,8 @@ def test_valign_per_cell(tmp_path):
else:
row.cell(datum)

# assert_pdf_equal(pdf, HERE / "table_simple.pdf", tmp_path)
show(pdf)
run_comparison(pdf, "table_valign_per_cell",tmp_path)

def test_table_with_gutter_REMOVE_DOUBLE(tmp_path):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", size=16)
with pdf.table(TABLE_DATA, gutter_height=3, gutter_width=3):
pass

show(pdf)

def test_table_with_gutter_and_padding_and_outer_border_width(tmp_path):
pdf = FPDF()
Expand All @@ -325,7 +313,7 @@ def test_table_with_gutter_and_padding_and_outer_border_width(tmp_path):
with pdf.table(TABLE_DATA, gutter_height=3, gutter_width=5, padding = 0, line_height = pdf.font_size, align="L", outer_border_width=2):
pass

show(pdf)
run_comparison(pdf, "table_with_gutter_and_padding_and_outer_border_width",tmp_path)


def test_table_with_colspan(tmp_path):
Expand Down Expand Up @@ -376,7 +364,7 @@ def make_text(n):
row.cell(
txt)

show(pdf)
run_comparison(pdf, "table_with_colspan",tmp_path)



Expand All @@ -393,19 +381,8 @@ def test_outside_border_width(tmp_path):
for icol in range(5):
datum = "Circus"
row.cell(datum)
# assert_pdf_equal(pdf, HERE / "table_simple.pdf", tmp_path)

show(pdf)
#
#
# def test_draw_box():
# pdf = FPDF()
# pdf.add_page()
# pdf.set_font("Times", size=16)
#
# pdf._draw_box(0, 0, 40, 20, 1, False)
#
# show(pdf)
run_comparison(pdf, "table_with_outside_border_width",tmp_path)


def test_table_colspan_and_padding(tmp_path):
Expand Down Expand Up @@ -451,10 +428,7 @@ def test_table_colspan_and_padding(tmp_path):
row.cell("B3")
row.cell("B4")

show(pdf)

# filename = HERE / "table_colspan_padding.pdf"
# assert_pdf_equal(pdf, HERE / "table_colspan_padding.pdf", tmp_path)
run_comparison(pdf, "table_colspan_and_padding",tmp_path)


def test_table_colspan_and_padding_and_gutter(tmp_path):
Expand All @@ -479,7 +453,7 @@ def test_table_colspan_and_padding_and_gutter(tmp_path):
row.cell("B4")


show(pdf)
run_comparison(pdf, "table_colspan_and_padding_and_gutter",tmp_path)

def test_table_colspan_and_padding_and_gutter_and_width(tmp_path):
pdf = FPDF()
Expand All @@ -502,7 +476,7 @@ def test_table_colspan_and_padding_and_gutter_and_width(tmp_path):
row.cell("B4")


show(pdf)
run_comparison(pdf, "table_colspan_and_padding_and_gutter_and_width",tmp_path)

def test_table_with_cell_overflow(tmp_path):
pdf = FPDF()
Expand All @@ -529,40 +503,20 @@ def test_table_with_cell_overflow(tmp_path):
row.cell("B2")
row.cell("B3")

show(pdf)
# assert_pdf_equal(pdf, HERE / "table_with_cell_overflow.pdf", tmp_path)

TABLE_DATA_OLD = (
("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_table_with_varying_col_widths(tmp_path):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", size=16)
with pdf.table(col_widths=(30, 30, 10, 30)) as table:
for data_row in TABLE_DATA_OLD:
row = table.row()
for datum in data_row:
row.cell(datum)
assert_pdf_equal(pdf, HERE / "table_with_varying_col_widths.pdf", tmp_path)
run_comparison(pdf, "table_with_cell_overflow_font_setting",tmp_path)



def test_draw_box(tmp_path):
def test_draw_box_borders(tmp_path):
pdf = FPDF()
pdf.set_font("Times", size=16)
pdf.add_page()

from fpdf.table import draw_box
from fpdf.table import draw_box_borders

def box(x,y, borders):
draw_box(pdf, x-10, y-10, x+40, y+20, fill=(200,200,200), border="None")
draw_box(pdf, x-20,y-20,x+50, y+30,border=borders)
draw_box_borders(pdf, x - 10, y - 10, x + 40, y + 20, fill=(200, 200, 200), border="None")
draw_box_borders(pdf, x - 20, y - 20, x + 50, y + 30, border=borders)
pdf.set_xy(x,y)
pdf.cell(txt=borders)

Expand All @@ -572,4 +526,4 @@ def box(x,y, borders):
box(40, 140, "T")
box(140, 140, "B")

show(pdf)
run_comparison(pdf, "draw_box_borders",tmp_path)

0 comments on commit ad6401b

Please sign in to comment.