feat: hooks added for print formats / pdf. (#20734)

This commit is contained in:
Maharshi Patel 2023-04-17 19:03:53 +05:30 committed by GitHub
parent 50505e1769
commit 1bae6a2b0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 13 deletions

View file

@ -83,6 +83,11 @@ on_logout = (
"frappe.core.doctype.session_default_settings.session_default_settings.clear_session_defaults"
)
# PDF
pdf_header_html = "frappe.utils.pdf.pdf_header_html"
pdf_body_html = "frappe.utils.pdf.pdf_body_html"
pdf_footer_html = "frappe.utils.pdf.pdf_footer_html"
# permissions
permission_query_conditions = {

View file

@ -23,6 +23,31 @@ PDF_CONTENT_ERRORS = [
]
def pdf_header_html(soup, head, content, styles, html_id, css):
return frappe.render_template(
"templates/print_formats/pdf_header_footer.html",
{
"head": head,
"content": content,
"styles": styles,
"html_id": html_id,
"css": css,
"lang": frappe.local.lang,
"layout_direction": "rtl" if is_rtl() else "ltr",
},
)
def pdf_body_html(template, args, **kwargs):
return template.render(args, filters={"len": len})
def pdf_footer_html(soup, head, content, styles, html_id, css):
return pdf_header_html(
soup=soup, head=head, content=content, styles=styles, html_id=html_id, css=css
)
def get_pdf(html, options=None, output: PdfWriter | None = None):
html = scrub_urls(html)
html, options = prepare_options(html, options)
@ -196,17 +221,15 @@ def prepare_header_footer(soup):
tag.extract()
toggle_visible_pdf(content)
html = frappe.render_template(
"templates/print_formats/pdf_header_footer.html",
{
"head": head,
"content": content,
"styles": styles,
"html_id": html_id,
"css": css,
"lang": frappe.local.lang,
"layout_direction": "rtl" if is_rtl() else "ltr",
},
id_map = {"header-html": "pdf_header_html", "footer-html": "pdf_footer_html"}
hook_func = frappe.get_hooks(id_map.get(html_id))
html = frappe.get_attr(hook_func[-1])(
soup=soup,
head=head,
content=content,
styles=styles,
html_id=html_id,
css=css,
)
# create temp file

View file

@ -208,8 +208,10 @@ def get_rendered_template(
"print_settings": print_settings,
}
)
html = template.render(args, filters={"len": len})
hook_func = frappe.get_hooks("pdf_body_html")
html = frappe.get_attr(hook_func[-1])(
jenv=jenv, template=template, print_format=print_format, args=args
)
if cint(trigger_print):
html += trigger_print_script