Skip to content

Commit

Permalink
feat: hooks added for print formats / pdf. (#20734)
Browse files Browse the repository at this point in the history
  • Loading branch information
maharshivpatel committed Apr 17, 2023
1 parent 50505e1 commit 1bae6a2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
5 changes: 5 additions & 0 deletions frappe/hooks.py
Expand Up @@ -83,6 +83,11 @@
"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 = {
Expand Down
45 changes: 34 additions & 11 deletions frappe/utils/pdf.py
Expand Up @@ -23,6 +23,31 @@
]


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)
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions frappe/www/printview.py
Expand Up @@ -208,8 +208,10 @@ def get_template_from_string():
"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
Expand Down

0 comments on commit 1bae6a2

Please sign in to comment.