diff --git a/frappe/utils/print_utils.py b/frappe/utils/print_utils.py index 9c5e903f3c..e2a4aabbd8 100644 --- a/frappe/utils/print_utils.py +++ b/frappe/utils/print_utils.py @@ -121,7 +121,8 @@ def attach_print( from frappe.utils.pdf import get_pdf print_settings = frappe.db.get_singles_dict("Print Settings") - + if print_letterhead and not letterhead: + letterhead = frappe.get_cached_value("Letter Head", {"is_default": 1}, "name") kwargs = dict( print_format=print_format, style=style, @@ -133,16 +134,29 @@ def attach_print( frappe.local.flags.ignore_print_permissions = True + is_weasyprint_print_format = False + if print_format and print_format != "Standard": + print_format_doc = frappe.get_cached_doc("Print Format", print_format) + is_weasyprint_print_format = not ( + print_format_doc.custom_format or print_format_doc.get("print_designer_print_format") + ) + with print_language(lang or frappe.local.lang): content = "" if cint(print_settings.send_print_as_pdf): ext = ".pdf" - kwargs["as_pdf"] = True - content = ( - get_pdf(html, options={"password": password} if password else None) - if html - else get_print(doctype, name, **kwargs) - ) + if html: + content = get_pdf(html, options={"password": password} if password else None) + elif is_weasyprint_print_format: + from frappe.utils.weasyprint import PrintFormatGenerator + + doc_obj = doc or frappe.get_cached_doc(doctype, name) + letterhead_name = letterhead if print_letterhead else None + generator = PrintFormatGenerator(print_format, doc_obj, letterhead_name) + content = generator.render_pdf() + else: + kwargs["as_pdf"] = True + content = get_print(doctype, name, **kwargs) else: ext = ".html" content = html or scrub_urls(get_print(doctype, name, **kwargs)).encode("utf-8")