Bernd Wurst commited on 2026-11-26 17:47:00
Zeige 16 geänderte Dateien mit 911 Einfügungen und 0 Löschungen.
| ... | ... |
@@ -0,0 +1,14 @@ |
| 1 |
+<?xml version="1.0" encoding="UTF-8"?> |
|
| 2 |
+<module external.system.id="pyproject.toml" type="PYTHON_MODULE" version="4"> |
|
| 3 |
+ <component name="NewModuleRootManager"> |
|
| 4 |
+ <content url="file://$MODULE_DIR$"> |
|
| 5 |
+ <sourceFolder url="file://$MODULE_DIR$/src" /> |
|
| 6 |
+ </content> |
|
| 7 |
+ <orderEntry type="inheritedJdk" /> |
|
| 8 |
+ <orderEntry type="sourceFolder" forTests="false" /> |
|
| 9 |
+ </component> |
|
| 10 |
+ <component name="PackageRequirementsSettings" /> |
|
| 11 |
+ <component name="PyDocumentationSettings" /> |
|
| 12 |
+ <component name="ReSTService" /> |
|
| 13 |
+ <component name="TestRunnerService" /> |
|
| 14 |
+</module> |
|
| 0 | 15 |
\ No newline at end of file |
| ... | ... |
@@ -0,0 +1,22 @@ |
| 1 |
+<component name="InspectionProjectProfileManager"> |
|
| 2 |
+ <profile version="1.0"> |
|
| 3 |
+ <option name="myName" value="Project Default" /> |
|
| 4 |
+ <inspection_tool class="HtmlUnknownAttribute" enabled="true" level="WARNING" enabled_by_default="true"> |
|
| 5 |
+ <option name="myValues"> |
|
| 6 |
+ <value> |
|
| 7 |
+ <list size="1"> |
|
| 8 |
+ <item index="0" class="java.lang.String" itemvalue="config" /> |
|
| 9 |
+ </list> |
|
| 10 |
+ </value> |
|
| 11 |
+ </option> |
|
| 12 |
+ <option name="myCustomValuesEnabled" value="true" /> |
|
| 13 |
+ </inspection_tool> |
|
| 14 |
+ <inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true"> |
|
| 15 |
+ <option name="ignoredIdentifiers"> |
|
| 16 |
+ <list> |
|
| 17 |
+ <option value="pyscript.document" /> |
|
| 18 |
+ </list> |
|
| 19 |
+ </option> |
|
| 20 |
+ </inspection_tool> |
|
| 21 |
+ </profile> |
|
| 22 |
+</component> |
|
| 0 | 23 |
\ No newline at end of file |
| ... | ... |
@@ -0,0 +1,8 @@ |
| 1 |
+<?xml version="1.0" encoding="UTF-8"?> |
|
| 2 |
+<project version="4"> |
|
| 3 |
+ <component name="ProjectModuleManager"> |
|
| 4 |
+ <modules> |
|
| 5 |
+ <module fileurl="file://$PROJECT_DIR$/.idea/escpos-printer.iml" filepath="$PROJECT_DIR$/.idea/escpos-printer.iml" /> |
|
| 6 |
+ </modules> |
|
| 7 |
+ </component> |
|
| 8 |
+</project> |
|
| 0 | 9 |
\ No newline at end of file |
| ... | ... |
@@ -0,0 +1,19 @@ |
| 1 |
+# escpos-printer |
|
| 2 |
+ |
|
| 3 |
+ESC/POS receipt printer library (USB and network). |
|
| 4 |
+ |
|
| 5 |
+Parts of this is based on a ancient version of https://github.com/python-escpos/python-escpos |
|
| 6 |
+ |
|
| 7 |
+```python |
|
| 8 |
+from escpos_printer import ESCPrinter |
|
| 9 |
+ |
|
| 10 |
+p = ESCPrinter() # first supported USB printer |
|
| 11 |
+p = ESCPrinter(type="network", address="192.168.1.50", port=9100, cp858=14) |
|
| 12 |
+p.text("Hello\n")
|
|
| 13 |
+p.cut() |
|
| 14 |
+``` |
|
| 15 |
+ |
|
| 16 |
+`usb_fix_script` optionally names a shell command run when USB access fails (e.g. to fix permissions). |
|
| 17 |
+ |
|
| 18 |
+Install: `pip install .` (or `pip install git+<repo-url>`). |
|
| 19 |
+Licensed under MIT license |
|
| 0 | 20 |
\ No newline at end of file |
| ... | ... |
@@ -0,0 +1,16 @@ |
| 1 |
+[build-system] |
|
| 2 |
+requires = ["setuptools>=61"] |
|
| 3 |
+build-backend = "setuptools.build_meta" |
|
| 4 |
+ |
|
| 5 |
+[project] |
|
| 6 |
+name = "escpos-printer" |
|
| 7 |
+version = "0.1.0" |
|
| 8 |
+description = "Simple ESC/POS receipt printer driver (USB and network)" |
|
| 9 |
+readme = "README.md" |
|
| 10 |
+requires-python = ">=3.8" |
|
| 11 |
+license = {text = "GPL-3.0-or-later"}
|
|
| 12 |
+authors = [{name = "Bernd Wurst", email = "bernd@schokokeks.org"}]
|
|
| 13 |
+dependencies = ["pyusb", "Pillow", "six"] |
|
| 14 |
+ |
|
| 15 |
+[tool.setuptools.packages.find] |
|
| 16 |
+where = ["src"] |
| ... | ... |
@@ -0,0 +1,129 @@ |
| 1 |
+""" Image format handling class |
|
| 2 |
+ |
|
| 3 |
+This module contains the image format handler :py:class:`EscposImage`. |
|
| 4 |
+ |
|
| 5 |
+:author: `Michael Billington <michael.billington@gmail.com>`_ |
|
| 6 |
+:organization: `python-escpos <https://github.com/python-escpos>`_ |
|
| 7 |
+:copyright: Copyright (c) 2016 Michael Billington <michael.billington@gmail.com> |
|
| 8 |
+:license: MIT |
|
| 9 |
+""" |
|
| 10 |
+ |
|
| 11 |
+ |
|
| 12 |
+import math |
|
| 13 |
+from PIL import Image, ImageOps |
|
| 14 |
+ |
|
| 15 |
+ |
|
| 16 |
+class EscposImage(object): |
|
| 17 |
+ """ |
|
| 18 |
+ Load images in, and output ESC/POS formats. |
|
| 19 |
+ |
|
| 20 |
+ The class is designed to efficiently delegate image processing to |
|
| 21 |
+ PIL, rather than spend CPU cycles looping over pixels. |
|
| 22 |
+ """ |
|
| 23 |
+ |
|
| 24 |
+ def __init__(self, img_source): |
|
| 25 |
+ """ |
|
| 26 |
+ Load in an image |
|
| 27 |
+ |
|
| 28 |
+ :param img_source: PIL.Image, or filename to load one from. |
|
| 29 |
+ """ |
|
| 30 |
+ if isinstance(img_source, Image.Image): |
|
| 31 |
+ img_original = img_source |
|
| 32 |
+ else: |
|
| 33 |
+ img_original = Image.open(img_source) |
|
| 34 |
+ |
|
| 35 |
+ # store image for eventual further processing (splitting) |
|
| 36 |
+ self.img_original = img_original |
|
| 37 |
+ |
|
| 38 |
+ # Convert to white RGB background, paste over white background |
|
| 39 |
+ # to strip alpha. |
|
| 40 |
+ img_original = img_original.convert("RGBA")
|
|
| 41 |
+ im = Image.new("RGB", img_original.size, (255, 255, 255))
|
|
| 42 |
+ im.paste(img_original, mask=img_original.split()[3]) |
|
| 43 |
+ # Convert down to greyscale |
|
| 44 |
+ im = im.convert("L")
|
|
| 45 |
+ # Invert: Only works on 'L' images |
|
| 46 |
+ im = ImageOps.invert(im) |
|
| 47 |
+ # Pure black and white |
|
| 48 |
+ self._im = im.convert("1")
|
|
| 49 |
+ |
|
| 50 |
+ @property |
|
| 51 |
+ def width(self): |
|
| 52 |
+ """ |
|
| 53 |
+ Width of image in pixels |
|
| 54 |
+ """ |
|
| 55 |
+ width_pixels, _ = self._im.size |
|
| 56 |
+ return width_pixels |
|
| 57 |
+ |
|
| 58 |
+ @property |
|
| 59 |
+ def width_bytes(self): |
|
| 60 |
+ """ |
|
| 61 |
+ Width of image if you use 8 pixels per byte and 0-pad at the end. |
|
| 62 |
+ """ |
|
| 63 |
+ return (self.width + 7) >> 3 |
|
| 64 |
+ |
|
| 65 |
+ @property |
|
| 66 |
+ def height(self): |
|
| 67 |
+ """ |
|
| 68 |
+ Height of image in pixels |
|
| 69 |
+ """ |
|
| 70 |
+ _, height_pixels = self._im.size |
|
| 71 |
+ return height_pixels |
|
| 72 |
+ |
|
| 73 |
+ def to_column_format(self, high_density_vertical=True): |
|
| 74 |
+ """ |
|
| 75 |
+ Extract slices of an image as equal-sized blobs of column-format data. |
|
| 76 |
+ |
|
| 77 |
+ :param high_density_vertical: Printed line height in dots |
|
| 78 |
+ """ |
|
| 79 |
+ im = self._im.transpose(Image.ROTATE_270).transpose(Image.FLIP_LEFT_RIGHT) |
|
| 80 |
+ line_height = 24 if high_density_vertical else 8 |
|
| 81 |
+ width_pixels, height_pixels = im.size |
|
| 82 |
+ top = 0 |
|
| 83 |
+ left = 0 |
|
| 84 |
+ while left < width_pixels: |
|
| 85 |
+ box = (left, top, left + line_height, top + height_pixels) |
|
| 86 |
+ im_slice = im.transform((line_height, height_pixels), Image.EXTENT, box) |
|
| 87 |
+ im_bytes = im_slice.tobytes() |
|
| 88 |
+ yield (im_bytes) |
|
| 89 |
+ left += line_height |
|
| 90 |
+ |
|
| 91 |
+ def to_raster_format(self): |
|
| 92 |
+ """ |
|
| 93 |
+ Convert image to raster-format binary |
|
| 94 |
+ """ |
|
| 95 |
+ return self._im.tobytes() |
|
| 96 |
+ |
|
| 97 |
+ def split(self, fragment_height): |
|
| 98 |
+ """ |
|
| 99 |
+ Split an image into multiple fragments after fragment_height pixels |
|
| 100 |
+ |
|
| 101 |
+ :param fragment_height: height of fragment |
|
| 102 |
+ :return: list of PIL objects |
|
| 103 |
+ """ |
|
| 104 |
+ passes = int(math.ceil(self.height / fragment_height)) |
|
| 105 |
+ fragments = [] |
|
| 106 |
+ for n in range(0, passes): |
|
| 107 |
+ left = 0 |
|
| 108 |
+ right = self.width |
|
| 109 |
+ upper = n * fragment_height |
|
| 110 |
+ lower = min((n + 1) * fragment_height, self.height) |
|
| 111 |
+ box = (left, upper, right, lower) |
|
| 112 |
+ fragments.append(self.img_original.crop(box)) |
|
| 113 |
+ return fragments |
|
| 114 |
+ |
|
| 115 |
+ def center(self, max_width): |
|
| 116 |
+ """In-place image centering |
|
| 117 |
+ |
|
| 118 |
+ :param: Maximum width in order to deduce x offset for centering |
|
| 119 |
+ :return: None |
|
| 120 |
+ """ |
|
| 121 |
+ old_width, height = self._im.size |
|
| 122 |
+ new_size = (max_width, height) |
|
| 123 |
+ |
|
| 124 |
+ new_im = Image.new("1", new_size)
|
|
| 125 |
+ paste_x = int((max_width - old_width) / 2) |
|
| 126 |
+ |
|
| 127 |
+ new_im.paste(self._im, (paste_x, 0)) |
|
| 128 |
+ |
|
| 129 |
+ self._im = new_im |
| ... | ... |
@@ -0,0 +1,251 @@ |
| 1 |
+# -* coding: utf-8 *- |
|
| 2 |
+# (C) 2012 by Bernd Wurst <bernd@schokokeks.org> |
|
| 3 |
+ |
|
| 4 |
+# This file is part of Bib2011. |
|
| 5 |
+# |
|
| 6 |
+# Bib2011 is free software: you can redistribute it and/or modify |
|
| 7 |
+# it under the terms of the GNU General Public License as published by |
|
| 8 |
+# the Free Software Foundation, either version 3 of the License, or |
|
| 9 |
+# (at your option) any later version. |
|
| 10 |
+# |
|
| 11 |
+# Bib2011 is distributed in the hope that it will be useful, |
|
| 12 |
+# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 13 |
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 14 |
+# GNU General Public License for more details. |
|
| 15 |
+# |
|
| 16 |
+# You should have received a copy of the GNU General Public License |
|
| 17 |
+# along with Bib2011. If not, see <http://www.gnu.org/licenses/>. |
|
| 18 |
+ |
|
| 19 |
+import time |
|
| 20 |
+from functools import reduce |
|
| 21 |
+ |
|
| 22 |
+from .networkprinter import NetworkPrinter |
|
| 23 |
+from .usbprinter import USBPrinter |
|
| 24 |
+from .EscposImage import EscposImage |
|
| 25 |
+import six |
|
| 26 |
+ |
|
| 27 |
+# Barcode chars |
|
| 28 |
+BARCODE_TXT_BLW = b'\x1d\x48\x02' # HRI barcode chars below |
|
| 29 |
+BARCODE_FONT_A = b'\x1d\x66\x00' # Font type A for HRI barcode chars |
|
| 30 |
+BARCODE_HEIGHT = b'\x1d\x68\x64' # Barcode Height [1-255] |
|
| 31 |
+BARCODE_WIDTH = b'\x1d\x77\x03' # Barcode Width [2-6] |
|
| 32 |
+BARCODE_EAN13 = b'\x1d\x6b\x02' # Barcode type EAN13 |
|
| 33 |
+ |
|
| 34 |
+ |
|
| 35 |
+ |
|
| 36 |
+ |
|
| 37 |
+class ESCPrinter(object): |
|
| 38 |
+ def __init__(self, type='usb', address=None, port=None, cp858=None, usb_fix_script=None): |
|
| 39 |
+ """type: 'usb' or 'network'. address/port/cp858 are used for network printers.""" |
|
| 40 |
+ if type == 'usb': |
|
| 41 |
+ self.device = USBPrinter(fix_script=usb_fix_script) |
|
| 42 |
+ elif type == 'network': |
|
| 43 |
+ if address is None or port is None: |
|
| 44 |
+ raise ValueError("address and port are required for network printers")
|
|
| 45 |
+ kwargs = {} if cp858 is None else {'cp858': cp858}
|
|
| 46 |
+ self.device = NetworkPrinter(address, port, **kwargs) |
|
| 47 |
+ else: |
|
| 48 |
+ raise ValueError("unknown printer type: %r" % (type,))
|
|
| 49 |
+ self.ready = self.device.initialized |
|
| 50 |
+ self.codepage_id = self.device.printer_config['cp858'] or 19 |
|
| 51 |
+ self.reset() |
|
| 52 |
+ |
|
| 53 |
+ def initialize(self): |
|
| 54 |
+ if not self.device.initialized: |
|
| 55 |
+ self.device.initialize() |
|
| 56 |
+ self.ready = self.device.initialized |
|
| 57 |
+ |
|
| 58 |
+ def __del__(self): |
|
| 59 |
+ self.terminate() |
|
| 60 |
+ del self.device |
|
| 61 |
+ |
|
| 62 |
+ def terminate(self): |
|
| 63 |
+ self.device.terminate() |
|
| 64 |
+ |
|
| 65 |
+ def reset(self): |
|
| 66 |
+ # Reset printer |
|
| 67 |
+ self.write(self.encode(chr(27) + chr(64))) |
|
| 68 |
+ # Set code page 858 (mit Euro-Zeichen) |
|
| 69 |
+ self.write(self.encode(chr(27) + chr(116) + chr(self.codepage_id))) |
|
| 70 |
+ |
|
| 71 |
+ |
|
| 72 |
+ def encode(self, string): |
|
| 73 |
+ assert type(string) == str, 'illegal argument, cannot encode' |
|
| 74 |
+ return string.encode('cp858')
|
|
| 75 |
+ |
|
| 76 |
+ def write(self, string): |
|
| 77 |
+ if not isinstance(string, bytes): |
|
| 78 |
+ string = string.encode('cp858')
|
|
| 79 |
+ self.raw(string) |
|
| 80 |
+ |
|
| 81 |
+ def raw(self, command): |
|
| 82 |
+ if not self.device.initialized: |
|
| 83 |
+ return |
|
| 84 |
+ self.device.write(command) |
|
| 85 |
+ |
|
| 86 |
+ def read(self): |
|
| 87 |
+ if not self.device.initialized: |
|
| 88 |
+ return |
|
| 89 |
+ return self.device.read() |
|
| 90 |
+ |
|
| 91 |
+ def print_esc(self, data): |
|
| 92 |
+ # split bei RESET-Befehl |
|
| 93 |
+ for part in data.split(self.encode(chr(27) + chr(64))): |
|
| 94 |
+ # alten codepage-Befehl herausnehmen |
|
| 95 |
+ if part.startswith(self.encode(chr(27) + chr(116))): |
|
| 96 |
+ part = part[3:] |
|
| 97 |
+ self.raw(part) |
|
| 98 |
+ self.reset() |
|
| 99 |
+ |
|
| 100 |
+ |
|
| 101 |
+ def ean13(self, code): |
|
| 102 |
+ if len(code) != 12: |
|
| 103 |
+ raise ValueError('require 12 digits')
|
|
| 104 |
+ code = [int(i) for i in code] |
|
| 105 |
+ sum_ = lambda x, y: int(x) + int(y) |
|
| 106 |
+ evensum = reduce(sum_, code[::2]) |
|
| 107 |
+ oddsum = reduce(sum_, code[1::2]) |
|
| 108 |
+ check = (10 - ((evensum + oddsum * 3) % 10)) % 10 |
|
| 109 |
+ code = b''.join([bytes(i) for i in code]) + bytes(check) |
|
| 110 |
+ # Align Bar Code() |
|
| 111 |
+ self.raw(b'\x1b\x61\x01') # center |
|
| 112 |
+ self.raw(BARCODE_HEIGHT) |
|
| 113 |
+ self.raw(BARCODE_WIDTH) |
|
| 114 |
+ self.raw(BARCODE_FONT_A) |
|
| 115 |
+ self.raw(BARCODE_TXT_BLW) |
|
| 116 |
+ self.raw(BARCODE_EAN13) |
|
| 117 |
+ # Print Code |
|
| 118 |
+ if code: |
|
| 119 |
+ self.raw(code) |
|
| 120 |
+ else: |
|
| 121 |
+ raise ValueError('No code')
|
|
| 122 |
+ |
|
| 123 |
+ def text(self, string): |
|
| 124 |
+ self.write(string) |
|
| 125 |
+ |
|
| 126 |
+ def bold(self, state=True): |
|
| 127 |
+ if state: |
|
| 128 |
+ self.write(chr(27) + chr(69) + chr(1)) |
|
| 129 |
+ else: |
|
| 130 |
+ self.write(chr(27) + chr(69) + chr(0)) |
|
| 131 |
+ |
|
| 132 |
+ def underline(self, state=True): |
|
| 133 |
+ if state == 2: |
|
| 134 |
+ self.write(chr(27) + chr(45) + chr(2)) |
|
| 135 |
+ elif state == 1 or state is True: |
|
| 136 |
+ self.write(chr(27) + chr(45) + chr(1)) |
|
| 137 |
+ else: |
|
| 138 |
+ self.write(chr(27) + chr(45) + chr(0)) |
|
| 139 |
+ |
|
| 140 |
+ def align(self, align): |
|
| 141 |
+ command = 0 |
|
| 142 |
+ if align == 'center': |
|
| 143 |
+ command = 1 |
|
| 144 |
+ elif align == 'right': |
|
| 145 |
+ command = 2 |
|
| 146 |
+ self.write(chr(27) + chr(97) + chr(command)) |
|
| 147 |
+ |
|
| 148 |
+ def font(self, mytype): |
|
| 149 |
+ command = 0 |
|
| 150 |
+ if mytype == 'B': |
|
| 151 |
+ command = 1 |
|
| 152 |
+ self.write(chr(27) + chr(77) + chr(command)) |
|
| 153 |
+ |
|
| 154 |
+ def fontsize(self, width, height): |
|
| 155 |
+ width = (width - 1) % 8 |
|
| 156 |
+ height = (height - 1) % 8 |
|
| 157 |
+ size = (width << 4) | height |
|
| 158 |
+ # set font scaling |
|
| 159 |
+ self.write(chr(29) + chr(33) + chr(size)) |
|
| 160 |
+ # turn smoothing on |
|
| 161 |
+ self.write(chr(29) + chr(98) + chr(1)) |
|
| 162 |
+ |
|
| 163 |
+ def cut(self, mode='full'): |
|
| 164 |
+ command = 0 |
|
| 165 |
+ if mode == 'partial': |
|
| 166 |
+ command = 1 |
|
| 167 |
+ blanklines = 3 |
|
| 168 |
+ self.write(chr(29) + chr(86) + chr(command + 65) + chr(blanklines)) |
|
| 169 |
+ |
|
| 170 |
+ def openDrawer(self): |
|
| 171 |
+ self.write(chr(27) + chr(112) + chr(0) + chr(25) + chr(250)) |
|
| 172 |
+ |
|
| 173 |
+ def drawerIsOpen(self): |
|
| 174 |
+ garbage = self.read() |
|
| 175 |
+ self.write(chr(27) + chr(117) + chr(0)) |
|
| 176 |
+ status = None |
|
| 177 |
+ for i in range(100): |
|
| 178 |
+ time.sleep(0.01) |
|
| 179 |
+ recv = self.read().tolist() |
|
| 180 |
+ if len(recv) > 0: |
|
| 181 |
+ status = recv[0] |
|
| 182 |
+ break |
|
| 183 |
+ if status & 0x01: |
|
| 184 |
+ return False |
|
| 185 |
+ else: |
|
| 186 |
+ return True |
|
| 187 |
+ |
|
| 188 |
+ def image(self, imgfile, center=True): |
|
| 189 |
+ # heavily based on python-escpos (https://github.com/python-escpos/python-escpos) |
|
| 190 |
+ # by Manuel F Martinez <manpaz@bashlinux.com> and others |
|
| 191 |
+ im = EscposImage(imgfile) |
|
| 192 |
+ # Breite: 576 px |
|
| 193 |
+ max_width = 576 |
|
| 194 |
+ if center: |
|
| 195 |
+ im.center(max_width) |
|
| 196 |
+ # GS v 0, raster format bit image |
|
| 197 |
+ header = ( |
|
| 198 |
+ b"\x1d" |
|
| 199 |
+ + b"v0" |
|
| 200 |
+ + b"\x00" |
|
| 201 |
+ + self._int_low_high(im.width_bytes, 2) |
|
| 202 |
+ + self._int_low_high(im.height, 2) |
|
| 203 |
+ ) |
|
| 204 |
+ self.raw(header + im.to_raster_format()) |
|
| 205 |
+ |
|
| 206 |
+ @staticmethod |
|
| 207 |
+ def _int_low_high(inp_number, out_bytes): |
|
| 208 |
+ """Generate multiple bytes for a number: In lower and higher parts, or more parts as needed. |
|
| 209 |
+ :param inp_number: Input number |
|
| 210 |
+ :param out_bytes: The number of bytes to output (1 - 4). |
|
| 211 |
+ """ |
|
| 212 |
+ max_input = 256 << (out_bytes * 8) - 1 |
|
| 213 |
+ if not 1 <= out_bytes <= 4: |
|
| 214 |
+ raise ValueError("Can only output 1-4 bytes")
|
|
| 215 |
+ if not 0 <= inp_number <= max_input: |
|
| 216 |
+ raise ValueError( |
|
| 217 |
+ "Number too large. Can only output up to {0} in {1} bytes".format(
|
|
| 218 |
+ max_input, out_bytes |
|
| 219 |
+ ) |
|
| 220 |
+ ) |
|
| 221 |
+ outp = b"" |
|
| 222 |
+ for _ in range(0, out_bytes): |
|
| 223 |
+ outp += six.int2byte(inp_number % 256) |
|
| 224 |
+ inp_number //= 256 |
|
| 225 |
+ return outp |
|
| 226 |
+ |
|
| 227 |
+ |
|
| 228 |
+if __name__ == '__main__': |
|
| 229 |
+ import sys |
|
| 230 |
+ e = ESCPrinter() |
|
| 231 |
+ |
|
| 232 |
+ e.raw(sys.stdin.buffer.read()) |
|
| 233 |
+ # e.font('A')
|
|
| 234 |
+ # e.reset() |
|
| 235 |
+ # rawdata = '\xff' * (16 * 32) |
|
| 236 |
+ # e.raw('\x1d\x76\x30\0\x02\0\x01\0\xff')
|
|
| 237 |
+ # e.text('\n\n\n')
|
|
| 238 |
+ # e.fontsize(2,2) |
|
| 239 |
+ # e.text('1234567890'*3)
|
|
| 240 |
+ # e.text('\n')
|
|
| 241 |
+ # e.ean13('200123456789')
|
|
| 242 |
+ # printer = e |
|
| 243 |
+ # printer.image('logo.gif')
|
|
| 244 |
+ # printer.text('\n')
|
|
| 245 |
+ # printer.align('center')
|
|
| 246 |
+ # printer.text('Mosterei Bernd Wurst\n')
|
|
| 247 |
+ # printer.text('Köchersberg 30\n')
|
|
| 248 |
+ # printer.text('71540 Murrhardt\n')
|
|
| 249 |
+ # printer.text('07192 - 936434\n')
|
|
| 250 |
+ # printer.text('\n\n')
|
|
| 251 |
+ # e.cut() |
| ... | ... |
@@ -0,0 +1,43 @@ |
| 1 |
+# -* coding: utf-8 *- |
|
| 2 |
+# (C) 2012 by Bernd Wurst <bernd@schokokeks.org> |
|
| 3 |
+ |
|
| 4 |
+# This file is part of Bib2011. |
|
| 5 |
+# |
|
| 6 |
+# Bib2011 is free software: you can redistribute it and/or modify |
|
| 7 |
+# it under the terms of the GNU General Public License as published by |
|
| 8 |
+# the Free Software Foundation, either version 3 of the License, or |
|
| 9 |
+# (at your option) any later version. |
|
| 10 |
+# |
|
| 11 |
+# Bib2011 is distributed in the hope that it will be useful, |
|
| 12 |
+# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 13 |
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 14 |
+# GNU General Public License for more details. |
|
| 15 |
+# |
|
| 16 |
+# You should have received a copy of the GNU General Public License |
|
| 17 |
+# along with Bib2011. If not, see <http://www.gnu.org/licenses/>. |
|
| 18 |
+ |
|
| 19 |
+from .esc import ESCPrinter |
|
| 20 |
+ |
|
| 21 |
+# Barcode chars |
|
| 22 |
+BARCODE_TXT_BLW = '\x1d\x48\x02' # HRI barcode chars below |
|
| 23 |
+BARCODE_FONT_A = '\x1d\x66\x00' # Font type A for HRI barcode chars |
|
| 24 |
+BARCODE_HEIGHT = '\x1d\x68\x64' # Barcode Height [1-255] |
|
| 25 |
+BARCODE_WIDTH = '\x1d\x77\x03' # Barcode Width [2-6] |
|
| 26 |
+BARCODE_EAN13 = '\x1d\x6b\x02' # Barcode type EAN13 |
|
| 27 |
+ |
|
| 28 |
+ |
|
| 29 |
+ |
|
| 30 |
+class ESCCapture(ESCPrinter): |
|
| 31 |
+ def __init__(self, fileobject): |
|
| 32 |
+ self.codepage_id = 19 |
|
| 33 |
+ self.fileobject = fileobject |
|
| 34 |
+ |
|
| 35 |
+ def __del__(self): |
|
| 36 |
+ pass |
|
| 37 |
+ |
|
| 38 |
+ def raw(self, data): |
|
| 39 |
+ self.fileobject.write(data) |
|
| 40 |
+ |
|
| 41 |
+ def drawerIsOpen(self): |
|
| 42 |
+ return False |
|
| 43 |
+ |
| ... | ... |
@@ -0,0 +1,137 @@ |
| 1 |
+# -* coding: utf-8 *- |
|
| 2 |
+# (C) 2012 by Bernd Wurst <bernd@schokokeks.org> |
|
| 3 |
+ |
|
| 4 |
+# This file is part of Bib2011. |
|
| 5 |
+# |
|
| 6 |
+# Bib2011 is free software: you can redistribute it and/or modify |
|
| 7 |
+# it under the terms of the GNU General Public License as published by |
|
| 8 |
+# the Free Software Foundation, either version 3 of the License, or |
|
| 9 |
+# (at your option) any later version. |
|
| 10 |
+# |
|
| 11 |
+# Bib2011 is distributed in the hope that it will be useful, |
|
| 12 |
+# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 13 |
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 14 |
+# GNU General Public License for more details. |
|
| 15 |
+# |
|
| 16 |
+# You should have received a copy of the GNU General Public License |
|
| 17 |
+# along with Bib2011. If not, see <http://www.gnu.org/licenses/>. |
|
| 18 |
+ |
|
| 19 |
+from .esc import ESCPrinter |
|
| 20 |
+from base64 import b64encode |
|
| 21 |
+import io |
|
| 22 |
+from PIL import Image |
|
| 23 |
+ |
|
| 24 |
+class ESCHTML(ESCPrinter): |
|
| 25 |
+ def __init__(self, fileobject): |
|
| 26 |
+ self._formatchanged = None |
|
| 27 |
+ self.printer_out = fileobject |
|
| 28 |
+ self.printer_in = None |
|
| 29 |
+ self.printer_out.write('''<html><head><style>
|
|
| 30 |
+p {
|
|
| 31 |
+ margin: 0; |
|
| 32 |
+ padding: 0; |
|
| 33 |
+} |
|
| 34 |
+body {
|
|
| 35 |
+ font-family: "DejaVu Sans Mono", monospace; |
|
| 36 |
+ width: 48ch; |
|
| 37 |
+ padding: 2em; |
|
| 38 |
+ border: 1px solid black; |
|
| 39 |
+} |
|
| 40 |
+</style></head><body>''') |
|
| 41 |
+ self.open_tags = ['html', 'body'] |
|
| 42 |
+ self._open_p = False |
|
| 43 |
+ self._fontsize = (1,1) |
|
| 44 |
+ self._align = 'left' |
|
| 45 |
+ self._bold = False |
|
| 46 |
+ self._underlined = False |
|
| 47 |
+ |
|
| 48 |
+ def reset(self): |
|
| 49 |
+ pass |
|
| 50 |
+ |
|
| 51 |
+ def text(self, string): |
|
| 52 |
+ for line in string.splitlines(keepends=True): |
|
| 53 |
+ self.p(line) |
|
| 54 |
+ |
|
| 55 |
+ |
|
| 56 |
+ def bold(self, state=True): |
|
| 57 |
+ self._bold = state |
|
| 58 |
+ self._formatchanged = True |
|
| 59 |
+ |
|
| 60 |
+ |
|
| 61 |
+ def underline(self, state=True): |
|
| 62 |
+ self._underlined = state |
|
| 63 |
+ self._formatchanged = True |
|
| 64 |
+ |
|
| 65 |
+ |
|
| 66 |
+ def align(self, align): |
|
| 67 |
+ self._align = align |
|
| 68 |
+ self._formatchanged = True |
|
| 69 |
+ |
|
| 70 |
+ |
|
| 71 |
+ def font(self, type): |
|
| 72 |
+ pass |
|
| 73 |
+ |
|
| 74 |
+ |
|
| 75 |
+ def fontsize(self, width, height): |
|
| 76 |
+ self._fontsize = (width, height) |
|
| 77 |
+ self._formatchanged = True |
|
| 78 |
+ |
|
| 79 |
+ def p(self, text): |
|
| 80 |
+ style = [] |
|
| 81 |
+ if self._bold: |
|
| 82 |
+ style.append('font-weight: bold;')
|
|
| 83 |
+ if self._underlined: |
|
| 84 |
+ style.append('text-decoration: underline;')
|
|
| 85 |
+ if self._fontsize in [(2,2), (1,2)]: |
|
| 86 |
+ style.append('font-size: 200%;')
|
|
| 87 |
+ if self._fontsize == (1,2): |
|
| 88 |
+ style.append('font-stretch: 50%;')
|
|
| 89 |
+ if self._fontsize == (2,1): |
|
| 90 |
+ style.append('font-stretch: 200%;')
|
|
| 91 |
+ |
|
| 92 |
+ if not self._open_p: |
|
| 93 |
+ self.printer_out.write('<p style="text-align: %s;">' % (self._align))
|
|
| 94 |
+ self._open_p = True |
|
| 95 |
+ if text == '\n': |
|
| 96 |
+ text = ' \n' |
|
| 97 |
+ text = text.replace(' ', ' ')
|
|
| 98 |
+ self.printer_out.write('<span style="%s">%s</span>' % (' '.join(style), text.replace('\n', ''),))
|
|
| 99 |
+ if text.endswith('\n'):
|
|
| 100 |
+ self.printer_out.write('</p>\n')
|
|
| 101 |
+ self._open_p = False |
|
| 102 |
+ |
|
| 103 |
+ |
|
| 104 |
+ def cut(self, mode='full'): |
|
| 105 |
+ for tag in reversed(self.open_tags): |
|
| 106 |
+ self.printer_out.write('</%s>' % tag)
|
|
| 107 |
+ |
|
| 108 |
+ |
|
| 109 |
+ def image(self, imgfile, center=True): |
|
| 110 |
+ im = Image.open(imgfile) |
|
| 111 |
+ # Bild wird im HTML zu groß angezeigt, daher verkleinern wir. Dafür konvertieren wir in RGB, damit die Qualität besser wird. |
|
| 112 |
+ scale_factor=0.8 |
|
| 113 |
+ im = im.convert('RGB')
|
|
| 114 |
+ original_width, original_height = im.size |
|
| 115 |
+ new_width = int(original_width * scale_factor) |
|
| 116 |
+ new_height = int(original_height * scale_factor) |
|
| 117 |
+ im = im.resize((new_width, new_height), Image.LANCZOS) |
|
| 118 |
+ buffered = io.BytesIO() |
|
| 119 |
+ im.save(buffered, format="PNG") |
|
| 120 |
+ data = b64encode(buffered.getvalue()) |
|
| 121 |
+ |
|
| 122 |
+ content = '<img width="%s" height="%s" src="data:image/gif;base64,%s">' % (im.width, im.height, data.decode()) |
|
| 123 |
+ if center: |
|
| 124 |
+ content = '<center>' + content + '</center>' |
|
| 125 |
+ |
|
| 126 |
+ self.printer_out.write(content) |
|
| 127 |
+ |
|
| 128 |
+ |
|
| 129 |
+ |
|
| 130 |
+ def __del__(self): |
|
| 131 |
+ pass |
|
| 132 |
+ |
|
| 133 |
+ |
|
| 134 |
+ |
|
| 135 |
+ def drawerIsOpen(self): |
|
| 136 |
+ return False |
|
| 137 |
+ |
| ... | ... |
@@ -0,0 +1,107 @@ |
| 1 |
+# -* coding: utf-8 *- |
|
| 2 |
+# (C) 2012 by Bernd Wurst <bernd@schokokeks.org> |
|
| 3 |
+# This file is part of Bib2011. |
|
| 4 |
+# |
|
| 5 |
+# Bib2011 is free software: you can redistribute it and/or modify |
|
| 6 |
+# it under the terms of the GNU General Public License as published by |
|
| 7 |
+# the Free Software Foundation, either version 3 of the License, or |
|
| 8 |
+# (at your option) any later version. |
|
| 9 |
+# |
|
| 10 |
+# Bib2011 is distributed in the hope that it will be useful, |
|
| 11 |
+# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 12 |
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 13 |
+# GNU General Public License for more details. |
|
| 14 |
+# |
|
| 15 |
+# You should have received a copy of the GNU General Public License |
|
| 16 |
+# along with Bib2011. If not, see <http://www.gnu.org/licenses/>. |
|
| 17 |
+import socket |
|
| 18 |
+import logging |
|
| 19 |
+ |
|
| 20 |
+log = logging.getLogger('networkprinter')
|
|
| 21 |
+ |
|
| 22 |
+ |
|
| 23 |
+class NetworkPrinter (object): |
|
| 24 |
+ def __init__(self, address, port, cp858=14): |
|
| 25 |
+ self.connection = None |
|
| 26 |
+ self.address = address |
|
| 27 |
+ self.port = port |
|
| 28 |
+ self.printer_config = {'idVendor': None, 'idProduct': None, 'cp858': cp858}
|
|
| 29 |
+ self.initialized = False |
|
| 30 |
+ self.connect() |
|
| 31 |
+ |
|
| 32 |
+ def initialize(self): |
|
| 33 |
+ self.connect() |
|
| 34 |
+ |
|
| 35 |
+ def connect(self): |
|
| 36 |
+ try: |
|
| 37 |
+ self.connection = socket.create_connection((self.address, self.port), timeout=5) |
|
| 38 |
+ self.initialized = True |
|
| 39 |
+ except OSError as e: |
|
| 40 |
+ log.error(str(e)) |
|
| 41 |
+ pass |
|
| 42 |
+ |
|
| 43 |
+ def write(self, data): |
|
| 44 |
+ if type(data) == str: |
|
| 45 |
+ data = data.encode('cp858')
|
|
| 46 |
+ # Es gab Probleme mit langen Belegen, daher senden wir die Daten jetzt in kleineren Häppchen |
|
| 47 |
+ i = 0 |
|
| 48 |
+ while i < len(data): |
|
| 49 |
+ self.send(data[i:i + 1024]) |
|
| 50 |
+ i += 1024 |
|
| 51 |
+ |
|
| 52 |
+ def read(self): |
|
| 53 |
+ return self.recv() |
|
| 54 |
+ |
|
| 55 |
+ def recv(self): |
|
| 56 |
+ data = self.connection.recv(1024) |
|
| 57 |
+ if len(data) > 0: |
|
| 58 |
+ return data |
|
| 59 |
+ return None |
|
| 60 |
+ |
|
| 61 |
+ def send(self, data): |
|
| 62 |
+ num = 0 |
|
| 63 |
+ try: |
|
| 64 |
+ num = self.connection.send(data) |
|
| 65 |
+ except Exception: |
|
| 66 |
+ pass |
|
| 67 |
+ if num < len(data): |
|
| 68 |
+ self.connect() |
|
| 69 |
+ num = self.connection.send(data) |
|
| 70 |
+ if num < len(data): |
|
| 71 |
+ raise RuntimeError('printer not reachable')
|
|
| 72 |
+ |
|
| 73 |
+ def __del__(self): |
|
| 74 |
+ self.terminate() |
|
| 75 |
+ |
|
| 76 |
+ def terminate(self): |
|
| 77 |
+ if self.initialized: |
|
| 78 |
+ self.connection.close() |
|
| 79 |
+ self.initialized = False |
|
| 80 |
+ |
|
| 81 |
+ |
|
| 82 |
+ |
|
| 83 |
+ |
|
| 84 |
+if __name__ == '__main__': |
|
| 85 |
+ prn = NetworkPrinter('10.0.11.98', 9100)
|
|
| 86 |
+ # RESET |
|
| 87 |
+ prn.write('\x1b\x40')
|
|
| 88 |
+ # Code page |
|
| 89 |
+ prn.write('\x1b\x74\x0e')
|
|
| 90 |
+ #prn.write('Hallo äöü\n\n\n'.encode('cp858'))
|
|
| 91 |
+ prn.write('\x1d\x72\x01')
|
|
| 92 |
+ status = prn.read() |
|
| 93 |
+ if status: |
|
| 94 |
+ print(f'received {len(status)} bytes!')
|
|
| 95 |
+ print(f"{status[0]:08b}")
|
|
| 96 |
+ |
|
| 97 |
+ |
|
| 98 |
+ |
|
| 99 |
+ |
|
| 100 |
+ |
|
| 101 |
+ |
|
| 102 |
+ |
|
| 103 |
+ |
|
| 104 |
+ |
|
| 105 |
+ |
|
| 106 |
+ |
|
| 107 |
+ |
| ... | ... |
@@ -0,0 +1,142 @@ |
| 1 |
+# -* coding: utf-8 *- |
|
| 2 |
+# (C) 2012 by Bernd Wurst <bernd@schokokeks.org> |
|
| 3 |
+# This file is part of Bib2011. |
|
| 4 |
+# |
|
| 5 |
+# Bib2011 is free software: you can redistribute it and/or modify |
|
| 6 |
+# it under the terms of the GNU General Public License as published by |
|
| 7 |
+# the Free Software Foundation, either version 3 of the License, or |
|
| 8 |
+# (at your option) any later version. |
|
| 9 |
+# |
|
| 10 |
+# Bib2011 is distributed in the hope that it will be useful, |
|
| 11 |
+# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 12 |
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 13 |
+# GNU General Public License for more details. |
|
| 14 |
+# |
|
| 15 |
+# You should have received a copy of the GNU General Public License |
|
| 16 |
+# along with Bib2011. If not, see <http://www.gnu.org/licenses/>. |
|
| 17 |
+import subprocess |
|
| 18 |
+import logging |
|
| 19 |
+ |
|
| 20 |
+log = logging.getLogger('usbprinter')
|
|
| 21 |
+ |
|
| 22 |
+printer_definitions = [ |
|
| 23 |
+ {'idVendor': 0x04b8, 'idProduct': 0x0e03, 'cp858': 19},
|
|
| 24 |
+ {'idVendor': 0x04b8, 'idProduct': 0x0e20, 'cp858': 14},
|
|
| 25 |
+] |
|
| 26 |
+ |
|
| 27 |
+import usb.core |
|
| 28 |
+import usb.util |
|
| 29 |
+ |
|
| 30 |
+ |
|
| 31 |
+class USBPrinter (object): |
|
| 32 |
+ |
|
| 33 |
+ def __init__(self, fix_script=None): |
|
| 34 |
+ self.fix_script = fix_script |
|
| 35 |
+ self.ep_out = None |
|
| 36 |
+ self.ep_in = None |
|
| 37 |
+ self.device = None |
|
| 38 |
+ self.printer_config = {'idVendor': None, 'idProduct': None, 'cp858': None}
|
|
| 39 |
+ self.initialized = False |
|
| 40 |
+ self.initialize() |
|
| 41 |
+ |
|
| 42 |
+ def initialize(self, auto_retry=True): |
|
| 43 |
+ try: |
|
| 44 |
+ devs = usb.core.find(find_all=True) |
|
| 45 |
+ for d in devs: |
|
| 46 |
+ for candidate in printer_definitions: |
|
| 47 |
+ if candidate['idVendor'] == d.idVendor and candidate['idProduct'] == d.idProduct: |
|
| 48 |
+ self.printer_config = candidate |
|
| 49 |
+ self.device = d |
|
| 50 |
+ log.info(f'using printer device {d.idVendor:04x}:{d.idProduct:04x}')
|
|
| 51 |
+ if not self.device: |
|
| 52 |
+ log.error('Kein Drucker gefunden!')
|
|
| 53 |
+ return None |
|
| 54 |
+ |
|
| 55 |
+ cfg = self.device.get_active_configuration() |
|
| 56 |
+ interface_number = cfg[(0,0)].bInterfaceNumber |
|
| 57 |
+ |
|
| 58 |
+ if self.device.is_kernel_driver_active(interface_number): |
|
| 59 |
+ self.device.detach_kernel_driver(interface_number) |
|
| 60 |
+ |
|
| 61 |
+ |
|
| 62 |
+ self.device.set_configuration() |
|
| 63 |
+ |
|
| 64 |
+ intf = usb.util.find_descriptor(cfg, bInterfaceNumber = interface_number) |
|
| 65 |
+ |
|
| 66 |
+ endpoints = list(usb.util.find_descriptor(intf, find_all = True)) |
|
| 67 |
+ ep_out = None |
|
| 68 |
+ for e in endpoints: |
|
| 69 |
+ if (usb.util.endpoint_direction(e.bEndpointAddress) == |
|
| 70 |
+ usb.util.ENDPOINT_OUT): |
|
| 71 |
+ ep_out = e |
|
| 72 |
+ break |
|
| 73 |
+ ep_in = None |
|
| 74 |
+ for e in endpoints: |
|
| 75 |
+ if (usb.util.endpoint_direction(e.bEndpointAddress) == |
|
| 76 |
+ usb.util.ENDPOINT_IN): |
|
| 77 |
+ ep_in = e |
|
| 78 |
+ break |
|
| 79 |
+ |
|
| 80 |
+ assert ep_out is not None and ep_in is not None, "Error getting a USB endpoint address" |
|
| 81 |
+ self.ep_out = ep_out |
|
| 82 |
+ self.ep_in = ep_in |
|
| 83 |
+ self.initialized = True |
|
| 84 |
+ except usb.core.USBError as e: |
|
| 85 |
+ log.error(str(e)) |
|
| 86 |
+ if self.fix_script: |
|
| 87 |
+ log.error('Running permission fixer script')
|
|
| 88 |
+ subprocess.call(self.fix_script, shell=True) |
|
| 89 |
+ if self.fix_script and auto_retry: |
|
| 90 |
+ self.initialize(auto_retry=False) |
|
| 91 |
+ |
|
| 92 |
+ def get_endpoints(self): |
|
| 93 |
+ if self.initialized: |
|
| 94 |
+ return self.ep_out, self.ep_in |
|
| 95 |
+ else: |
|
| 96 |
+ log.error('initialization error')
|
|
| 97 |
+ return None, None |
|
| 98 |
+ |
|
| 99 |
+ def write(self, data): |
|
| 100 |
+ # Es gab Probleme mit langen Belegen, daher senden wir die Daten jetzt in kleineren Häppchen |
|
| 101 |
+ i = 0 |
|
| 102 |
+ while i < len(data): |
|
| 103 |
+ self.ep_out.write(data[i:i + 1024]) |
|
| 104 |
+ i += 1024 |
|
| 105 |
+ |
|
| 106 |
+ def read(self) -> bytes: |
|
| 107 |
+ data = self.ep_in.read(1024, timeout=200) |
|
| 108 |
+ if data: |
|
| 109 |
+ return data |
|
| 110 |
+ return b'' |
|
| 111 |
+ |
|
| 112 |
+ |
|
| 113 |
+ def __del__(self): |
|
| 114 |
+ self.terminate() |
|
| 115 |
+ |
|
| 116 |
+ def terminate(self): |
|
| 117 |
+ if self.initialized: |
|
| 118 |
+ usb.util.dispose_resources(self.device) |
|
| 119 |
+ self.initialized = False |
|
| 120 |
+ |
|
| 121 |
+ |
|
| 122 |
+ |
|
| 123 |
+ |
|
| 124 |
+if __name__ == '__main__': |
|
| 125 |
+ prn = USBPrinter() |
|
| 126 |
+ (fd_out, fd_in) = prn.get_endpoints() |
|
| 127 |
+ # RESET |
|
| 128 |
+ fd_out.write('\x1b\x40')
|
|
| 129 |
+ # Code page |
|
| 130 |
+ fd_out.write('\x1b\x74\x0e')
|
|
| 131 |
+ fd_out.write('Hallo äöü\n\n\n'.encode('cp858'))
|
|
| 132 |
+ prn.write('\x1d\x72\x01')
|
|
| 133 |
+ status = prn.read() |
|
| 134 |
+ if status: |
|
| 135 |
+ print(f'received {len(status)} bytes!')
|
|
| 136 |
+ print(f"{status[0]:08b}")
|
|
| 137 |
+ |
|
| 138 |
+ |
|
| 139 |
+ |
|
| 140 |
+ |
|
| 141 |
+ |
|
| 142 |
+ |