Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZPL Utils #136

Merged
merged 6 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
run: pip install mypy

- name: Install mypy types
run: mypy ./beam/. --install-types
run: mypy ./beam/. --install-types --non-interactive

- name: Run mypy
uses: sasanquaneuf/mypy-github-action@releases/v1
Expand Down
103 changes: 100 additions & 3 deletions beam/beam/barcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
import uuid
from io import BytesIO

import barcode
import frappe
from barcode import Code128
from barcode.writer import ImageWriter
from zebra_zpl import Barcode, Label, Printable, Text


@frappe.whitelist()
Expand All @@ -26,7 +28,7 @@ def create_beam_barcode(doc, method=None):
b.idx = row_index + 1
doc.append(
"barcodes",
{"barcode": str(uuid.uuid4().int >> 64), "barcode_type": "Code128", "idx": 1},
{"barcode": f"{str(uuid.uuid4().int >> 64):020}", "barcode_type": "Code128", "idx": 1},
)
return doc

Expand All @@ -37,9 +39,104 @@ def barcode128(barcode_text: str) -> str:
if not barcode_text:
return ""
temp = BytesIO()
barcode.Code128(barcode_text, writer=barcode.writer.ImageWriter()).write(
instance = Code128(barcode_text, writer=ImageWriter())
instance.write(
temp,
options={"module_width": 0.4, "module_height": 10, "font_size": 0, "compress": True},
)
encoded = base64.b64encode(temp.getvalue()).decode("ascii")
return f'<img src="data:image/png;base64,{encoded}"/>'


@frappe.whitelist()
@frappe.read_only()
def formatted_zpl_barcode(barcode_text: str) -> str:
bc = Barcode(
barcode_text,
type="C",
human_readable="Y",
width=4,
height=260,
ratio=1,
justification="C",
position=(20, 40),
)
return bc.to_zpl()


@frappe.whitelist()
@frappe.read_only()
def formatted_zpl_label(
width: int, length: int, dpi: int = 203, print_speed: int = 2, copies: int = 1
) -> str:
l = frappe._dict()
# ^XA Start format
# ^LL<label height in dots>,<space between labels in dots>
# ^LH<label home - x,y coordinates of top left label>
# ^LS<shift the label to the left(or right)>
# ^PW<label width in dots>
# Print Rate(speed) (^PR command)
l.start = f"^XA^LL{length}^LH0,0^LS10^PW{width}^PR{print_speed}"
# Specify how many copies to print
# End format
l.end = f"^PQ{copies}^XZ\n"
return l


@frappe.whitelist()
@frappe.read_only()
def formatted_zpl_text(text: str, width: int | None = None) -> str:
tf = Text(text, font_type=0, font_size=28, position=(0, 25), width=width, y=25, justification="C")
return tf.to_zpl()


@frappe.whitelist()
@frappe.read_only()
def zebra_zpl_label(*args, **kwargs):
return ZPLLabelStringOutput(*args, **kwargs)


@frappe.whitelist()
@frappe.read_only()
def zebra_zpl_barcode(data: str, **kwargs):
return Barcode(data, **kwargs)


@frappe.whitelist()
@frappe.read_only()
def zebra_zpl_text(data: str, **kwargs):
return Text(data, **kwargs)


@frappe.whitelist()
@frappe.read_only()
def add_to_label(label: Label, element: Printable):
label.add(element)


class ZPLLabelStringOutput(Label):
def __init__(
self, width: int = 100, length: int = 100, dpi: int = 203, print_speed: int = 2, copies: int = 1
):
super().__init__(width, length, dpi, print_speed, copies)

def dump_contents(self, io=None):
s = ""
s += f"^XA^LL{self.length}^LH0,0^LS10^PW{self.width}^PR{self.print_speed}"
for e in self.elements:
s += e.to_zpl()

s += f"^PQ{self.copies}^XZ\n"
return s


"""
{% set label = namespace(zebra_zpl_label(width=4*203, length=6*203, dpi=203)) %}

{{ zpl_text(hu.item_code + " " + hu.warehouse) }}
{{ zpl_text(hu.posting_date + " " + hu.posting_time) }}

{% label.add(zebra_zpl_barcode(hu.handling_unit, width=4, height=260, position=(20, 40), justification="C", ratio=1, human_readable='Y') -%}
{{ label.dump_contents() }}

"""
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"absolute_value": 0,
"align_labels_right": 0,
"creation": "2024-07-01 15:46:33.585470",
"custom_format": 1,
"default_print_language": "en",
"disabled": 0,
"doc_type": "Handling Unit",
"docstatus": 0,
"doctype": "Print Format",
"font_size": 14,
"idx": 0,
"line_breaks": 0,
"margin_bottom": 15.0,
"margin_left": 15.0,
"margin_right": 15.0,
"margin_top": 15.0,
"modified": "2024-07-01 18:53:58.230171",
"modified_by": "Administrator",
"module": "BEAM",
"name": "Handling Unit 6x4 ZPL Format",
"owner": "Administrator",
"page_number": "Hide",
"print_format_builder": 0,
"print_format_builder_beta": 0,
"print_format_type": "Jinja",
"raw_commands": "{% set hu = get_handling_unit(doc.name) %}\n{% set label = zebra_zpl_label(width=4*203, length=6*203, dpi=203) -%}\n\n{{ label.add(zebra_zpl_barcode(hu.handling_unit, width=4, height=260, position=(20, 40), justification=\"C\", ratio=1, human_readable='N')) -}}\n{{ label.add(zebra_zpl_text(hu.handling_unit, position=(40, 320), width=(4*203-80), font_size=40, justification=\"C\")) }}\n{{ label.add(zebra_zpl_text(frappe.utils.cstr(hu.qty) + \" \" + hu.uom, position=(40, 400), width=(4*203-80), font_size=40)) }}\n{{ label.add(zebra_zpl_text(hu.item_code, position=(40, 480), width=(4*203-80), font_size=40)) }}\n{{ label.add(zebra_zpl_text(hu.warehouse, position=(40, 560), width=(4*203-80), font_size=40)) }}\n{{ label.add(zebra_zpl_text(frappe.utils.format_datetime(hu.posting_datetime), position=(40, 640), width=(4*203-80), font_size=40)) }}\n\n{{ label.dump_contents() }}",
"raw_printing": 1,
"show_section_headings": 0,
"standard": "Yes"
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"absolute_value": 0,
"align_labels_right": 0,
"creation": "2024-07-01 15:53:35.152254",
"css": ".print-format-preview {\n background-color: var(--gray-200);\n}\n\n.zpl-label {\n min-height: 4in;\n max-height: 4in;\n min-width: 6in;\n max-width: 6in;\n background-color: white;\n}\n\n.print-format {\n max-width: unset;\n min-height: unset;\n}\n\n.print-preview {\n background: unset;\n}",
"custom_format": 1,
"default_print_language": "en",
"disabled": 0,
"doc_type": "Handling Unit",
"docstatus": 0,
"doctype": "Print Format",
"font_size": 14,
"html": "<div class=\"zpl-label\">\n<img src=\"data:image/png;base64,{{ labelary_api(doc, 'Handling Unit 6x4 ZPL Format', {}) }}\" /> \n</div>\n",
"idx": 0,
"line_breaks": 0,
"margin_bottom": 15.0,
"margin_left": 15.0,
"margin_right": 15.0,
"margin_top": 15.0,
"modified": "2024-07-01 18:54:12.428312",
"modified_by": "Administrator",
"module": "BEAM",
"name": "Labelary Print Preview",
"owner": "Administrator",
"page_number": "Hide",
"print_format_builder": 0,
"print_format_builder_beta": 0,
"print_format_type": "Jinja",
"raw_commands": "",
"raw_printing": 0,
"show_section_headings": 0,
"standard": "Yes"
}
Empty file.
31 changes: 31 additions & 0 deletions beam/beam/print_format/warehouse_barcode/warehouse_barcode.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"absolute_value": 0,
"align_labels_right": 0,
"creation": "2024-06-11 10:14:57.183542",
"custom_format": 1,
"default_print_language": "en",
"disabled": 0,
"doc_type": "Warehouse",
"docstatus": 0,
"doctype": "Print Format",
"font_size": 14,
"html": "<meta name=\"pdfkit-orientation\" content=\"Portrait\"/>\n<div class=\"barcode-label-container\">\n <div id=\"margin-top\">0mm</div>\n <div id=\"margin-left\">0mm</div>\n <div id=\"margin-bottom\">0mm</div>\n <div id=\"margin-right\">0mm</div>\n\n <div id=\"barcode-label\" class=\"align-middle\">\n <div class=\"text-center\">\n {{barcode128(doc.barcodes[0].barcode)}}\n </div>\n <div class=\"text-center item_code\">\n <span class=\"ellipsis\">{{ doc.name }}</span>\n </div>\n </div>\n</div>\n\n<style>\n#margin-top,#margin-left,#margin-bottom,#margin-right {\n display: none;\n}\n\n\n@media screen, print {\n .barcode-label-container {\n border: 1px solid black;\n padding: 0;\n margin: 0;\n min-width: 3in;\n max-width: 3in;\n min-height: 1in;\n max-height: 1in;\n font-family: Helvetica, sans-serif;\n display: block;\n }\n .text-center {\n margin: 0;\n padding: 0;\n }\n .barcode {\n max-width: 2.8in;\n min-width: 2.8in;\n min-height: .5in;\n max-height: .5in;\n }\n .item_code {\n }\n}\n\n</style>",
"idx": 0,
"line_breaks": 0,
"margin_bottom": 15.0,
"margin_left": 15.0,
"margin_right": 15.0,
"margin_top": 15.0,
"modified": "2024-06-11 10:16:45.288929",
"modified_by": "Administrator",
"module": "BEAM",
"name": "Warehouse Barcode",
"owner": "Administrator",
"page_number": "Hide",
"print_format_builder": 0,
"print_format_builder_beta": 0,
"print_format_type": "Jinja",
"raw_printing": 0,
"show_section_headings": 0,
"standard": "Yes"
}
Loading
Loading