Skip to content

Commit

Permalink
Using .local_context() in .write_html() to better isolate HTML render…
Browse files Browse the repository at this point in the history
…ing & styling
  • Loading branch information
Lucas-C committed Jun 17, 2024
1 parent 5239fea commit 9752952
Show file tree
Hide file tree
Showing 63 changed files with 12 additions and 15 deletions.
3 changes: 2 additions & 1 deletion fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,8 @@ def write_html(self, text, *args, **kwargs):
tag_styles (dict): mapping of HTML tag names to colors
"""
html2pdf = self.HTML2FPDF_CLASS(self, *args, **kwargs)
html2pdf.feed(text)
with self.local_context():
html2pdf.feed(text)

def _set_min_pdf_version(self, version):
self.pdf_version = max(self.pdf_version, version)
Expand Down
13 changes: 3 additions & 10 deletions fpdf/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,6 @@ def __init__(
self.emphasis = dict(b=False, i=False, u=False)
self.font_size = pdf.font_size_pt
self.set_font(pdf.font_family or "times", size=self.font_size, set_default=True)
self._prev_font = (pdf.font_family, self.font_size, self.emphasis)
self.pdf._push_local_stack() # xpylint: disable=protected-access

self._pre_formatted = False # preserve whitespace while True.
self._pre_started = (
Expand Down Expand Up @@ -464,11 +462,10 @@ def _end_paragraph(self):
self.align = ""
if self._paragraph:
self._column.end_paragraph()
our_context = (
self.pdf._pop_local_stack() # pylint: disable=protected-access
)
# pylint: disable=protected-access
our_context = self.pdf._pop_local_stack()
self._column.render()
self.pdf._push_local_stack(our_context) # pylint: disable=protected-access
self.pdf._push_local_stack(our_context)
self._paragraph = None
self.follows_trailing_space = True

Expand Down Expand Up @@ -991,10 +988,6 @@ def feed(self, data):
while self._tags_stack and self._tags_stack[-1] in self.HTML_UNCLOSED_TAGS:
self._tags_stack.pop()
self._end_paragraph() # render the final chunk of text and clean up our local context.
self.pdf._pop_local_stack() # pylint: disable=protected-access
if self._prev_font[0]: # restore previously defined font settings
self.emphasis = self._prev_font[2]
self.set_font(self._prev_font[0], size=self._prev_font[1], set_default=True)
if self._tags_stack and self.warn_on_tags_not_matching:
LOGGER.warning("Missing HTML end tag for <%s>", self._tags_stack[-1])

Expand Down
11 changes: 7 additions & 4 deletions scripts/compare-changed-pdfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# Generate a HTML page that makes it easy to visually compare all PDF files
# that are modified in the current branch, compared to the master branch.

# USAGE: ./compare-changed-pdfs.py
# USAGE: ./compare-changed-pdfs.py [test_subdir_path]

import webbrowser
import sys, webbrowser
from functools import partial
from http.server import HTTPServer, SimpleHTTPRequestHandler
from os import makedirs, scandir
Expand All @@ -30,15 +30,18 @@ def scantree_dirs(path):
yield from scantree_dirs(entry.path)


target_dir = sys.argv[1] if len(sys.argv) > 1 else "test"
print(f"Processing all PDF reference files in {target_dir}")

stdout = check_output("git diff --name-status master", shell=True)
changed_pdf_files = [
line[1:].strip()
for line in stdout.decode("utf-8").splitlines()
if line.startswith("M\ttest/")
if line.startswith(f"M\t{target_dir}")
]

TMP_DIR.mkdir(exist_ok=True)
for dir in scantree_dirs(REPO_DIR / "test"):
for dir in scantree_dirs(REPO_DIR / target_dir):
(TMP_DIR / dir).mkdir(exist_ok=True)
for changed_pdf_file in changed_pdf_files:
command = f"git show master:{changed_pdf_file} > {TMP_DIR}/{changed_pdf_file}"
Expand Down
Binary file modified test/html/html_align_paragraph.pdf
Binary file not shown.
Binary file modified test/html/html_blockquote_color.pdf
Binary file not shown.
Binary file modified test/html/html_blockquote_indent.pdf
Binary file not shown.
Binary file modified test/html/html_bold_italic_underline.pdf
Binary file not shown.
Binary file modified test/html/html_custom_heading_sizes.pdf
Binary file not shown.
Binary file modified test/html/html_custom_line_height.pdf
Binary file not shown.
Binary file modified test/html/html_custom_pre_code_font.pdf
Binary file not shown.
Binary file modified test/html/html_customize_ul.pdf
Binary file not shown.
Binary file modified test/html/html_description.pdf
Binary file not shown.
Binary file modified test/html/html_features.pdf
Binary file not shown.
Binary file modified test/html/html_font_color_name.pdf
Binary file not shown.
Binary file modified test/html/html_format_within_p.pdf
Binary file not shown.
Binary file modified test/html/html_heading_color_attribute.pdf
Binary file not shown.
Binary file modified test/html/html_heading_hebrew.pdf
Binary file not shown.
Binary file modified test/html/html_headings_color.pdf
Binary file not shown.
Binary file modified test/html/html_headings_line_height.pdf
Binary file not shown.
Binary file modified test/html/html_images.pdf
Binary file not shown.
Binary file modified test/html/html_img_not_overlapping.pdf
Binary file not shown.
Binary file modified test/html/html_li_prefix_color.pdf
Binary file not shown.
Binary file modified test/html/html_li_tag_indent.pdf
Binary file not shown.
Binary file modified test/html/html_link_color.pdf
Binary file not shown.
Binary file modified test/html/html_list_vertical_margin.pdf
Binary file not shown.
Binary file modified test/html/html_ln_outside_p.pdf
Binary file not shown.
Binary file modified test/html/html_long_list_entries.pdf
Binary file not shown.
Binary file modified test/html/html_long_ol_bullets.pdf
Binary file not shown.
Binary file modified test/html/html_measurement_units.pdf
Binary file not shown.
Binary file modified test/html/html_ol_start_and_type.pdf
Binary file not shown.
Binary file modified test/html/html_ol_ul_line_height.pdf
Binary file not shown.
Binary file modified test/html/html_preserve_initial_text_color.pdf
Binary file not shown.
Binary file modified test/html/html_superscript.pdf
Binary file not shown.
Binary file modified test/html/html_table_honoring_align.pdf
Binary file not shown.
Binary file modified test/html/html_table_line_separators.pdf
Binary file not shown.
Binary file modified test/html/html_table_simple.pdf
Binary file not shown.
Binary file modified test/html/html_table_th_inside_tr_issue_137.pdf
Binary file not shown.
Binary file modified test/html/html_table_with_bgcolor.pdf
Binary file not shown.
Binary file modified test/html/html_table_with_border.pdf
Binary file not shown.
Binary file modified test/html/html_table_with_data_that_contains_entity_names.pdf
Binary file not shown.
Binary file modified test/html/html_table_with_empty_cell_contents.pdf
Binary file not shown.
Binary file modified test/html/html_table_with_font_tags_used_to_set_text_color.pdf
Binary file not shown.
Binary file modified test/html/html_table_with_img.pdf
Binary file not shown.
Binary file modified test/html/html_table_with_img_without_explicit_dimensions.pdf
Binary file not shown.
Binary file modified test/html/html_table_with_imgs_captions_and_colspan.pdf
Binary file not shown.
Binary file modified test/html/html_table_with_multi_lines_text.pdf
Binary file not shown.
Binary file not shown.
Binary file modified test/html/html_table_with_only_tds.pdf
Binary file not shown.
Binary file modified test/html/html_table_with_width_and_align.pdf
Binary file not shown.
Binary file modified test/html/html_ul_type.pdf
Binary file not shown.
Binary file modified test/html/html_unorthodox_headings_hierarchy.pdf
Binary file not shown.
Binary file modified test/html/html_whitespace_handling.pdf
Binary file not shown.
Binary file modified test/html/issue_156.pdf
Binary file not shown.
Binary file modified test/hyperlinks.pdf
Binary file not shown.
Binary file modified test/internal_links.pdf
Binary file not shown.
Binary file modified test/link_to_other_document.pdf
Binary file not shown.
Binary file modified test/outline/custom_HTML2FPDF.pdf
Binary file not shown.
Binary file modified test/outline/html_toc.pdf
Binary file not shown.
Binary file modified test/outline/html_toc_2_pages.pdf
Binary file not shown.
Binary file modified test/outline/html_toc_with_h1_as_2nd_heading.pdf
Binary file not shown.
Binary file modified test/table/table_vertical_alignment.pdf
Binary file not shown.
Binary file modified test/table/table_with_rowspan.pdf
Binary file not shown.
Binary file modified test/table/table_with_rowspan_and_colspan.pdf
Binary file not shown.

0 comments on commit 9752952

Please sign in to comment.