From 6292687003e0e21cd9c9ebe5e6c331cf7a4c631d Mon Sep 17 00:00:00 2001 From: Alexander Loechel Date: Thu, 24 Sep 2020 00:56:01 +0200 Subject: [PATCH] gs --- setup.py | 1 + src/lmu/localprintservice/views.py | 33 ++++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/setup.py b/setup.py index 7b19560..347f348 100644 --- a/setup.py +++ b/setup.py @@ -41,6 +41,7 @@ "pyramid_openapi3", "waitress", "pywin32; sys_platform == 'win32'", + "ghostscript; sys_platform == 'win32'", "pycups; sys_platform != 'win32'", ], extras_require={ diff --git a/src/lmu/localprintservice/views.py b/src/lmu/localprintservice/views.py index 772847e..975904b 100644 --- a/src/lmu/localprintservice/views.py +++ b/src/lmu/localprintservice/views.py @@ -4,8 +4,10 @@ from pyramid.view import notfound_view_config import base64 +import ghostscript import glob import json +import locale import os.path import subprocess import sys @@ -47,6 +49,19 @@ def getGhostScript(): else: breakpoint() + +def getGhostPrint(): + if sys.platform == "win32": + p = Path("/Program Files") + gs = [f for f in p.rglob("gsprint.exe")] + if len(gs) == 0: + raise NotImplementedError("No GhostView / GhostPrint Dependency found.") + elif len(gs) == 1: + return str(gs[0]) + else: + breakpoint() + + options = { "media": "A4", "InputSlot": "Tray2", @@ -126,16 +141,22 @@ def print_pdf_put_view(request): printer = request.params.get("printer") if request.params.get("printer") else get_default_printer() if not request.body: return Response("Bad Request", status=400) - #pdf_file = request.params.get("pdf_file") with tempfile.TemporaryDirectory(suffix="lmu.localprintservice") as dir: with open(os.path.join(os.path.abspath(dir), "file_to_print.pdf"), "w+b") as pdf: pdf.write(request.body) - files_to_print = glob.glob(os.path.join(os.path.abspath(dir), "*.pdf")) + files_to_print = glob.glob(os.path.join(os.path.abspath(dir), "*.pdf"))[0].replace('\\\\', '\\') if sys.platform == "win32": - args = f'"{getGhostScript()}" -sDevice=mswinpr2 -dBATCH -dNOPAUSE -dFitPage -sOutputFile="%printer%{printer}" ' - ghostscript = args + os.path.join(files_to_print[0]).replace("\\", "\\\\") - print(ghostscript) - subprocess.call(ghostscript, shell=True) + args = [ + "-dPrinted", "-dBATCH", "-dNOSAFER", "-dNOPAUSE", "-dNOPROMPT" + "-q", + "-dNumCopies#1", + "-sDEVICE#mswinpr2", + f'-sOutputFile#"%printer%{printer}"', + f'"{files_to_print}"' + ] + encoding = locale.getpreferredencoding() + args = [a.encode(encoding) for a in args] + ghostscript.Ghostscript(*args) else: import cups conn = cups.Connection()