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
|
from pathlib import Path
import fpdf
from test.conftest import assert_pdf_equal
import pytest
HERE = Path(__file__).resolve().parent
FONTS_DIR = HERE.parent / "fonts"
def test_multi_cell_markdown(tmp_path):
pdf = fpdf.FPDF()
pdf.add_page()
pdf.set_font("Times", size=32)
text = ( # Some text where styling occur over line breaks:
"Lorem ipsum dolor amet, **consectetur adipiscing** elit,"
" sed do eiusmod __tempor incididunt__ ut labore et dolore --magna aliqua--."
)
pdf.multi_cell(
w=pdf.epw, text=text, markdown=True
) # This is tricky to get working well
pdf.ln()
pdf.multi_cell(w=pdf.epw, text=text, markdown=True, align="L")
assert_pdf_equal(pdf, HERE / "multi_cell_markdown.pdf", tmp_path)
def test_multi_cell_markdown_strikethrough(tmp_path):
pdf = fpdf.FPDF()
pdf.add_page()
pdf.set_font("Times", size=32)
pdf.multi_cell(w=pdf.epw, text="~~strikethrough~~", markdown=True)
assert_pdf_equal(pdf, HERE / "multi_cell_markdown_strikethrough.pdf", tmp_path)
def test_multi_cell_markdown_escaped(tmp_path):
pdf = fpdf.FPDF()
pdf.add_page()
pdf.set_font("Times", size=32)
text = ( # Some text where styling occur over line breaks:
"Lorem ipsum \\ dolor amet, \\**consectetur adipiscing\\** elit,"
" sed do eiusmod \\\\__tempor incididunt\\\\__ ut labore et dolore --magna aliqua--."
)
pdf.multi_cell(
w=pdf.epw, text=text, markdown=True
) # This is tricky to get working well
pdf.ln()
pdf.multi_cell(w=pdf.epw, text=text, markdown=True, align="L")
assert_pdf_equal(pdf, HERE / "multi_cell_markdown_escaped.pdf", tmp_path)
@pytest.mark.skip(reason="Font related tests are failing with the fonts available in Debian")
def test_multi_cell_markdown_with_ttf_fonts(tmp_path):
pdf = fpdf.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=32)
text = ( # Some text where styling occur over line breaks:
"Lorem ipsum dolor, **consectetur adipiscing** elit,"
" eiusmod __tempor incididunt__ ut labore et dolore --magna aliqua--."
)
pdf.multi_cell(
w=pdf.epw, text=text, markdown=True
) # This is tricky to get working well
pdf.ln()
pdf.multi_cell(w=pdf.epw, text=text, markdown=True, align="L")
assert_pdf_equal(pdf, HERE / "multi_cell_markdown_with_ttf_fonts.pdf", tmp_path)
def test_multi_cell_markdown_with_ttf_fonts_escaped(tmp_path):
pdf = fpdf.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=32)
text = ( # Some text where styling occur over line breaks:
"Lorem ipsum \\ dolor, \\**consectetur adipiscing\\** elit,"
" eiusmod \\\\__tempor incididunt\\\\__ ut labore et dolore --magna aliqua--."
)
pdf.multi_cell(
w=pdf.epw, text=text, markdown=True
) # This is tricky to get working well
pdf.ln()
pdf.multi_cell(w=pdf.epw, text=text, markdown=True, align="L")
assert_pdf_equal(
pdf, HERE / "multi_cell_markdown_with_ttf_fonts_escaped.pdf", tmp_path
)
def test_multi_cell_markdown_missing_ttf_font():
pdf = fpdf.FPDF()
pdf.add_page()
pdf.add_font(fname=FONTS_DIR / "Roboto-Regular.ttf")
pdf.set_font("Roboto-Regular", size=60)
with pytest.raises(fpdf.FPDFException) as error:
pdf.multi_cell(w=pdf.epw, 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_multi_cell_markdown_with_fill_color(tmp_path): # issue 348
pdf = fpdf.FPDF()
pdf.add_page()
pdf.set_font("Times", size=10)
pdf.set_fill_color(255, 0, 0)
pdf.multi_cell(
50, markdown=True, text="aa bb cc **dd ee dd ee dd ee dd ee dd ee dd ee**"
)
assert_pdf_equal(pdf, HERE / "multi_cell_markdown_with_fill_color.pdf", tmp_path)
def test_multi_cell_markdown_justified(tmp_path): # issue 327
pdf = fpdf.FPDF()
pdf.add_page()
for font in ("Helvetica", "Courier"):
pdf.set_font(family=font, size=12)
pdf.set_y(pdf.y + 3)
pdf.multi_cell(
190,
markdown=True,
align="J",
text=(
"Lorem **ipsum** dolor sit amet, **consectetur** adipiscing elit, "
"sed do eiusmod tempor incididunt ut labore et dolore magna "
"aliqua. Ut enim ad minim veniam, __quis__ nostrud exercitation "
"ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis "
"aute irure dolor in reprehenderit in voluptate velit esse cillum "
"dolore eu fugiat nulla pariatur. Excepteur sint occaecat "
"cupidatat non proident, sunt in culpa qui officia deserunt "
"mollit anim id est laborum."
),
)
pdf.set_x(10)
pdf.set_y(pdf.y + 5)
assert_pdf_equal(pdf, HERE / "multi_cell_markdown_justified.pdf", tmp_path)
def test_multi_cell_markdown_link(tmp_path):
pdf = fpdf.FPDF()
pdf.set_font("Helvetica")
pdf.add_page()
pdf.multi_cell(
pdf.epw,
text="**Start** [One Page Dungeon Context](https://www.dungeoncontest.com/) __End__",
markdown=True,
)
assert_pdf_equal(pdf, HERE / "multi_cell_markdown_link.pdf", tmp_path)
|