From 18c51358aa5fa3cbffdaab9013413eb8deac8c31 Mon Sep 17 00:00:00 2001 From: Ramon Poca Date: Thu, 13 Dec 2018 08:01:05 -0800 Subject: [PATCH 1/4] Add Win32Raw printer to available printers --- src/escpos/printer.py | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/escpos/printer.py b/src/escpos/printer.py index d7fc6ac..a8905d0 100644 --- a/src/escpos/printer.py +++ b/src/escpos/printer.py @@ -334,3 +334,51 @@ class Dummy(Escpos): def close(self): pass + + +_WIN32PRINT=False +try: + import win32print + _WIN32PRINT=True +except ImportError: + pass + +if _WIN32PRINT: + class Win32Raw(Escpos): + def __init__(self, printer_name = None, profile=None, magic_encode_args=None, **kwargs): + super().__init__(profile=profile, magic_encode_args=magic_encode_args, **kwargs) + if printer_name is not None: + self.printer_name = printer_name + else: + self.printer_name = win32print.GetDefaultPrinter() + self.hPrinter = None + + + def open(self, job_name="python-escpos"): + if self.printer_name is None: + raise Exception("Printer not found") + self.hPrinter = win32print.OpenPrinter (self.printer_name) + self.current_job = win32print.StartDocPrinter (self.hPrinter, 1, (job_name, None, "RAW")) + win32print.StartPagePrinter (self.hPrinter) + + def close(self): + if not self.hPrinter: + return + win32print.EndPagePrinter (self.hPrinter) + win32print.EndDocPrinter (self.hPrinter) + win32print.ClosePrinter (self.hPrinter) + self.hPrinter = None + + def _raw(self, msg): + """ Print any command sent in raw format + + :param msg: arbitrary code to be printed + :type msg: bytes + """ + if self.printer_name is None: + raise Exception("Printer not found") + if self.hPrinter is None: + raise Exception("Printer job not opened") + win32print.WritePrinter (self.hPrinter, msg) + + From d5b9d99093f0557f46d87b9508ef9b1266cb3631 Mon Sep 17 00:00:00 2001 From: Ramon Poca Date: Mon, 31 Dec 2018 10:43:31 +0100 Subject: [PATCH 2/4] Update AUTHORS --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index 6000080..7c5ef60 100644 --- a/AUTHORS +++ b/AUTHORS @@ -23,6 +23,7 @@ Nathan Bookham Patrick Kanzler primax79 Qian Linfeng +Ramon Poca reck31 Renato Lorenzi Romain Porte From 73fff6291dd7133667f8d71915a7124f82ea4ea8 Mon Sep 17 00:00:00 2001 From: Ramon Poca Date: Mon, 31 Dec 2018 13:57:53 +0100 Subject: [PATCH 3/4] Fix syntax --- src/escpos/printer.py | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/escpos/printer.py b/src/escpos/printer.py index a8905d0..ecb708f 100644 --- a/src/escpos/printer.py +++ b/src/escpos/printer.py @@ -336,16 +336,16 @@ class Dummy(Escpos): pass -_WIN32PRINT=False +_WIN32PRINT = False try: - import win32print - _WIN32PRINT=True + import win32print + _WIN32PRINT = True except ImportError: - pass + pass if _WIN32PRINT: class Win32Raw(Escpos): - def __init__(self, printer_name = None, profile=None, magic_encode_args=None, **kwargs): + def __init__(self, printer_name=None, profile=None, magic_encode_args=None, **kwargs): super().__init__(profile=profile, magic_encode_args=magic_encode_args, **kwargs) if printer_name is not None: self.printer_name = printer_name @@ -353,22 +353,21 @@ if _WIN32PRINT: self.printer_name = win32print.GetDefaultPrinter() self.hPrinter = None - def open(self, job_name="python-escpos"): if self.printer_name is None: raise Exception("Printer not found") - self.hPrinter = win32print.OpenPrinter (self.printer_name) - self.current_job = win32print.StartDocPrinter (self.hPrinter, 1, (job_name, None, "RAW")) - win32print.StartPagePrinter (self.hPrinter) + self.hPrinter = win32print.OpenPrinter(self.printer_name) + self.current_job = win32print.StartDocPrinter(self.hPrinter, 1, (job_name, None, "RAW")) + win32print.StartPagePrinter(self.hPrinter) def close(self): if not self.hPrinter: return - win32print.EndPagePrinter (self.hPrinter) - win32print.EndDocPrinter (self.hPrinter) - win32print.ClosePrinter (self.hPrinter) + win32print.EndPagePrinter(self.hPrinter) + win32print.EndDocPrinter(self.hPrinter) + win32print.ClosePrinter(self.hPrinter) self.hPrinter = None - + def _raw(self, msg): """ Print any command sent in raw format @@ -379,6 +378,4 @@ if _WIN32PRINT: raise Exception("Printer not found") if self.hPrinter is None: raise Exception("Printer job not opened") - win32print.WritePrinter (self.hPrinter, msg) - - + win32print.WritePrinter(self.hPrinter, msg) From 2886075ce9feabfc76ffa7238e6fcb294cc327ca Mon Sep 17 00:00:00 2001 From: Ramon Poca Date: Mon, 31 Dec 2018 14:24:10 +0100 Subject: [PATCH 4/4] Fix initializer mess --- src/escpos/printer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/escpos/printer.py b/src/escpos/printer.py index ecb708f..f1c6145 100644 --- a/src/escpos/printer.py +++ b/src/escpos/printer.py @@ -345,8 +345,8 @@ except ImportError: if _WIN32PRINT: class Win32Raw(Escpos): - def __init__(self, printer_name=None, profile=None, magic_encode_args=None, **kwargs): - super().__init__(profile=profile, magic_encode_args=magic_encode_args, **kwargs) + def __init__(self, printer_name=None, *args, **kwargs): + Escpos.__init__(self, *args, **kwargs) if printer_name is not None: self.printer_name = printer_name else: