print server for cups (generic pdf set to A6)
This commit is contained in:
parent
20fd5642b0
commit
992a81425c
2 changed files with 78 additions and 11 deletions
63
appsocket.py
Executable file
63
appsocket.py
Executable file
|
@ -0,0 +1,63 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import tspl
|
||||
import socket
|
||||
import logging
|
||||
import tempfile
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
||||
UEL = b'\x1b%-12345X'
|
||||
ENTER_PDF = b'@PJL ENTER LANGUAGE = PDF'
|
||||
|
||||
def accept_one_job(sock):
|
||||
def read_more_data(data):
|
||||
new_data = sock.recv(1048576)
|
||||
if not len(new_data):
|
||||
logging.error('Received unexpected EOF from client')
|
||||
return
|
||||
data.extend(new_data)
|
||||
|
||||
def consume_up_to(data, marker):
|
||||
while not marker in data:
|
||||
read_more_data(data)
|
||||
|
||||
pos = data.index(marker)
|
||||
before_marker = data[:pos]
|
||||
after_marker = data[pos+len(marker):]
|
||||
return before_marker, after_marker
|
||||
|
||||
data = bytearray()
|
||||
|
||||
_, data = consume_up_to(data, UEL)
|
||||
_, data = consume_up_to(data, ENTER_PDF)
|
||||
pdf_data, _ = consume_up_to(data, UEL)
|
||||
pdf_data = pdf_data.lstrip()
|
||||
|
||||
with tempfile.NamedTemporaryFile(suffix=".pdf") as pdffile:
|
||||
pdffile.write(pdf_data)
|
||||
pdffile.flush()
|
||||
tspl.print_pdf(pdffile.name)
|
||||
|
||||
logging.info('Job complete')
|
||||
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
sock.bind(('0.0.0.0', 9100))
|
||||
sock.listen()
|
||||
|
||||
class ConnectionClosed(Exception):
|
||||
pass
|
||||
|
||||
while True:
|
||||
conn, peer = sock.accept()
|
||||
logging.info('New connection from %s' % peer[0])
|
||||
|
||||
try:
|
||||
accept_one_job(conn)
|
||||
except ConnectionClosed:
|
||||
pass
|
||||
except Exception as e:
|
||||
logging.exception(e)
|
||||
finally:
|
||||
conn.close()
|
|
@ -25,7 +25,7 @@ def convert_pdf_scaled(pdfname, max_width, max_height):
|
|||
args = ['-scale-to-x', str(max_width), '-scale-to-y', str(max_height)]
|
||||
im = convert_pdf(pdfname, args)
|
||||
|
||||
assert im.size[0] <= max_width
|
||||
assert im.size[0] <= max_width + 1
|
||||
assert im.size[1] <= max_height
|
||||
|
||||
return im
|
||||
|
@ -35,20 +35,24 @@ def label_to_command(image, labelwidth_mm=100, labelheight_mm=150, dpi=203):
|
|||
labelwidth = labelwidth_mm / 25.4 * dpi
|
||||
labelheight = labelheight_mm / 25.4 * dpi
|
||||
|
||||
assert im.size[0] <= labelwidth
|
||||
assert im.size[1] <= labelheight
|
||||
assert image.size[0] <= labelwidth + 5
|
||||
assert image.size[1] <= labelheight + 5
|
||||
|
||||
paste_x = (labelwidth - im.size[0]) // 2
|
||||
paste_y = (labelheight - im.size[1]) // 2
|
||||
row_bytes = (im.size[0] + 7) // 8
|
||||
paste_x = (labelwidth - image.size[0]) // 2
|
||||
paste_y = (labelheight - image.size[1]) // 2
|
||||
row_bytes = (image.size[0] + 7) // 8
|
||||
|
||||
command = b"SIZE %d mm\r\nCLS\r\nBITMAP %d,%d,%d,%d,0," % (labelwidth_mm, paste_x, paste_y, row_bytes, im.size[1])
|
||||
command = b"SIZE %d mm\r\nCLS\r\nBITMAP %d,%d,%d,%d,0," % (labelwidth_mm, paste_x, paste_y, row_bytes, image.size[1])
|
||||
command += image.tobytes()
|
||||
command += b"\r\nPRINT 1,1\r\n"
|
||||
return command
|
||||
|
||||
|
||||
im = convert_pdf_scaled("test.pdf", 800, 1200)
|
||||
def print_pdf(filename):
|
||||
im = convert_pdf_scaled(filename, 800, 1200)
|
||||
cmd = label_to_command(im)
|
||||
with open("/dev/usb/lp0", "r+b") as prn:
|
||||
prn.write(cmd)
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
print_pdf(sys.argv[1])
|
Loading…
Add table
Reference in a new issue