I never used Python ESC/POS before and I started doing a restaurant app that has print-order function running on my VPS as a docker container.
My printing is working fine, currently I'm trying to avoid port forwarding to print orders, but I think that cannot be done using Network Printer.
from escpos.printer import Network
...
@app.route("/print-order", methods=["POST"])
def print_order():
order_id = request.form.get('order-id')
# I used Network with port forwarding
with open('config.yaml', 'r') as file:
config = yaml.safe_load(file)
PRINTER_IP = config['user_ip']
PRINTER_PORT = 9100
printer = Network(PRINTER_IP, PRINTER_PORT)
try:
conn = get_db_connection()
cur = conn.cursor()
cur.execute("SELECT id, phone, date, total, items FROM orders WHERE id = ?", (order_id,))
order = cur.fetchone()
conn.close()
printer.set(align="center", font=1, bold=True, custom_size=True, width=2, height=2, smooth=True)
printer.text(f"Example Restaurant")
...
printer.cut()
except requests.exceptions.RequestException as e:
logger.debug(f"Failed to print order: {e}")
printer.close()
return redirect('/orders')
print("Print successful!")
printer.close()
return redirect('/ordering')
Do you have an idea that how can I make my app automatically detect like an Usb Printer when its connected to the user's device? Or do I have any other options for Network printing?
The goal would be to make the app more user friendly, so people don't need to setup either Port Forwarding (for Networking) or VID/PID (for Usb).