From 347b03f8a66abf3689ad5b72706edead857a96a2 Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Mon, 7 Jul 2014 11:48:55 +0530 Subject: [PATCH] added pdf, fixes frappe/erpnext#863 --- frappe/boot.py | 2 -- .../doctype/communication/communication.py | 23 +++++++++++-------- .../outgoing_email_settings.json | 10 +------- frappe/utils/email_lib/email_body.py | 8 +++++++ requirements.txt | 1 + 5 files changed, 23 insertions(+), 21 deletions(-) diff --git a/frappe/boot.py b/frappe/boot.py index 5fd03b7ad1..dca136c49c 100644 --- a/frappe/boot.py +++ b/frappe/boot.py @@ -24,8 +24,6 @@ def get_bootinfo(): # system info bootinfo['sysdefaults'] = frappe.defaults.get_defaults() bootinfo['server_date'] = frappe.utils.nowdate() - bootinfo["send_print_in_body_and_attachment"] = frappe.db.get_value("Outgoing Email Settings", - None, "send_print_in_body_and_attachment") if frappe.session['user'] != 'Guest': bootinfo['user_info'] = get_fullnames() diff --git a/frappe/core/doctype/communication/communication.py b/frappe/core/doctype/communication/communication.py index a4740affb8..eabd4e006f 100644 --- a/frappe/core/doctype/communication/communication.py +++ b/frappe/core/doctype/communication/communication.py @@ -118,21 +118,24 @@ def send_comm_email(d, name, sent_via=None, print_html=None, attachments='[]', s if hasattr(sent_via, "get_content"): d.content = sent_via.get_content(d) - footer = set_portal_link(sent_via, d) - - send_print_in_body = frappe.db.get_value("Outgoing Email Settings", None, "send_print_in_body_and_attachment") - if print_html and not send_print_in_body: - d.content += "

Please see attachment for document details.

" + footer = "
" + set_portal_link(sent_via, d) + footer += "

" + _("Please see attachment") + "

" mail = get_email(d.recipients, sender=d.sender, subject=d.subject, - msg=d.content, footer=footer, print_html=print_html if send_print_in_body else None) + msg=d.content, footer=footer) if send_me_a_copy: mail.cc.append(frappe.db.get_value("User", frappe.session.user, "email")) if print_html: print_html = scrub_urls(print_html) - mail.add_attachment(name.replace(' ','').replace('/','-') + '.html', print_html) + + try: + mail.add_pdf_attachment(name.replace(' ','').replace('/','-') + '.pdf', print_html) + except Exception: + frappe.msgprint(_("Error generating PDF, attachment sent as HTML")) + mail.add_attachment(name.replace(' ','').replace('/','-') + '.html', + print_html, 'text/html') for a in json.loads(attachments): try: @@ -145,7 +148,7 @@ def send_comm_email(d, name, sent_via=None, print_html=None, attachments='[]', s def set_portal_link(sent_via, comm): """set portal link in footer""" - footer = None + footer = "" if is_signup_enabled() and hasattr(sent_via, "get_portal_page"): portal_page = sent_via.get_portal_page() @@ -154,7 +157,7 @@ def set_portal_link(sent_via, comm): sent_via.get("contact_email")) in comm.recipients if is_valid_recipient: url = "%s/%s?name=%s" % (get_url(), portal_page, urllib.quote(sent_via.name)) - footer = """
- View this on our website""" % url + footer = """ +

View this on our website

""" % url return footer diff --git a/frappe/core/doctype/outgoing_email_settings/outgoing_email_settings.json b/frappe/core/doctype/outgoing_email_settings/outgoing_email_settings.json index bcc856c851..4d3fcb7f62 100644 --- a/frappe/core/doctype/outgoing_email_settings/outgoing_email_settings.json +++ b/frappe/core/doctype/outgoing_email_settings/outgoing_email_settings.json @@ -74,14 +74,6 @@ "label": "Auto Email Id", "permlevel": 0 }, - { - "default": "1", - "description": "If checked, an email with an attached HTML format will be added to part of the EMail body as well as attachment. To only send as attachment, uncheck this.", - "fieldname": "send_print_in_body_and_attachment", - "fieldtype": "Check", - "label": "Send Print in Body and Attachment", - "permlevel": 0 - }, { "fieldname": "section_break_10", "fieldtype": "Section Break", @@ -100,7 +92,7 @@ "idx": 1, "in_create": 1, "issingle": 1, - "modified": "2014-07-04 04:19:14.935886", + "modified": "2014-07-07 11:42:10.128767", "modified_by": "Administrator", "module": "Core", "name": "Outgoing Email Settings", diff --git a/frappe/utils/email_lib/email_body.py b/frappe/utils/email_lib/email_body.py index b226930cc9..649c0773af 100644 --- a/frappe/utils/email_lib/email_body.py +++ b/frappe/utils/email_lib/email_body.py @@ -146,6 +146,14 @@ class EMail: self.msg_root.attach(part) + def add_pdf_attachment(self, name, html): + import pdfkit, os + fname = frappe.generate_hash() + ".pdf" + pdfkit.from_string(html, fname) + with open(fname, "rb") as fileobj: + self.add_attachment(name, fileobj.read(), 'application/octet-stream') + os.remove(fname) + def validate(self): """validate the email ids""" from frappe.utils import validate_email_add diff --git a/requirements.txt b/requirements.txt index c309d80ebd..f3fb7545b1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -21,3 +21,4 @@ requests==1.2.3 celery redis selenium +pdfkit