diff --git a/label_printing/api.py b/label_printing/api.py index 53f25dd..0aedf40 100644 --- a/label_printing/api.py +++ b/label_printing/api.py @@ -1,20 +1,24 @@ from __future__ import unicode_literals -import frappe, os +import frappe +import os import json from frappe import _ -from frappe.utils.pdf import get_pdf,cleanup +from frappe.utils.pdf import get_pdf, cleanup from frappe.core.doctype.access_log.access_log import make_access_log from PyPDF2 import PdfFileWriter +from frappe.utils.jinja import render_template @frappe.whitelist() def print_label(values): values = json.loads(values) - printer_setting = frappe.db.get_single_value('Label Printer Settings', 'label_printer'), - + printer_setting = frappe.db.get_single_value( + 'Label Printer Settings', 'label_printer'), + print_format = frappe.db.get_single_value( + 'Label Printer Settings', 'label_print_format') for label in values["labels"]: doc = frappe.new_doc('Label') @@ -28,24 +32,27 @@ def print_label(values): doc.customer = values["customer"] if "batch" in values: doc.batch = values["batch"] - if "qty" in label: + if "item_qty" in label: doc.qty = label["item_qty"] if "information" in label: doc.information = label["information"] newdoc = doc.insert() - print_label_by_server("Label", newdoc.name, label["label_qty"], printer_setting[0],"Label", doc=None, no_letterhead=0, file_path=None) + # TODO comment in + # print_label_by_server( + # "Label", newdoc.name, label["label_qty"], printer_setting[0], print_format, doc=None, no_letterhead=0, file_path=None) return 200 - + def print_label_by_server(doctype, name, qty, printer_setting, print_format=None, doc=None, no_letterhead=0, file_path=None): pdf_options = { - 'page-width': '{0}mm'.format(frappe.db.get_single_value('Label Printer Settings', 'label_width')), - 'page-height': '{0}mm'.format(frappe.db.get_single_value('Label Printer Settings', 'label_height')), + 'page-width': '{0}mm'.format(frappe.db.get_single_value('Label Printer Settings', 'label_width')), + 'page-height': '{0}mm'.format(frappe.db.get_single_value('Label Printer Settings', 'label_height')), } - - print_settings = frappe.get_doc("Network Printer Settings", printer_setting) + + print_settings = frappe.get_doc( + "Network Printer Settings", printer_setting) try: import cups except ImportError: @@ -56,17 +63,19 @@ def print_label_by_server(doctype, name, qty, printer_setting, print_format=None cups.setPort(print_settings.port) conn = cups.Connection() output = PdfFileWriter() - output = frappe.get_print(doctype, name, print_format, doc=doc, no_letterhead=no_letterhead, as_pdf = True, output = output, pdf_options=pdf_options) + output = frappe.get_print(doctype, name, print_format, doc=doc, + no_letterhead=no_letterhead, as_pdf=True, output=output, pdf_options=pdf_options) if not file_path: - file_path = os.path.join("/", "tmp", "frappe-pdf-{0}.pdf".format(frappe.generate_hash())) - output.write(open(file_path,"wb")) + file_path = os.path.join( + "/", "tmp", "frappe-pdf-{0}.pdf".format(frappe.generate_hash())) + output.write(open(file_path, "wb")) for _ in range(qty): - conn.printFile(print_settings.printer_name,file_path , name, {}) + conn.printFile(print_settings.printer_name, file_path, name, {}) except IOError as e: if ("ContentNotFoundError" in e.message or "ContentOperationNotPermittedError" in e.message or "UnknownContentError" in e.message - or "RemoteHostClosedError" in e.message): + or "RemoteHostClosedError" in e.message): frappe.throw(_("PDF generation failed")) except cups.IPPError: frappe.throw(_("Printing failed")) @@ -76,4 +85,4 @@ def print_label_by_server(doctype, name, qty, printer_setting, print_format=None @frappe.whitelist() def get_associated_stockentry(workorder): - return frappe.get_last_doc('Stock Entry', filters = {"work_order": workorder}) \ No newline at end of file + return frappe.get_last_doc('Stock Entry', filters={"work_order": workorder}) diff --git a/label_printing/label_printing/doctype/label_printer_settings/label_printer_settings.json b/label_printing/label_printing/doctype/label_printer_settings/label_printer_settings.json index 9d85795..e4acf58 100644 --- a/label_printing/label_printing/doctype/label_printer_settings/label_printer_settings.json +++ b/label_printing/label_printing/doctype/label_printer_settings/label_printer_settings.json @@ -8,7 +8,8 @@ "field_order": [ "label_printer", "label_height", - "label_width" + "label_width", + "label_print_format" ], "fields": [ { @@ -28,12 +29,18 @@ "fieldname": "label_width", "fieldtype": "Int", "label": "Label Width" + }, + { + "fieldname": "label_print_format", + "fieldtype": "Link", + "label": "Label Print Format", + "options": "Print Format" } ], "index_web_pages_for_search": 1, "issingle": 1, "links": [], - "modified": "2022-02-09 11:54:25.055075", + "modified": "2022-04-03 14:50:49.776678", "modified_by": "Administrator", "module": "Label Printing", "name": "Label Printer Settings", diff --git a/label_printing/label_printing/print_format/label/label.json b/label_printing/label_printing/print_format/label/label.json index 6225eca..a24a99e 100644 --- a/label_printing/label_printing/print_format/label/label.json +++ b/label_printing/label_printing/print_format/label/label.json @@ -2,18 +2,18 @@ "absolute_value": 0, "align_labels_right": 0, "creation": "2022-03-01 09:42:22.551134", - "css": ".print-format { margin: 0; padding: 0; width: 104mm; max-height: 40mm; } .page-size { width: 104mm; } .label-wrapper { width: 100%; text-align: center; display: -webkit-box; /* wkhtmltopdf uses this one */ display: -webkit-flex; display: flex; -webkit-box-pack: center; /* wkhtmltopdf uses this one */ -webkit-justify-content: center; justify-content: center; font-size: 19px; } .left-wrapper { width: 80%; } .left-wrapper div { white-space: nowrap; font-size: 15px; } .right-wrapper { width: 20%; text-align: right; } .label-wrapper h4 { margin: 0; word-break: break-all; font-size: 25px; } .label-wrapper h4 small { color: black; } .barcode { font-family: 'Libre Barcode 128', cursive; font-size: 30px; }", + "css": ".print-format {\n\tmargin: 0;\n\tpadding: 0;\n margin-left: 1mm;\n margin-right: 1mm;\n margin-top: 1mm;\n margin-bottom: 1mm;\n\twidth: 100mm;\n\tmax-height: 39mm;\n}\n\n.page-size {\n\twidth: 104mm;\n\theight: 40mm;\n}\n\n.label-wrapper {\n\twidth: 100mm;\n\ttext-align: center;\n\tdisplay: -webkit-box;\n\t/* wkhtmltopdf uses this one */\n\tdisplay: -webkit-flex;\n\tdisplay: flex;\n\t-webkit-box-pack: center;\n\t/* wkhtmltopdf uses this one */\n\t-webkit-justify-content: center;\n\tjustify-content: center;\n\tfont-size: 15px;\n}\n\n.left-wrapper {\n\twidth: 80mm;\n}\n\n.left-wrapper div {\n\twhite-space: nowrap;\n\tfont-size: 12px;\n}\n\n.right-wrapper {\n\twidth: 20mm;\n\ttext-align: right;\n}\n\n.label-wrapper h4 {\n\tmargin: 0;\n\tword-break: break-all;\n\tfont-size: 23px;\n}\n\n.label-wrapper h4 small {\n\tcolor: black;\n}\n\n", "custom_format": 1, "disabled": 0, "doc_type": "Label", "docstatus": 0, "doctype": "Print Format", "font": "Default", - "html": "
\n\n
\n {% if doc.item_code %}\n

