File: test_html_table.py

package info (click to toggle)
fpdf2 2.8.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 114,352 kB
  • sloc: python: 50,410; sh: 133; makefile: 12
file content (392 lines) | stat: -rw-r--r-- 12,257 bytes parent folder | download
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
from pathlib import Path

import pytest

from fpdf import FPDF, FPDFException, TextStyle
from test.conftest import assert_pdf_equal
from test.table.test_table import MULTILINE_TABLE_DATA

HERE = Path(__file__).resolve().parent
ROOT = HERE.parent.parent


def test_html_table_simple(tmp_path):
    pdf = FPDF()
    pdf.set_font_size(30)
    pdf.add_page()
    pdf.write_html("""<table><thead><tr>
        <th width="25%">left</th><th width="50%">center</th><th width="25%">right</th>
    </tr></thead><tbody><tr>
        <td>1</td><td>2</td><td>3</td>
    </tr><tr>
        <td>4</td><td>5</td><td>6</td>
    </tr></tbody></table>""")
    assert_pdf_equal(pdf, HERE / "html_table_simple.pdf", tmp_path)


def test_html_table_line_separators(tmp_path):
    pdf = FPDF()
    pdf.set_font_size(30)
    pdf.add_page()
    pdf.write_html(
        """<table><thead><tr>
        <th width="25%">left</th><th width="50%">center</th><th width="25%">right</th>
    </tr></thead><tbody><tr>
        <td>1</td><td>2</td><td>3</td>
    </tr><tr>
        <td>4</td><td>5</td><td>6</td>
    </tr></tbody></table>""",
        table_line_separators=True,
    )
    assert_pdf_equal(pdf, HERE / "html_table_line_separators.pdf", tmp_path)


def test_html_table_th_inside_tr_issue_137(tmp_path):
    pdf = FPDF()
    pdf.add_page()
    pdf.write_html("""<table border="1">
    <tr>
        <th width="40%">header1</th>
        <th width="60%">header2</th>
    </tr>
    <tr>
        <th>value1</th>
        <td>value2</td>
    </tr>
</table>""")
    assert_pdf_equal(pdf, HERE / "html_table_th_inside_tr_issue_137.pdf", tmp_path)


def test_html_table_with_border(tmp_path):
    pdf = FPDF()
    pdf.set_font_size(30)
    pdf.add_page()
    pdf.write_html("""<table border="1"><thead><tr>
        <th width="25%">left</th><th width="50%">center</th><th width="25%">right</th>
    </tr></thead><tbody><tr>
        <td>1</td><td>2</td><td>3</td>
    </tr><tr>
        <td>4</td><td>5</td><td>6</td>
    </tr></tbody></table>""")
    assert_pdf_equal(pdf, HERE / "html_table_with_border.pdf", tmp_path)


def test_html_table_with_img(caplog, tmp_path):
    pdf = FPDF()
    pdf.add_page()
    pdf.write_html("""<table>
        <tr>
            <td width="50%">
                <img src="{}/test/image/png_images/affc57dfffa5ec448a0795738d456018.png" height="235" width="435"/>
            </td>
            <td width="50%">
                <img src="{}/test/image/image_types/insert_images_insert_png.png" height="162" width="154"/>
            </td>
        </tr>
    </table>""".replace("{}", str(ROOT)))
    assert_pdf_equal(pdf, HERE / "html_table_with_img.pdf", tmp_path)
    assert 'Ignoring unsupported "width" / "height" set on <img> element' in caplog.text


def test_html_table_with_img_without_explicit_dimensions(tmp_path):
    pdf = FPDF()
    pdf.add_page()
    pdf.write_html("""<table>
        <tr>
            <td width="50%">
                <img src="{}/test/image/png_images/affc57dfffa5ec448a0795738d456018.png"/>
            </td>
            <td width="50%">
                <img src="{}/test/image/image_types/insert_images_insert_png.png"/>
            </td>
        </tr>
    </table>""".replace("{}", str(ROOT)))
    assert_pdf_equal(
        pdf,
        HERE / "html_table_with_img_without_explicit_dimensions.pdf",
        tmp_path,
    )


def test_html_table_with_imgs_captions_and_colspan(caplog, tmp_path):
    pdf = FPDF()
    pdf.add_page()
    pdf.write_html("""<table border="0">
        <tr>
            <td colspan="2" align="center"><b>Side by side centered pictures and captions</b></td>
        </tr>
        <tr>
            <td width="50%" align="center"><img src="{}/docs/fpdf2-logo.png"/></td>
            <td width="50%" align="center"><img src="{}/docs/fpdf2-logo.png"/></td>
        </tr>
        <tr>
            <td width="50%" align="center">left caption</td>
            <td width="50%" align="center">right caption</td>
        </tr>
    </table>""".replace("{}", str(ROOT)))
    assert_pdf_equal(
        pdf, HERE / "html_table_with_imgs_captions_and_colspan.pdf", tmp_path
    )
    assert (
        'Ignoring width="50%" specified on a <td> that is not in the first <tr>'
        in caplog.text
    )


