From bcca1da8d841e985cbe304cf9cbb99e73475bbe4 Mon Sep 17 00:00:00 2001 From: marination Date: Mon, 29 Jan 2024 13:25:45 +0100 Subject: [PATCH] fix: Extract header/footer html into `content` to simplify things - Extract header/footer html into `content` first, then remove any instances from the main content since it is already rendered via the extracted header/footer html --- frappe/utils/pdf.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/frappe/utils/pdf.py b/frappe/utils/pdf.py index 321c65a73c..be25b8cd48 100644 --- a/frappe/utils/pdf.py +++ b/frappe/utils/pdf.py @@ -238,7 +238,13 @@ def prepare_header_footer(soup: BeautifulSoup): # extract header and footer for html_id in ("header-html", "footer-html"): - if content := soup.find(id=html_id): + if content := soup.find(id=html_id).extract(): + # `header/footer-html` are extracted, rendered as html + # and passed in wkhtmltopdf options (as '--header/footer-html') + # Remove instances of them from main content for render_template + for tag in soup.find_all(id=html_id): + tag.extract() + toggle_visible_pdf(content) id_map = {"header-html": "pdf_header_html", "footer-html": "pdf_footer_html"} hook_func = frappe.get_hooks(id_map.get(html_id)) @@ -251,10 +257,6 @@ def prepare_header_footer(soup: BeautifulSoup): css=css, ) - # there could be multiple instances of header-html/footer-html - for tag in soup.find_all(id=html_id): - tag.extract() - # create temp file fname = os.path.join("/tmp", f"frappe-pdf-{frappe.generate_hash()}.html") with open(fname, "wb") as f: