forked from hansegucker/documento
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_server.py
128 lines (102 loc) · 3.95 KB
/
print_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import json
import subprocess
import time
import requests
from barcode import Code128
from barcode.writer import ImageWriter
from escpos.printer import Usb
# Load config
with open("config.json") as f:
config = json.load(f)
# Read vendor and product IDs from config and setup printer
raw_vendor = config.get("vendor")
raw_product = config.get("product")
vendor = int(f"0x{raw_vendor}", 16)
product = int(f"0x{raw_product}", 16)
print(f"Printer: Vendor: {raw_vendor} ({vendor}); Product: {raw_product} ({product})")
printer = Usb(vendor, product)
def print_info(document, categories=None):
"""Print report info page."""
if categories is None:
categories = {}
if document["category"]:
current_category = categories[document["category"]]
cat_list = [current_category]
while current_category["parent"]:
cat_list.insert(0, categories[current_category["parent"]])
current_category = categories[current_category["parent"]]
else:
cat_list = []
printer.set(font="a", align="center")
printer.text("DOCUMENTO - Archivetikett\n")
printer.set(font="a", double_height=True, align="right", underline=False)
printer.text("NR. {}\n".format(document["id"]))
printer.set(font="b", align="left")
printer.text("Titel:\n")
printer.set(font="a", double_height=True, align="left", underline=True)
printer.textln(document["name"])
printer.set(font="b", align="left")
printer.text("Zeitpunkt der Archivierung:\n")
printer.set(font="a", double_height=False, align="left", underline=False)
printer.textln(document["added_at"])
printer.set(font="b", align="left")
printer.text("Kategorie:\n")
printer.set(font="a", double_height=False, align="center", underline=False)
cat_names = ["[" + cat["name"] + "]\n" for cat in cat_list]
printer.text("V\n".join(cat_names))
printer.set(font="b", align="left")
printer.text("Barcode:\n")
barcode = document["barcode"]
writer = ImageWriter()
writer.set_options(
{"module_width": 0.4, "module_height": 5.0, "text_distance": 3.0}
)
code = Code128(barcode, writer=writer).render()
printer.image(code, center=True)
printer.ln(3)
printer.control("LF")
base_url = config.get("url")
login_url = base_url + "/api/auth/login/"
jobs_url = base_url + "/api/print_jobs/"
categories_url = base_url + "/api/categories/"
while True:
# Get auth token
r = requests.post(
login_url,
json={"username": config.get("username"), "password": config.get("password")},
)
token = r.json()["token"]
headers = {"Authorization": f"Token {token}"}
# Fetch print jobs
jobs = requests.get(jobs_url, headers=headers).json()
# Fetch categories
categories = requests.get(categories_url, headers=headers).json()
categories = {category["id"]: category for category in categories}
for job in jobs:
job_id = job["id"]
report = job["report"]
document = job["document"]
document_id = document["id"]
document_name = document["name"]
print(f"Job {job_id}: {report}; Document {document_id}: {document_name}")
printed = False
if report == "barcode_label":
label_url = base_url + document["barcode_label"]
# Download barcode label
r = requests.get(label_url)
with open("barcode-tmp.pdf", "wb") as f:
f.write(r.content)
# Send barcode label to printer
subprocess.Popen(
["lp", "-d", config.get("barcode_printer"), "barcode-tmp.pdf"],
stderr=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
)
printed = True
elif report == "info_page":
print_info(document, categories)
printed = True
# Mark print job as printed
if printed:
r3 = requests.post(f"{jobs_url}{job_id}/mark_as_printed/", headers=headers)
time.sleep(3)