1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
|
import gzip
from pathlib import Path
import pytest
from fpdf import FPDF, FPDFException
from test.conftest import (
assert_pdf_equal,
assert_same_file,
ensure_exec_time_below,
ensure_rss_memory_below,
LOREM_IPSUM,
)
HERE = Path(__file__).resolve().parent
FONTS_DIR = HERE.parent / "fonts"
with gzip.open(HERE / "long_text.txt.gz") as text_file:
LONG_TEXT_LINES = text_file.read().decode(encoding="utf-8").splitlines()
TEXT_SIZE, SPACING = 36, 1.15
LINE_HEIGHT = TEXT_SIZE * SPACING
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_ln_positioning_and_page_breaking_for_cell(tmp_path):
doc = FPDF(format="letter", unit="pt")
doc.add_page()
doc.set_font("helvetica", size=TEXT_SIZE)
text = LOREM_IPSUM * 100
for i in range(20):
doc.cell(
w=72,
h=TEXT_SIZE * 1.5,
border=1,
new_x="LEFT",
new_y="NEXT",
text=text[i * 100 : i * 100 + 99],
)
assert_pdf_equal(
doc, HERE / "ln_positioning_and_page_breaking_for_cell.pdf", tmp_path
)
def test_cell_ln_0(tmp_path):
doc = FPDF()
doc.add_page()
doc.set_font("helvetica", size=TEXT_SIZE)
doc.cell(w=45, h=LINE_HEIGHT, border=1, text="Lorem")
doc.cell(w=45, h=LINE_HEIGHT, border=1, text="ipsum")
doc.cell(w=45, h=LINE_HEIGHT, border=1, text="Ut")
doc.cell(w=45, h=LINE_HEIGHT, border=1, text="nostrud")
assert_pdf_equal(doc, HERE / "ln_0.pdf", tmp_path)
def test_cell_ln_1(tmp_path):
"""
Catch DeprecationWarning for ln=1.
Validating that: "Using ln=1 is equivalent to using ln=0 and calling the `ln` method just after."
"""
doc = FPDF()
doc.add_page()
doc.set_font("helvetica", size=TEXT_SIZE)
with pytest.warns(DeprecationWarning) as record:
doc.cell(w=100, h=LINE_HEIGHT, border=1, text="Lorem ipsum", ln=1)
assert len(record) == 1
assert_same_file(record[0].filename, __file__)
doc.cell(w=100, h=LINE_HEIGHT, border=1, text="Ut nostrud irure")
assert_pdf_equal(doc, HERE / "ln_1.pdf", tmp_path)
doc = FPDF()
doc.add_page()
doc.set_font("helvetica", size=TEXT_SIZE)
doc.cell(w=100, h=LINE_HEIGHT, border=1, text="Lorem ipsum")
doc.ln()
doc.cell(w=100, h=LINE_HEIGHT, border=1, text="Ut nostrud irure")
assert_pdf_equal(doc, HERE / "ln_1.pdf", tmp_path)
def test_cell_table_with_pagebreak(tmp_path):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", size=16)
line_height = pdf.font_size * 2
col_width = pdf.epw / 4 # distribute content evenly
for i in range(4): # repeat table 4 times
for row in TABLE_DATA:
for datum in row:
pdf.cell(col_width, line_height, f"{datum} ({i})", border=1)
pdf.ln(line_height)
pdf.ln(line_height * 2)
assert_pdf_equal(pdf, HERE / "cell_table_with_pagebreak.pdf", tmp_path)
def test_cell_table_unbreakable(tmp_path):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", size=16)
line_height = pdf.font_size * 2
col_width = pdf.epw / 4 # distribute content evenly
for i in range(5): # repeat table 5 times
with pdf.unbreakable() as doc:
for row in TABLE_DATA:
for datum in row:
doc.cell(col_width, line_height, f"{datum} ({i})", border=1)
doc.ln(line_height)
pdf.ln(line_height * 2)
assert_pdf_equal(pdf, HERE / "cell_table_unbreakable.pdf", tmp_path)
def test_cell_without_font_set():
pdf = FPDF()
pdf.add_page()
with pytest.raises(FPDFException) as error:
pdf.cell(text="Hello World!")
expected_msg = "No font set, you need to call set_font() beforehand"
assert str(error.value) == expected_msg
def test_cell_without_w_nor_h(tmp_path):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", size=16)
pdf.cell(text="Lorem ipsum", border=1)
pdf.set_font_size(80)
pdf.cell(text="Lorem ipsum", border=1)
assert_pdf_equal(pdf, HERE / "cell_without_w_nor_h.pdf", tmp_path)
def test_cell_missing_text_or_width():
pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", size=16)
with pytest.raises(ValueError) as error:
pdf.cell()
assert (
str(error.value)
== "'text_line' must have fragments if 'text_line.text_width' is None"
)
def test_cell_centering(tmp_path):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", size=60)
pdf.cell(text="Lorem ipsum", border=1, center=True)
assert_pdf_equal(pdf, HERE / "cell_centering.pdf", tmp_path)
def test_cell_centering_and_align_x(tmp_path):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", size=16)
pdf.cell(text="Lorem ipsum", border=1, center=True, align="X")
pdf.set_draw_color(r=0, g=255, b=0)
pdf.line(pdf.w / 2, 0, pdf.w / 2, pdf.h)
assert_pdf_equal(pdf, HERE / "cell_centering_and_align_x.pdf", tmp_path)
def test_cell_markdown(tmp_path):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", size=60)
pdf.cell(text="**Lorem** __Ipsum__ --dolor--", markdown=True)
assert_pdf_equal(pdf, HERE / "cell_markdown.pdf", tmp_path)
def test_cell_markdown_escaped(tmp_path):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", size=40)
pdf.cell(text="**Lo\\rem** \\__Ipsum\\__ \\\\--dolor\\\\--", markdown=True)
assert_pdf_equal(pdf, HERE / "cell_markdown_escaped.pdf", tmp_path)
def test_cell_markdown_bold_italic(tmp_path):
# issue 1094
pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", size=60)
pdf.cell(text="**__Lorem --Ipsum--__**", markdown=True)
assert_pdf_equal(pdf, HERE / "cell_markdown_bold_italic.pdf", tmp_path)
def test_cell_markdown_bold_italic_escaped(tmp_path):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", size=40)
pdf.cell(text="**__Lorem \\--Ipsum\\--__**", markdown=True)
assert_pdf_equal(pdf, HERE / "cell_markdown_bold_italic_escaped.pdf", tmp_path)
@pytest.mark.skip(reason="Font related tests are failing with the fonts available in Debian")
def test_cell_markdown_with_ttf_fonts(tmp_path):
pdf = FPDF()
pdf.add_page()
pdf.add_font("Roboto", "", FONTS_DIR / "Roboto-Regular.ttf")
pdf.add_font("Roboto", "B", FONTS_DIR / "Roboto-Bold.ttf")
pdf.add_font("Roboto", "I", FONTS_DIR / "Roboto-Italic.ttf")
pdf.set_font("Roboto", size=60)
pdf.cell(text="**Lorem** __Ipsum__ --dolor--", markdown=True)
assert_pdf_equal(pdf, HERE / "cell_markdown_with_ttf_fonts.pdf", tmp_path)
def test_cell_markdown_with_ttf_fonts_escaped(tmp_path):
pdf = FPDF()
pdf.add_page()
pdf.add_font("Roboto", "", FONTS_DIR / "Roboto-Regular.ttf")
pdf.add_font("Roboto", "B", FONTS_DIR / "Roboto-Bold.ttf")
pdf.add_font("Roboto", "I", FONTS_DIR / "Roboto-Italic.ttf")
pdf.set_font("Roboto", size=40)
pdf.cell(text="**Lo\\rem** \\__Ipsum\\__ \\\\--dolor\\\\--", markdown=True)
assert_pdf_equal(pdf, HERE / "cell_markdown_with_ttf_fonts_escaped.pdf", tmp_path)
def test_cell_markdown_missing_ttf_font():
pdf = FPDF()
pdf.add_page()
pdf.add_font(fname=FONTS_DIR / "Roboto-Regular.ttf")
pdf.set_font("Roboto-Regular", size=60)
with pytest.raises(FPDFException) as error:
pdf.cell(text="**Lorem Ipsum**", markdown=True)
expected_msg = "Undefined font: roboto-regularB - Use built-in fonts or FPDF.add_font() beforehand"
assert str(error.value) == expected_msg
def test_cell_markdown_bleeding(tmp_path): # issue 241
pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", size=60)
pdf.cell(text="**Lorem Ipsum dolor**", markdown=True, new_x="LMARGIN", new_y="NEXT")
assert pdf.font_style == ""
pdf.cell(text="No Markdown", markdown=False, new_x="LMARGIN", new_y="NEXT")
pdf.cell(text="--Lorem Ipsum dolor--", markdown=True, new_x="LMARGIN", new_y="NEXT")
assert pdf.font_style == ""
pdf.cell(text="No Markdown", markdown=False, new_x="LMARGIN", new_y="NEXT")
pdf.cell(text="__Lorem Ipsum dolor__", markdown=True, new_x="LMARGIN", new_y="NEXT")
assert pdf.font_style == ""
pdf.cell(text="No Markdown", markdown=False, new_x="LMARGIN", new_y="NEXT")
assert_pdf_equal(pdf, HERE / "cell_markdown_bleeding.pdf", tmp_path)
@pytest.mark.skip(reason="Font related tests are failing with the fonts available in Debian")
def test_cell_markdown_right_aligned(tmp_path): # issue 333
pdf = FPDF()
pdf.add_page()
pdf.add_font("Roboto", fname=FONTS_DIR / "Roboto-Regular.ttf")
pdf.add_font("Roboto", style="B", fname=FONTS_DIR / "Roboto-Bold.ttf")
pdf.set_font("Roboto", size=60)
pdf.cell(
0,
9,
"**X** **X** **X** **X** **X** **X** **X** **X** **X**",
markdown=True,
align="R",
)
assert_pdf_equal(pdf, HERE / "cell_markdown_right_aligned.pdf", tmp_path)
def test_table_with_headers_on_every_page(tmp_path):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", size=16)
line_height = pdf.font_size * 2
col_width = pdf.epw / 4 # distribute content evenly
def render_table_header():
pdf.set_font(style="B") # enabling bold text
for col_name in TABLE_DATA[0]:
pdf.cell(col_width, line_height, col_name, border=1)
pdf.ln(line_height)
pdf.set_font(style="") # disabling bold text
render_table_header()
for _ in range(10): # repeat data rows
for row in TABLE_DATA[1:]:
if pdf.will_page_break(line_height):
render_table_header()
for datum in row:
pdf.cell(col_width, line_height, datum, border=1)
pdf.ln(line_height)
assert_pdf_equal(pdf, HERE / "table_with_headers_on_every_page.pdf", tmp_path)
def test_cell_newpos_badinput():
pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", size=16)
with pytest.raises(ValueError):
pdf.cell(w=0, ln=5)
with pytest.raises(TypeError):
pdf.cell(w=0, new_x=5)
with pytest.raises(TypeError):
pdf.cell(w=0, new_y=None)
with pytest.raises(ValueError):
pdf.cell(w=0, align="J")
@pytest.mark.skip(reason="Font related tests are failing with the fonts available in Debian")
def test_cell_curfont_leak(tmp_path): # issue #475
pdf = FPDF()
pdf.add_page()
pdf.add_font("Roboto", fname=FONTS_DIR / "Roboto-Regular.ttf")
pdf.add_font("Roboto", style="B", fname=FONTS_DIR / "Roboto-Bold.ttf")
with pdf.local_context():
pdf.set_font("Roboto", style="B", size=10)
pdf.cell(text="ABCDEFGH", new_x="LEFT", new_y="NEXT")
pdf.set_font("Roboto", size=10)
pdf.cell(text="IJKLMNOP", new_x="LEFT", new_y="NEXT")
with pdf.local_context():
pdf.set_font("Roboto", style="B", size=10)
pdf.cell(text="QRSTUVW", new_x="LEFT", new_y="NEXT")
pdf.set_font("Roboto", size=10)
pdf.cell(text="XYZ012abc,-", new_x="LEFT", new_y="NEXT")
pdf.cell(text="3,7E-05", new_x="LEFT", new_y="NEXT")
assert_pdf_equal(pdf, HERE / "cell_curfont_leak.pdf", tmp_path)
# pylint: disable=protected-access
def test_cell_lasth(tmp_path): # issue #601
pdf = FPDF()
pdf.add_page()
pdf.set_font("helvetica", size=18)
pdf.set_fill_color(255, 255, 0)
pdf.cell(w=100, text="Hello world", fill=True)
pdf.ln()
assert pdf._lasth == 6.35, f"pdf._lasth ({pdf._lasth}) != 5.35"
pdf.set_fill_color(255, 0, 255)
pdf.cell(w=100, text="Hello world", h=50, fill=True)
pdf.ln()
assert pdf._lasth == 50, f"pdf._lasth ({pdf._lasth}) != 50 after cell(h=50)"
pdf.set_fill_color(0, 255, 255)
pdf.cell(w=100, text="Hello world", fill=True)
pdf.cell(w=100, text="")
pdf.ln()
assert pdf._lasth == 6.35, f"pdf._lasth ({pdf._lasth}) != 5.35 after empty cell"
pdf.cell(w=100, text="Hello world", border=True)
assert_pdf_equal(pdf, HERE / "cell_lasth.pdf", tmp_path)
def test_cell_deprecated_txt_arg():
pdf = FPDF()
pdf.add_page()
pdf.set_font("Helvetica", size=TEXT_SIZE)
with pytest.warns(
DeprecationWarning, match='The parameter "txt" has been renamed to "text"'
):
# pylint: disable=unexpected-keyword-arg
pdf.cell(txt="Lorem ipsum Ut nostrud irure")
@ensure_exec_time_below(seconds=18)
@ensure_rss_memory_below(mib=1)
def test_cell_speed_with_long_text(): # issue #907
pdf = FPDF()
pdf.add_font(fname=FONTS_DIR / "DejaVuSans.ttf")
pdf.set_font("DejaVuSans", size=5)
pdf.add_page()
assert len(LONG_TEXT_LINES) == 26862
for line in LONG_TEXT_LINES:
pdf.cell(0, 3, line, new_x="LMARGIN", new_y="NEXT")
|