def test_html_table_with_empty_cell_contents(tmp_path):  # issue 349
    pdf = FPDF()
    pdf.set_font_size(30)
    pdf.add_page()
    # Reference table cells positions:
    pdf.write_html("""<table><thead><tr>
        <th width="25%">left</th><th width="50%">center</th><th width="25%">right</th>
    </tr></thead><tbody><tr>
        <td>1</td><td>2</td><td>3</td>
    </tr><tr>
        <td>4</td><td>5</td><td>6</td>
    </tr></tbody></table>""")
    # Table with empty cells:
    pdf.write_html("""<table><thead><tr>
        <th width="25%">left</th><th width="50%">center</th><th width="25%">right</th>
    </tr></thead><tbody><tr>
        <td>1</td><td></td><td>3</td>
    </tr><tr>
        <td></td><td>5</td><td></td>
    </tr></tbody></table>""")
    assert_pdf_equal(pdf, HERE / "html_table_with_empty_cell_contents.pdf", tmp_path)


def test_html_table_with_bgcolor(tmp_path):  # issue-512
    pdf = FPDF()
    pdf.add_page()
    pdf.write_html("""<table>
    <thead>
        <tr>
            <th width="25%">Career</th>
            <th width="75%">Quote</th>
        </tr>
    </thead>
    <tbody>
        <tr bgcolor="grey"><td>Engineer</td><td>The engineer has been, and is, a maker of history.</td></tr>
        <tr bgcolor="white"><td>Developer</td><td>Logical thinking, passion and perseverance is the paint on your palette.</td></tr>
        <tr bgcolor="grey"><td>Analyst</td><td>Seeing what other people can't see gives you great vision.</td></tr>
        <tr bgcolor="white"><td><i>None of the above</i></td><td>I'm sorry. We could not find a quote for your job.</td></tr>
    </tbody>
</table>""")
    assert_pdf_equal(pdf, HERE / "html_table_with_bgcolor.pdf", tmp_path)


def test_html_table_with_only_tds(tmp_path):  # issue-740
    pdf = FPDF()
    pdf.set_font_size(30)
    pdf.add_page()
    pdf.write_html("""<table><tr>
        <td>left</td><td>center</td><td>right</td>
    </tr><tr>
        <td>1</td><td>2</td><td>3</td>
    </tr><tr>
        <td>4</td><td>5</td><td>6</td>
    </tr></table>""")
    assert_pdf_equal(pdf, HERE / "html_table_with_only_tds.pdf", tmp_path)


def test_html_table_with_multi_lines_text(tmp_path):  # issue-91
    pdf = FPDF()
    pdf.set_font_size(30)
    pdf.add_page()
    pdf.write_html("""<table border="1"><thead><tr>
    <th width="30%">First name</th><th width="30%">Last name</th><th width="15%">Age</th><th width="25%">City</th>
</tr></thead><tbody><tr>
    <td>Jean Abdul William</td><td>Smith</td><td>34</td><td>San Juan</td>
</tr></tbody></table>""")
    assert_pdf_equal(pdf, HERE / "html_table_with_multi_lines_text.pdf", tmp_path)


def test_html_table_with_multiline_cells_and_split_over_page(tmp_path):
    pdf = FPDF()
    pdf.add_page()
    pdf.set_font("Times", size=16)
    html = "<table><thead><tr>"
    # pylint: disable=consider-using-join
    for cell_text in MULTILINE_TABLE_DATA[0]:
        html += f"\n<th>{cell_text}</th>"
    html += "\n</tr></thead><tbody>"
    for data_row in MULTILINE_TABLE_DATA[1:-1] + MULTILINE_TABLE_DATA[1:]:
        html += "\n<tr>"
        for cell_text in data_row:
            html += f"\n<td>{cell_text}</td>"
        html += "</tr>"
    html += "\n</tbody></table>"
    pdf.write_html(html)
    assert_pdf_equal(
        pdf, HERE / "html_table_with_multiline_cells_and_split_over_page.pdf", tmp_path
    )


def test_html_table_with_width_and_align(tmp_path):
    pdf = FPDF()
    pdf.set_font_size(24)
    pdf.add_page()
    pdf.write_html("""<table width=50% align=right><thead><tr>
        <th width="25%">left</th><th width="50%">center</th><th width="25%">right</th>
    </tr></thead><tbody><tr>
        <td>1</td><td>2</td><td>3</td>
    </tr><tr>
        <td>4</td><td>5</td><td>6</td>
    </tr></tbody></table>""")
    assert_pdf_equal(pdf, HERE / "html_table_with_width_and_align.pdf", tmp_path)


