From 68cf5941a30460520596011829eb866b2e28de21 Mon Sep 17 00:00:00 2001 From: Lucas Cimon <925560+Lucas-C@users.noreply.github.com> Date: Mon, 17 Jun 2024 15:33:00 +0200 Subject: [PATCH] Fixed FPDF.local_context() style leak during page breaks - Fix #1204 (#1207) --- CHANGELOG.md | 10 +- fpdf/fpdf.py | 100 ++++++++++++++---- fpdf/graphics_state.py | 5 +- fpdf/html.py | 41 +++---- fpdf/text_region.py | 2 +- scripts/compare-changed-pdfs.py | 11 +- test/embed_file_all_optionals.pdf | Bin 1459 -> 1566 bytes test/embed_file_self.pdf | Bin 1492 -> 1683 bytes test/file_attachment_annotation.pdf | Bin 1664 -> 1855 bytes test/html/html_align_paragraph.pdf | Bin 1685 -> 1691 bytes test/html/html_blockquote_color.pdf | Bin 1030 -> 1032 bytes test/html/html_blockquote_indent.pdf | Bin 1349 -> 1357 bytes test/html/html_bold_italic_underline.pdf | Bin 1462 -> 1464 bytes test/html/html_custom_heading_sizes.pdf | Bin 2024 -> 2027 bytes test/html/html_custom_line_height.pdf | Bin 1011 -> 1017 bytes test/html/html_custom_pre_code_font.pdf | Bin 8068 -> 8072 bytes test/html/html_customize_ul.pdf | Bin 1298 -> 1299 bytes test/html/html_description.pdf | Bin 1160 -> 1166 bytes test/html/html_features.pdf | Bin 6243 -> 6323 bytes test/html/html_font_color_name.pdf | Bin 1393 -> 1397 bytes test/html/html_format_within_p.pdf | Bin 1432 -> 1435 bytes test/html/html_heading_color_attribute.pdf | Bin 1421 -> 1424 bytes test/html/html_heading_hebrew.pdf | Bin 8993 -> 8997 bytes test/html/html_headings_color.pdf | Bin 1371 -> 1376 bytes test/html/html_headings_line_height.pdf | Bin 2821 -> 2825 bytes test/html/html_images.pdf | Bin 2295 -> 2299 bytes test/html/html_img_not_overlapping.pdf | Bin 11198 -> 11203 bytes test/html/html_li_prefix_color.pdf | Bin 1108 -> 1113 bytes test/html/html_li_tag_indent.pdf | Bin 1076 -> 1078 bytes test/html/html_link_color.pdf | Bin 1139 -> 1141 bytes test/html/html_list_vertical_margin.pdf | Bin 2766 -> 2783 bytes test/html/html_ln_outside_p.pdf | Bin 1054 -> 1056 bytes test/html/html_long_list_entries.pdf | Bin 1040 -> 1043 bytes test/html/html_long_ol_bullets.pdf | Bin 1213 -> 1218 bytes test/html/html_measurement_units.pdf | Bin 1303 -> 1306 bytes test/html/html_ol_start_and_type.pdf | Bin 1042 -> 1045 bytes test/html/html_ol_ul_line_height.pdf | Bin 1255 -> 1261 bytes .../html/html_preserve_initial_text_color.pdf | Bin 1012 -> 1017 bytes test/html/html_superscript.pdf | Bin 1315 -> 1317 bytes test/html/html_table_honoring_align.pdf | Bin 1201 -> 1205 bytes test/html/html_table_line_separators.pdf | Bin 1254 -> 1261 bytes test/html/html_table_simple.pdf | Bin 1224 -> 1228 bytes .../html_table_th_inside_tr_issue_137.pdf | Bin 1229 -> 1233 bytes test/html/html_table_with_bgcolor.pdf | Bin 1654 -> 1657 bytes test/html/html_table_with_border.pdf | Bin 1354 -> 1359 bytes ...e_with_data_that_contains_entity_names.pdf | Bin 1162 -> 1165 bytes .../html_table_with_empty_cell_contents.pdf | Bin 1281 -> 1286 bytes ..._with_font_tags_used_to_set_text_color.pdf | Bin 1212 -> 1217 bytes test/html/html_table_with_img.pdf | Bin 14743 -> 14748 bytes ...e_with_img_without_explicit_dimensions.pdf | Bin 14743 -> 14748 bytes ...l_table_with_imgs_captions_and_colspan.pdf | Bin 15233 -> 15236 bytes .../html/html_table_with_multi_lines_text.pdf | Bin 1366 -> 1370 bytes ...th_multiline_cells_and_split_over_page.pdf | Bin 3159 -> 3183 bytes test/html/html_table_with_only_tds.pdf | Bin 1038 -> 1043 bytes test/html/html_table_with_width_and_align.pdf | Bin 1229 -> 1236 bytes test/html/html_ul_type.pdf | Bin 8837 -> 8842 bytes .../html_unorthodox_headings_hierarchy.pdf | Bin 1337 -> 1341 bytes test/html/html_whitespace_handling.pdf | Bin 1857 -> 1864 bytes test/html/issue_156.pdf | Bin 12326 -> 12330 bytes test/html/test_html.py | 20 ++-- test/hyperlinks.pdf | Bin 1543 -> 1546 bytes test/internal_links.pdf | Bin 2196 -> 2200 bytes test/link_to_other_document.pdf | Bin 1805 -> 1808 bytes test/outline/html_toc.pdf | Bin 4296 -> 4312 bytes test/outline/html_toc_2_pages.pdf | Bin 20861 -> 20920 bytes ...pdf => html_toc_with_custom_rendering.pdf} | Bin 2461 -> 2476 bytes .../html_toc_with_h1_as_2nd_heading.pdf | Bin 2838 -> 2850 bytes test/outline/test_outline_html.py | 4 +- test/requirements.txt | 1 + test/table/table_vertical_alignment.pdf | Bin 2722 -> 2723 bytes test/table/table_with_rowspan.pdf | Bin 2253 -> 2260 bytes test/table/table_with_rowspan_and_colspan.pdf | Bin 2986 -> 2991 bytes test/test_graphics_context.py | 3 - ...der_footer_and_local_context_font_size.pdf | Bin 0 -> 1546 bytes test/text/header_footer_and_use_font_face.pdf | Bin 0 -> 1577 bytes test/text/test_cell.py | 21 ++++ test/text/test_use_font_face.py | 28 +++++ 77 files changed, 177 insertions(+), 69 deletions(-) rename test/outline/{custom_HTML2FPDF.pdf => html_toc_with_custom_rendering.pdf} (72%) create mode 100644 test/text/header_footer_and_local_context_font_size.pdf create mode 100644 test/text/header_footer_and_use_font_face.pdf create mode 100644 test/text/test_use_font_face.py diff --git a/CHANGELOG.md b/CHANGELOG.md index f7a768c95..de0f49290 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,18 +24,20 @@ This can also be enabled programmatically with `warnings.simplefilter('default', * feature to identify the Unicode script of the input text and break it into fragments when different scripts are used, improving text shaping results * [`FPDF.image()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.image): now handles `keep_aspect_ratio` in combination with an enum value provided to `x` * file names are mentioned in errors when `fpdf2` fails to parse a SVG image -* * feature to adjust spacing before lists via the `HTML2FPDF.list_vertical_margin` attribute +* [`FPDF.write_html()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.write_html): spacing before lists can now be adjusted via the `HTML2FPDF.list_vertical_margin` attribute ### Fixed +* [`FPDF.local_context()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.local_context) used to leak styling during page breaks, when rendering `footer()` & `header()` * [`fpdf.drawing.DeviceCMYK`](https://py-pdf.github.io/fpdf2/fpdf/drawing.html#fpdf.drawing.DeviceCMYK) objects can now be passed to [`FPDF.set_draw_color()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.set_draw_color), [`FPDF.set_fill_color()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.set_fill_color) and [`FPDF.set_text_color()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.set_text_color) without raising a `ValueError`: [documentation](https://py-pdf.github.io/fpdf2/Text.html#text-formatting). +* [`FPDF.write_html()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.write_html): fixing rendering of `
` tags, that do not trigger a page break anymore * individual `/Resources` directories are now properly created for each document page. This change ensures better compliance with the PDF specification but results in a slight increase in the size of PDF documents. You can still use the old behavior by setting `FPDF().single_resources_object = True` * line size calculation for fragments when text shaping is used * fixed incoherent indentation of long list entries - _cf._ [issue #1073](https://github.com/py-pdf/fpdf2/issues/1073) * default values for `top_margin` and `bottom_margin` in `HTML2FPDF._new_paragraph()` calls are now correctly converted into chosen document units. +### Removed +* an obscure and undocumented [feature](https://github.com/py-pdf/fpdf2/issues/1198) of [`FPDF.write_html()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.write_html), which used to magically pass local variables as arguments. ### Changed -* Removed an obscure and undocumented [feature](https://github.com/py-pdf/fpdf2/issues/1198) of [`FPDF.write_html()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.write_html), which used to magically pass local variables as arguments. * [`FPDF.table()`](https://py-pdf.github.io/fpdf2/Tables.html) now raises an error when a single row is too high to be rendered on a single page -* `HTML2FPDF.tag_indents` can now be non-integer. Indentation of HTML elements is now independent of font size and bullet strings. -* No spacing controlled by `HTML2FPDF.list_vertical_margin` is created for nested HTML `
  • ` elements in contrast with prior respect for `Paragraph.top_margin` when handling `Paragraph`s created when handling `