diff --git a/print/barcode_template.txt b/print/barcode_template.txt new file mode 100644 index 0000000..540f7bd --- /dev/null +++ b/print/barcode_template.txt @@ -0,0 +1,8 @@ +SIZE 76 mm,95 mm +GAP 2 mm,0 +DIRECTION 1 +DENSITY 8 +CLS +{items_block} +PRINT 1,1 +CUT \ No newline at end of file diff --git a/print/print_barcode.py b/print/print_barcode.py index 9b6ccb8..ed55302 100644 --- a/print/print_barcode.py +++ b/print/print_barcode.py @@ -1,6 +1,24 @@ import win32print import sys import json +import os + +def load_template(filename): + script_dir = os.path.dirname(os.path.abspath(__file__)) + path = os.path.join(script_dir, filename) + with open(path, "r", encoding="utf-8") as f: + return f.read() + +def money(n): + return f"{int(float(n)):,}" if n is not None else "0" + +def build_item_block(item, y_offset): + name = item['name'] + price = money(item['price']) + imei = item['imei'] + return f'''TEXT 20,{y_offset},"3",0,1,1,"{name}" +TEXT 20,{y_offset+30},"2",0,1,1,"{price} VND" +BARCODE 20,{y_offset+60},"128",50,1,0,2,2,"{imei}"''' def print_barcode(tspl_content, printer_name="Xprinter XP-350BM"): h = win32print.OpenPrinter(printer_name) @@ -15,18 +33,18 @@ def print_barcode(tspl_content, printer_name="Xprinter XP-350BM"): if __name__ == "__main__": data = json.loads(sys.argv[1]) + items = data['items'] # list of {name, price, imei} - tspl = f''' -SIZE 50 mm,40 mm -GAP 2 mm,0 -DIRECTION 1 -DENSITY 8 -CLS -TEXT 20,15,"3",0,1,1,"{data['name']}" -TEXT 20,70,"1",0,1,1,"Price: {data['price']} VND" -BARCODE 20,140,"128",50,1,0,2,2,"{data['imei']}" -PRINT 1,1 -'''.strip() + spacing = 180 # vertical gap between each stacked label, adjust if overlapping/too sparse + blocks = [] + for i, item in enumerate(items): + y_offset = 20 + (i * spacing) + blocks.append(build_item_block(item, y_offset)) + + items_block = "\n\n".join(blocks) + + template = load_template("barcode_template.txt") + tspl = template.replace("{items_block}", items_block) print_barcode(tspl) print("Sent to printer.") \ No newline at end of file diff --git a/print/test_barcode.txt b/print/test_barcode.txt deleted file mode 100644 index e979e48..0000000 --- a/print/test_barcode.txt +++ /dev/null @@ -1,17 +0,0 @@ -SIZE 50 mm,40 mm -GAP 2 mm,0 -DIRECTION 1 -DENSITY 8 -CLS - -TEXT 20,15,"3",0,1,1,"CellphoneS Store" -TEXT 20,45,"2",0,1,1,"iPhone 15 Pro Max 256GB" -TEXT 20,70,"2",0,1,1,"Color: Natural Titanium" -TEXT 20,95,"1",0,1,1,"Price: 29,990,000 VND" -TEXT 20,115,"1",0,1,1,"Warranty: 12 months" - -BARCODE 20,140,"128",50,1,0,2,2,"SKU123456789" - -QRCODE 280,140,"L",4,"A",0,"https://example.com/p/123456789" - -PRINT 1,1 \ No newline at end of file diff --git a/server/api/print/barcode.post.ts b/server/api/print/barcode.post.ts index d662004..adb2226 100644 --- a/server/api/print/barcode.post.ts +++ b/server/api/print/barcode.post.ts @@ -6,10 +6,10 @@ const execFileAsync = promisify(execFile); export default defineEventHandler(async (event) => { const body = await readBody(event); - const { name, imei, price } = body; + const { items } = body; const scriptPath = path.join(process.cwd(), "print", "print_barcode.py"); - const payload = JSON.stringify({ name, imei, price }); + const payload = JSON.stringify({ items }); try { const { stdout } = await execFileAsync("python", [scriptPath, payload]);