Skip to content

Commit

Permalink
Windows Printing
Browse files Browse the repository at this point in the history
  • Loading branch information
loechel committed Sep 23, 2020
1 parent 9c4f185 commit 8fcc149
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/lmu/localprintservice/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pyramid.config import Configurator

import os.path
import sys


def main(global_config, **settings):
Expand Down
10 changes: 5 additions & 5 deletions src/lmu/localprintservice/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ paths:
type: string
example: "Printer 1"

/printpdf:
/print:
post:
summary: Silent Print of the provided PDF on designated printer
requestBody:
Expand All @@ -42,8 +42,8 @@ paths:
type: string
format: binary
responses:
"201":
description: Created
"202":
description: Accepted
"400":
description: Bad Request
put:
Expand All @@ -62,7 +62,7 @@ paths:
type: string
format: binary
responses:
"201":
description: Created
"202":
description: Accepted
"400":
description: Bad Request
4 changes: 2 additions & 2 deletions src/lmu/localprintservice/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ def includeme(config):
config.add_static_view("static", "static", cache_max_age=3600)
# API Routes
config.add_route("get_printer_route", "/api/v1/printers", request_method="GET")
config.add_route("print_pdf_route_post", "/api/v1/printpdf", request_method="POST")
config.add_route("print_pdf_route_put", "/api/v1/printpdf", request_method="PUT")
config.add_route("print_pdf_route_post", "/api/v1/print", request_method="POST")
config.add_route("print_pdf_route_put", "/api/v1/print", request_method="PUT")
30 changes: 23 additions & 7 deletions src/lmu/localprintservice/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pathlib import Path
from pyramid.response import Response
from pyramid.view import view_config
from pyramid.view import notfound_view_config
Expand All @@ -6,6 +7,7 @@
import glob
import json
import os.path
import subprocess
import sys
import tempfile

Expand Down Expand Up @@ -34,6 +36,17 @@ def get_default_printer():
return default_printer


def getGhostScript():
if sys.platform == "win32":
p = Path("/Program Files")
gs = [f for f in p.rglob("gswin64c.exe")] + [f for f in p.rglob("gswin32c.exe")]
if len(gs) == 0:
raise NotImplementedError("No GhostScript Dependency found.")
elif len(gs) == 1:
return str(gs[0])
else:
breakpoint()

options = {
"media": "A4",
"InputSlot": "Tray2",
Expand Down Expand Up @@ -95,14 +108,16 @@ def print_pdf_post_view(request):
counter += 1
files_to_print = glob.glob(os.path.join(os.path.abspath(dir), "*.pdf"))
if sys.platform == "win32":
from win32 import win32print
return win32print.GetDefaultPrinter()
args = f'"{getGhostScript()}" -sDevice=mswinpr2 -dBATCH -dNOPAUSE -dFitPage -sOutputFile="%printer%{printer}" '
for f in files_to_print:
ghostscript = args + os.path.join(files_to_print[0]).replace("\\", "\\\\")
subprocess.call(ghostscript, shell=True)
else:
import cups
conn = cups.Connection()
printer = "HP_LaserJet_400_colorMFP_M475dn"
conn.printFiles(printer, files_to_print, "Test", options)
return Response("Created", status=201)
return Response("Accepted", status=202)



Expand All @@ -117,14 +132,15 @@ def print_pdf_put_view(request):
pdf.write(request.body)
files_to_print = glob.glob(os.path.join(os.path.abspath(dir), "*.pdf"))
if sys.platform == "win32":
from win32 import win32print
return win32print.GetDefaultPrinter()
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)
else:
import cups
conn = cups.Connection()
breakpoint()
conn.printFiles(printer, files_to_print, "Test", options)
return Response("Created", status=201)
return Response(status=202)


@notfound_view_config(renderer="json")
Expand Down

0 comments on commit 8fcc149

Please sign in to comment.