From b47d9d57fb2b6b6b915be99676f87f6c736e49ae Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Thu, 11 Apr 2024 15:36:36 +0530 Subject: [PATCH] fix: handle empty style tag (#25910) --- frappe/tests/test_pdf.py | 7 +++++++ frappe/utils/pdf.py | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/frappe/tests/test_pdf.py b/frappe/tests/test_pdf.py index 4ae8e8ff3c..243cd0f7b2 100644 --- a/frappe/tests/test_pdf.py +++ b/frappe/tests/test_pdf.py @@ -63,6 +63,13 @@ class TestPdf(FrappeTestCase): # so it should not be extracted into options self.assertFalse(options.get("margin-right")) + def test_empty_style(self): + html = """ +
Hello
+ """ + _, options = pdfgen.read_options_from_html(html) + self.assertTrue(options) + def test_pdf_encryption(self): password = "qwe" pdf = pdfgen.get_pdf(self.html, options={"password": password}) diff --git a/frappe/utils/pdf.py b/frappe/utils/pdf.py index 7325f87201..a5de5808b9 100644 --- a/frappe/utils/pdf.py +++ b/frappe/utils/pdf.py @@ -17,7 +17,7 @@ from pypdf import PdfReader, PdfWriter import frappe from frappe import _ from frappe.core.doctype.file.utils import find_file_by_url -from frappe.utils import scrub_urls +from frappe.utils import cstr, scrub_urls from frappe.utils.jinja_globals import bundled_asset, is_rtl PDF_CONTENT_ERRORS = [ @@ -242,7 +242,7 @@ def get_print_format_styles(soup: BeautifulSoup) -> list[cssutils.css.Property]: # Prepare a css stylesheet from all the style tags' contents for style_tag in style_tags: - stylesheet += style_tag.string + stylesheet += cstr(style_tag.string) # Use css parser to tokenize the classes and their styles parsed_sheet = cssutils.parseString(stylesheet)