Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 

98 строки
3.0 KiB

  1. #!/usr/bin/env python3
  2. """
  3. ZPL label generator and TCP send for Zebra label printer (標簽機).
  4. - Rotated 90° clockwise for ~53 mm media width
  5. - UTF-8 (^CI28) for Chinese
  6. - Large fonts, QR code mag 6
  7. Standalone: python label_zpl.py
  8. Or import generate_zpl() / send_zpl() and call from Bag1.
  9. """
  10. import socket
  11. # Default printer (override via args or Bag1 settings)
  12. DEFAULT_PRINTER_IP = "192.168.17.27"
  13. DEFAULT_PRINTER_PORT = 3008
  14. SOCKET_TIMEOUT = 10
  15. def generate_zpl(
  16. batch_no: str,
  17. item_code: str = "PP2238-02",
  18. chinese_desc: str = "(餐廳用)凍咖啡底P+10(0.91L包)",
  19. ) -> str:
  20. """
  21. Generates ZPL label:
  22. - Rotated 90° clockwise to fit ~53 mm media width
  23. - UTF-8 mode (^CI28) for correct Chinese display
  24. - Larger fonts
  25. - Bigger QR code (mag 6)
  26. """
  27. return f"""^XA
  28. ^PW420 ^# Fits ~53 mm width (~420 dots @ 203 dpi)
  29. ^LL780 ^# Taller label after rotation + bigger elements
  30. ^PO N ^# Normal — change to ^POI if upside-down
  31. ^CI28 ^# Enable UTF-8 / Unicode for Chinese (critical fix for boxes)
  32. ^FO70,70
  33. ^A@R,60,60,E:SIMSUN.FNT^FD{chinese_desc}^FS
  34. ^# Very large Chinese text, rotated
  35. ^FO220,70
  36. ^A0R,50,50^FD{item_code}^FS
  37. ^# Larger item code
  38. ^FO310,70
  39. ^A0R,45,45^FDBatch: {batch_no}^FS
  40. ^# Larger batch text
  41. ^FO150,420
  42. ^BQN,2,6^FDQA,{batch_no}^FS
  43. ^# Bigger QR code (magnification 6), lower position for space
  44. ^XZ"""
  45. def send_zpl(
  46. zpl: str,
  47. host: str = DEFAULT_PRINTER_IP,
  48. port: int = DEFAULT_PRINTER_PORT,
  49. timeout: float = SOCKET_TIMEOUT,
  50. ) -> None:
  51. """Send ZPL to printer over TCP. Raises on connection/send error."""
  52. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  53. sock.settimeout(timeout)
  54. sock.connect((host, port))
  55. sock.sendall(zpl.encode("utf-8"))
  56. sock.close()
  57. # ────────────────────────────────────────────────
  58. # Example usage — prints one label
  59. # ────────────────────────────────────────────────
  60. if __name__ == "__main__":
  61. test_batch = "2025121209"
  62. zpl = generate_zpl(test_batch)
  63. print("Sending ZPL (90° rotated, UTF-8 Chinese, bigger QR):")
  64. print("-" * 90)
  65. print(zpl)
  66. print("-" * 90)
  67. try:
  68. send_zpl(zpl)
  69. print("Label sent successfully!")
  70. print("→ Check Chinese — should show real characters (not 口口 or symbols)")
  71. print("→ QR is now bigger (mag 6) — test scan with phone")
  72. print("→ If upside-down: edit ^PO N → ^POI")
  73. print("→ If still boxes: SimSun font may be missing — reinstall via Zebra Setup Utilities")
  74. except ConnectionRefusedError:
  75. print(f"Cannot connect to {DEFAULT_PRINTER_IP}:{DEFAULT_PRINTER_PORT} — printer off or wrong IP?")
  76. except socket.timeout:
  77. print("Connection timeout — check printer/network/port")
  78. except Exception as e:
  79. print(f"Error: {e}")