\n {{doc.item_code}}\n

\n {% endif %}\n\n {% if doc.item_name%}\n

\n {{doc.item_name}}\n

\n {% endif %}\n\n\n {% if doc.customer %}\n
{{doc.customer}}
\n {% endif %}\n\n {% if doc.delivery_date %}\n
\n {{_(\"Delivery Date\")}} {{frappe.utils.formatdate(doc.delivery_date, \"dd.MM.YYYY\")}}\n
\n {% endif %}\n\n {% if doc.batch %}\n
\n {{_(\"Batch\")}} {{doc.batch}}\n
\n {% endif %}\n\n {% if doc.qty %}\n
\n {{_(\"Qty\")}} {{ doc.qty }}\n
\n {% endif %}\n\n {% if doc.information%}\n
\n {{doc.information}}\n
\n {% endif %}\n\n\n
\n \n
\n \n
\n\n\n
", + "html": "{% if doc.customer %}\n
\n\n
\n {% if doc.item_code %}\n

\n {{doc.item_code}}\n

\n {% endif %}\n\n {% if doc.item_name%}\n

\n {{doc.item_name}}\n

\n {% endif %}\n\n\n {% if doc.customer %}\n
{{doc.customer}}
\n {% endif %}\n\n {% if doc.delivery_date %}\n
\n {{_(\"Delivery Date\")}} {{frappe.utils.formatdate(doc.delivery_date, \"dd.MM.YYYY\")}}\n
\n {% endif %}\n\n {% if doc.batch %}\n
\n {{_(\"Batch\")}} {{doc.batch}}\n
\n {% endif %}\n\n {% if doc.qty %}\n
\n {{_(\"Qty\")}} {{ doc.qty }}\n
\n {% endif %}\n\n {% if doc.information%}\n
\n {{doc.information}}\n
\n {% endif %}\n\n\n
\n \n
\n \n
\n
\n\n{% else %}\n\n
\n\n
\n {% if doc.item_code %}\n

\n {{doc.item_code}}\n

\n {% endif %}\n\n\n {% if doc.batch %}\n
\n {{_(\"Batch\")}} {{doc.batch}}\n
\n {% endif %}\n\n\n {% if doc.information%}\n
\n {{doc.information}}\n
\n {% endif %}\n\n\n
\n
\n\n{% endif %}", "idx": 0, "line_breaks": 0, - "modified": "2022-03-01 09:43:42.031856", - "modified_by": "christian@prothmann.com", + "modified": "2022-04-03 14:52:16.543089", + "modified_by": "Administrator", "module": "Label Printing", "name": "Label", "owner": "christian@prothmann.com", diff --git a/label_printing/public/js/label_printing_desk.js b/label_printing/public/js/label_printing_desk.js index b6c5d5e..535d228 100644 --- a/label_printing/public/js/label_printing_desk.js +++ b/label_printing/public/js/label_printing_desk.js @@ -179,7 +179,6 @@ function setupLabelsDialog(page) { } fields.total_amount = doc.qty - fields.labels[0].qty_per_label = doc.qty d.fields_dict.labels.refresh(); } else if (fields.doctype === "Item") {