fix: code changes as pdf_generator is now select field

This commit is contained in:
Maharshi Patel 2025-02-14 17:04:13 +05:30
parent e818ef22f7
commit 463d1f2b05
6 changed files with 33 additions and 77 deletions

View file

@ -2082,7 +2082,7 @@ def get_print(
password=None,
pdf_options=None,
letterhead=None,
chrome_pdf_generator=None,
pdf_generator: Literal["wkhtmltopdf", "chrome"] | None = None,
):
"""Get Print Format for given document.
@ -2091,21 +2091,23 @@ def get_print(
:param print_format: Print Format name. Default 'Standard',
:param style: Print Format style.
:param as_pdf: Return as PDF. Default False.
:param password: Password to encrypt the pdf with. Default None"""
:param password: Password to encrypt the pdf with. Default None
:param pdf_generator: PDF generator to use. Default 'wkhtmltopdf'
"""
from frappe.utils.pdf import get_pdf
from frappe.website.serve import get_response_without_exception_handling
"""
local.form_dict.chrome_pdf_generator is set from before_request hook (print designer app) for download_pdf endpoint
local.form_dict.pdf_generator is set from before_request hook (print designer app) for download_pdf endpoint
if it is not set (internal function call) then set it
"""
if "chrome_pdf_generator" not in local.form_dict:
if "pdf_generator" not in local.form_dict:
# if arg is passed, use that, else get setting from print format
if chrome_pdf_generator is None:
chrome_pdf_generator = frappe.get_cached_value(
"Print Format", print_format, "chrome_pdf_generator"
if pdf_generator is None:
pdf_generator = (
frappe.get_cached_value("Print Format", print_format, "pdf_generator") or "wkhtmltopdf"
)
local.form_dict.chrome_pdf_generator = bool(cint(chrome_pdf_generator))
local.form_dict.pdf_generator = pdf_generator
original_form_dict = copy.deepcopy(local.form_dict)
try:
@ -2129,16 +2131,24 @@ def get_print(
if not as_pdf:
return html
if local.form_dict.chrome_pdf_generator:
hook_func = frappe.get_hooks("chrome_pdf_generator")
if hook_func:
return frappe.call(
hook_func[-1],
if local.form_dict.pdf_generator != "wkhtmltopdf":
hook_func = frappe.get_hooks("pdf_generator")
for hook in hook_func:
"""
check pdf_generator value in your hook function.
if it matches run and return pdf else return None
"""
pdf = frappe.call(
hook,
print_format=print_format,
html=html,
options=pdf_options,
output=output,
pdf_generator=local.form_dict.pdf_generator,
)
# if hook returns a value, assume it was the correct pdf_generator and return it
if pdf:
return pdf
return get_pdf(html, options=pdf_options, output=output)

View file

@ -22,7 +22,6 @@ class PrintFormat(Document):
absolute_value: DF.Check
align_labels_right: DF.Check
chrome_pdf_generator: DF.Check
css: DF.Code | None
custom_format: DF.Check
default_print_language: DF.Link | None
@ -41,6 +40,7 @@ class PrintFormat(Document):
page_number: DF.Literal[
"Hide", "Top Left", "Top Center", "Top Right", "Bottom Left", "Bottom Center", "Bottom Right"
]
pdf_generator: DF.Literal["wkhtmltopdf"]
print_format_builder: DF.Check
print_format_builder_beta: DF.Check
print_format_type: DF.Literal["Jinja", "JS"]

View file

@ -1,49 +0,0 @@
<!DOCTYPE html>
<html lang={{ lang }} dir={{ layout_direction }}>
<head>
<meta charset="utf-8">
{% for tag in head -%}
{{ tag | string }}
{%- endfor %}
<style>
body {
margin: 0 !important;
border: 0 !important;
/* padding-top: 1mm !important; */
}
.letter-head,
.letter-head-footer {
margin-top: -12mm !important;
}
/* Dont show explicit links for <a> tags */
@media print {
/* padding is added to simulate old wkhtmltopdf format prints */
.wrapper {
box-sizing: border-box;
padding: 17mm 0 1mm !important;
page-break-after: always !important;
}
[document-status] {
margin-bottom: 0 !important;
}
a[href]:after {
content: none;
}
}
</style>
{% for tag in styles -%}
{{ tag | string }}
{%- endfor %}
</head>
<body>
<div class="print-format">
<div class="wrapper">
{% for tag in content -%}
{{ tag | string }}
{%- endfor %}
</div>
</div>
</body>
</html>

View file

@ -33,10 +33,9 @@ PDF_CONTENT_ERRORS = [
]
def pdf_header_html(soup, head, content, styles, html_id, css):
path = "templates/print_formats/pdf_header_footer.html"
if frappe.local.form_dict.get("chrome_pdf_generator", False):
path = "templates/print_formats/pdf_header_footer_chrome.html"
def pdf_header_html(soup, head, content, styles, html_id, css, path=None):
if not path:
path = "templates/print_formats/pdf_header_footer.html"
return frappe.render_template(
path,
{
@ -78,14 +77,9 @@ def _guess_template_error_line_number(template) -> int | None:
return frame.lineno
def pdf_footer_html(soup, head, content, styles, html_id, css):
def pdf_footer_html(soup, head, content, styles, html_id, css, path=None):
return pdf_header_html(
soup=soup,
head=head,
content=content,
styles=styles,
html_id=html_id,
css=css,
soup=soup, head=head, content=content, styles=styles, html_id=html_id, css=css, path=path
)

View file

@ -3,6 +3,7 @@ import json
import os
import uuid
from io import BytesIO
from typing import Literal
from pypdf import PdfWriter
@ -225,7 +226,7 @@ def download_pdf(
no_letterhead=0,
language=None,
letterhead=None,
chrome_pdf_generator=None,
pdf_generator: Literal["wkhtmltopdf", "chrome"] | None = None,
):
doc = doc or frappe.get_doc(doctype, name)
validate_print_permission(doc)
@ -239,7 +240,7 @@ def download_pdf(
as_pdf=True,
letterhead=letterhead,
no_letterhead=no_letterhead,
chrome_pdf_generator=chrome_pdf_generator,
pdf_generator=pdf_generator,
)
frappe.local.response.filename = "{name}.pdf".format(name=name.replace(" ", "-").replace("/", "-"))

View file

@ -99,7 +99,7 @@ def get_context(context) -> PrintContext:
"print_format": getattr(print_format, "name", None),
"letterhead": letterhead,
"no_letterhead": frappe.form_dict.no_letterhead,
"chrome_pdf_generator": frappe.form_dict.get("chrome_pdf_generator", False),
"pdf_generator": frappe.form_dict.get("pdf_generator", "wkhtmltopdf"),
}