From cf0fe8eecdf6704c6fdb3baf4765e488c30b9c79 Mon Sep 17 00:00:00 2001 From: Maharshi Patel Date: Tue, 4 Feb 2025 20:36:44 +0530 Subject: [PATCH] fix: read_file return base64 encoded string - added flag to read_file to return base64 encoded string - fixed pdf_header_footer_chrome to remove unused subst function call - added new_pdf_backend flag to get_print_format_template function --- frappe/__init__.py | 10 ++++++---- .../print_formats/pdf_header_footer_chrome.html | 2 +- frappe/www/printview.py | 4 +++- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/frappe/__init__.py b/frappe/__init__.py index cf8e52c160..625e7aa7c9 100644 --- a/frappe/__init__.py +++ b/frappe/__init__.py @@ -11,6 +11,7 @@ be used to build database driven apps. Read the documentation: https://frappeframework.com/docs """ +import base64 import copy import functools import importlib @@ -1665,14 +1666,15 @@ def get_file_json(path): return json.load(f) -def read_file(path, raise_not_found=False): - """Open a file and return its content as Unicode.""" +def read_file(path, raise_not_found=False, as_base64=False): + """Open a file and return its content as Unicode or Base64 string.""" if isinstance(path, str): path = path.encode("utf-8") if os.path.exists(path): - with open(path) as f: - return as_unicode(f.read()) + with open(path, "rb" if as_base64 else "r") as f: + content = f.read() + return base64.b64encode(content).decode("utf-8") if as_base64 else as_unicode(content) elif raise_not_found: raise OSError(f"{path} Not Found") else: diff --git a/frappe/templates/print_formats/pdf_header_footer_chrome.html b/frappe/templates/print_formats/pdf_header_footer_chrome.html index 8916fad898..de7e6f8016 100644 --- a/frappe/templates/print_formats/pdf_header_footer_chrome.html +++ b/frappe/templates/print_formats/pdf_header_footer_chrome.html @@ -34,7 +34,7 @@ {{ tag | string }} {%- endfor %} - +