From f41e48c669ded48b7909f6c31a5b30768aa37daa Mon Sep 17 00:00:00 2001 From: kardmode Date: Sun, 28 Feb 2016 16:13:29 +0400 Subject: [PATCH 1/4] Multi Doc PDF Compile all html into one html --- frappe/templates/pages/print.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/frappe/templates/pages/print.py b/frappe/templates/pages/print.py index 35184a5fa6..42d1c62ce6 100644 --- a/frappe/templates/pages/print.py +++ b/frappe/templates/pages/print.py @@ -143,6 +143,38 @@ def get_html_and_style(doc, name=None, print_format=None, meta=None, "style": get_print_style(print_format=print_format) } +@frappe.whitelist() +def download_multi_pdf(doctype, name, format=None): + # name can include names of many docs of the same doctype. + totalhtml = "" + # Pagebreak to be added between each doc html + pagebreak = """

""" + + options = {} + + import json + result = json.loads(name) + # Get html of each doc and combine including page breaks + for i, ss in enumerate(result): + html = frappe.get_print(doctype, ss, format) + if i == len(result)-1: + totalhtml = totalhtml + html + else: + totalhtml = totalhtml + html + pagebreak + + + + frappe.local.response.filename = "{doctype}.pdf".format(doctype=doctype.replace(" ", "-").replace("/", "-")) + + + # Title of pdf + options.update({ + 'title': doctype, + }) + + frappe.local.response.filecontent = get_pdf(totalhtml,options) + frappe.local.response.type = "download" + @frappe.whitelist() def download_pdf(doctype, name, format=None): html = frappe.get_print(doctype, name, format) From 3679689d716e7b45ccc79b00c4d7a447c8ccc278 Mon Sep 17 00:00:00 2001 From: kardmode Date: Sun, 28 Feb 2016 16:15:18 +0400 Subject: [PATCH 2/4] [fix] for html parser for multi doc printing BeautifulSoup parser "html5lib" doesn't work well with multi doc printing --- frappe/utils/pdf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frappe/utils/pdf.py b/frappe/utils/pdf.py index 8c33022191..f465ca3979 100644 --- a/frappe/utils/pdf.py +++ b/frappe/utils/pdf.py @@ -72,7 +72,7 @@ def prepare_options(html, options): def read_options_from_html(html): options = {} - soup = BeautifulSoup(html, "html5lib") + soup = BeautifulSoup(html, "html.parser") # extract pdfkit options from html for html_id in ("margin-top", "margin-bottom", "margin-left", "margin-right", "page-size"): From 37bfc82824eeab6c1edcf848c5a4aece74c5b47f Mon Sep 17 00:00:00 2001 From: kardmode Date: Sun, 28 Feb 2016 17:34:18 +0400 Subject: [PATCH 3/4] Add Print Button to list view Print button in list view works with multi doc printing. Uses standard print format. --- frappe/public/js/frappe/list/doclistview.js | 25 +++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/frappe/public/js/frappe/list/doclistview.js b/frappe/public/js/frappe/list/doclistview.js index e304172a2d..8e449027e8 100644 --- a/frappe/public/js/frappe/list/doclistview.js +++ b/frappe/public/js/frappe/list/doclistview.js @@ -452,6 +452,31 @@ frappe.views.DocListView = frappe.ui.Listing.extend({ frappe.msgprint(__("Select records for assignment")) } }, true) + + //bulk assignment + me.page.add_menu_item(__("Print"), function(){ + + docname = []; + + $.each(me.get_checked_items(), function(i, doc){ + docname.push(doc.name); + }) + + if(docname.length >= 1){ + var json_string = JSON.stringify(docname); + var w = window.open("/api/method/frappe.templates.pages.print.download_multi_pdf?" + +"doctype="+encodeURIComponent(me.doctype) + +"&name="+encodeURIComponent(json_string) + +"&format="+encodeURIComponent("Standard") + +"&no_letterhead="+encodeURIComponent('0')); + if(!w) { + msgprint(__("Please enable pop-ups")); return; + } + } + else{ + frappe.msgprint(__("Select records for assignment")) + } + }, true) }, init_like: function() { From a69be832c48f2a16091a4906610112be89692730 Mon Sep 17 00:00:00 2001 From: kardmode Date: Sun, 28 Feb 2016 19:47:48 +0400 Subject: [PATCH 4/4] Print PDF from list view with dialog Using a dialog to allow custom settings of print format and print letterhead. --- frappe/public/js/frappe/list/doclistview.js | 41 ++++++++++++++++----- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/frappe/public/js/frappe/list/doclistview.js b/frappe/public/js/frappe/list/doclistview.js index 8e449027e8..a20682a008 100644 --- a/frappe/public/js/frappe/list/doclistview.js +++ b/frappe/public/js/frappe/list/doclistview.js @@ -463,15 +463,38 @@ frappe.views.DocListView = frappe.ui.Listing.extend({ }) if(docname.length >= 1){ - var json_string = JSON.stringify(docname); - var w = window.open("/api/method/frappe.templates.pages.print.download_multi_pdf?" - +"doctype="+encodeURIComponent(me.doctype) - +"&name="+encodeURIComponent(json_string) - +"&format="+encodeURIComponent("Standard") - +"&no_letterhead="+encodeURIComponent('0')); - if(!w) { - msgprint(__("Please enable pop-ups")); return; - } + + var dialog = new frappe.ui.Dialog({ + title: "Print Documents", + fields: [ + {"fieldtype": "Check", "label": __("Print Letterhead"), "fieldname": "print_letterhead"}, + {"fieldtype": "Select", "label": __("Print Format"), "fieldname": "print_sel"}, + + {"fieldtype": "Button", "label": __("Print"), "fieldname": "print"}, + ] + }); + + print_formats = frappe.meta.get_print_formats(me.doctype); + dialog.fields_dict.print_sel.$input.empty().add_options(print_formats); + + dialog.fields_dict.print.$input.click(function() { + args = dialog.get_values(); + if(!args) return; + var default_print_format = locals.DocType[me.doctype].default_print_format; + with_letterhead = args.print_letterhead ? 1 : 0; + print_format = args.print_sel ? args.print_sel:default_print_format; + + var json_string = JSON.stringify(docname); + var w = window.open("/api/method/frappe.templates.pages.print.download_multi_pdf?" + +"doctype="+encodeURIComponent(me.doctype) + +"&name="+encodeURIComponent(json_string) + +"&format="+encodeURIComponent(print_format) + +"&no_letterhead="+(with_letterhead ? "0" : "1")); + if(!w) { + msgprint(__("Please enable pop-ups")); return; + } + }); + dialog.show(); } else{ frappe.msgprint(__("Select records for assignment"))