def test_html_table_invalid(caplog):
    pdf = FPDF()
    pdf.set_font_size(30)
    pdf.add_page()
    # OK with empty tables:
    pdf.write_html("<table></table>")
    pdf.write_html("<table><tr></tr></table>")
    pdf.write_html("<table><tr><td></td></tr></table>")
    # KO if some elements are missing:
    with pytest.raises(FPDFException) as error:
        pdf.write_html("<table><td></td></table>")
    assert str(error.value) == "Invalid HTML: <td> used outside any <tr>"
    with pytest.raises(FPDFException) as error:
        pdf.write_html("<table><th></th></table>")
    assert str(error.value) == "Invalid HTML: <th> used outside any <tr>"
    with pytest.raises(FPDFException) as error:
        pdf.write_html("<tr></tr>")
    assert str(error.value) == "Invalid HTML: <tr> used outside any <table>"
    assert caplog.text == ""


def test_html_table_with_nested_tags():  # issue 845
    pdf = FPDF()
    pdf.set_font_size(24)
    pdf.add_page()
    with pytest.raises(NotImplementedError):
        pdf.write_html("""<table><tr>
            <th>LEFT</th>
            <th>RIGHT</th>
        </tr><tr>
            <td><font size=7>This is supported</font></td>
            <td>This <font size=20>is not</font> <b>supported</b></td>
        </tr></table>""")


def test_html_table_with_font_tags_used_to_set_text_color(tmp_path):  # issue 1013
    pdf = FPDF()
    pdf.add_page()
    pdf.set_font("Helvetica", size=8)
    pdf.write_html("""<table>
    <thead>
        <tr bgcolor="#711C45">
            <th width="25%"><font color="#711C45">Mark</font></th>
        </tr>
    </thead>
    <tbody>
        <tr bgcolor="#E4E4E4">
            <td align="center"><font color="#E4E4E4">less than 2</font></td>
        </tr>
    </tbody>
    </table>""")
    assert_pdf_equal(
        pdf,
        HERE / "html_table_with_font_tags_used_to_set_text_color.pdf",
        tmp_path,
    )


def test_html_table_with_data_that_contains_entity_names(tmp_path):  # issue 1010
    pdf = FPDF()
    pdf.add_page()
    pdf.set_font("Helvetica", size=8)
    pdf.write_html("""<table>
    <thead>
        <tr>
            <th>Value</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Pi &lt; 22 &divide; 7</td>
        </tr>
    </tbody>
    </table>""")
    assert_pdf_equal(
        pdf,
        HERE / "html_table_with_data_that_contains_entity_names.pdf",
        tmp_path,
    )


def test_html_table_honoring_align(tmp_path):
    pdf = FPDF()
    pdf.add_page()
    pdf.write_html("""<table>
          <tr>
            <th align="right">Foo</th>
            <th align="center">Bar</th>
            <th align="left">Baz</th>
          </tr>
          <tr>
            <td align="right">Foo</td>
            <td align="center">Bar</td>
            <td align="left">Baz</td>
          </tr>
        </table>""")
    assert_pdf_equal(pdf, HERE / "html_table_honoring_align.pdf", tmp_path)


def test_html_table_with_null_text_in_span_cell(tmp_path):
    pdf = FPDF()
    pdf.add_page()
    pdf.write_html(
        """<table border="1">
        <tr>
            <td rowspan=2></td>
            <td>cell 2</td>
            <td>cell 3</td>
        </tr>
        <tr>
            <td>cell 4</td>
            <td>cell 5</td>
        </tr>
        <tr>
            <td colspan=2></td>
            <td>cell 7</td>
        </tr>
    </table>
        """,
        table_line_separators=True,
    )
    assert_pdf_equal(
        pdf,
        HERE / "html_table_with_null_text_in_span_cell.pdf",
        tmp_path,
    )


def test_html_table_inside_paragraph(tmp_path):  # issue 1453
    pdf = FPDF()
    pdf.add_page()
    pdf.write_html(
        """
<h1>This is a &lt;h1&gt;</h1>
<p>This is a &lt;p&gt;</p>
Table outside paragraph:
<table>
 <tr><th>th1</th><th>th2</th></tr>
 <tr align="C"><td>td1</td><td>td2</td></tr>
</table>
<p>
  Table inside paragraph:
  <table>
   <tr><th>th1</th><th>th2</th></tr>
   <tr align="C"><td>td1</td><td>td2</td></tr>
  </table>
</p>""",
        tag_styles={
            "h1": TextStyle(font_size_pt=32),
            "p": TextStyle(font_size_pt=16),
        },
    )
    assert_pdf_equal(pdf, HERE / "html_table_inside_paragraph.pdf", tmp_path)