Created
June 16, 2016 14:34
-
-
Save russelldavies/c08edd1077c757e284c0993af721ee1e to your computer and use it in GitHub Desktop.
Zebra CUPS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from flask import Flask, request, abort | |
import cups | |
import requests | |
import os | |
import tempfile | |
app = Flask(__name__) | |
@app.route('/', methods=['POST']) | |
def print_job(): | |
try: | |
zpl_url = request.form.get('zpl_url') | |
zpl_data = request.form.get('zpl_data') | |
printer = request.form['printer_name'] | |
if zpl_url: | |
response = requests.get(zpl_url) | |
response.raise_for_status() | |
file_print(printer, response.content) | |
elif zpl_data: | |
file_print(printer, zpl_data) | |
except (RuntimeError, cups.IPPError, requests.exceptions.RequestException) as error: | |
abort(400) | |
return '', 201 | |
def stream_print(printer, zpl_data): | |
cups_connection = cups.Connection() | |
job_id = cups_connection.createJob(printer, '', {}) | |
cups_connection.startDocument(printer, job_id, '', cups.CUPS_FORMAT_RAW, 1) | |
cups_connection.writeRequestData(zpl_data, len(zpl_data)) | |
cups_connection.finishDocument(printer) | |
def file_print(printer, zpl_data): | |
temp = tempfile.NamedTemporaryFile() | |
try: | |
temp.write(zpl_data) | |
temp.seek(0) | |
cups_connection = cups.Connection() | |
cups_connection.printFile(printer, temp.name, 'label', {}) | |
finally: | |
temp.close() | |
if __name__ == "__main__": | |
#app.debug = True | |
app.run(host='0.0.0.0', port=8000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment