In HTML5 we use the <hr> tag to draw a horizontal line. Now from Javascript I want to post data to a Python project for printing on a thermal printer :
let data = "Prestation : steak \n";
data += "Quantite : 3 \n";
data += // here I want to draw a horizontal line
data += "Prestation : coca \n";
data += "Quantite : 1";
$.post("localhost:5000/print", JSON.stringify({"printer": "some_printer_name", "payload": data}), function(response) {
...
});
The Python codes :
file printing.py :
import logging
import win32print
import win32ui
import win32con
class Printing(object):
printer = None
# Constructor
def __init__(self, printer):
self.printer = printer
@staticmethod
def print(printer_name, text):
try:
printer_handle = win32print.OpenPrinter(printer_name)
dc = win32ui.CreateDC()
dc.CreatePrinterDC(printer_name)
dc.StartDoc(text)
dc.StartPage()
dc.TextOut(10, 10, text)
dc.EndPage()
dc.EndDoc()
win32print.ClosePrinter(printer_handle)
return True
except:
logging.info('Error occured during printing')
return False
finally:
pass
file app.py running in background process waitinf for any call from the Javascript side :
import logging
from flask import Flask, request, jsonify, make_response
from printing import Printing
app = Flask(__name__)
@app.route('/')
def index():
return 'If you see this, it means the printer service is up and running !'
@app.route('/print', methods=["POST"])
def print():
printer_name = request.json['printer']
data = request.json['payload']
result = Printing.print(printer_name, data)
if result:
response = make_response(
jsonify(
{"message": str("Printing success")}
),
200,
)
response.headers["Content-Type"] = "application/json"
return response
else:
response = make_response(
jsonify(
{"message": str("Printing failed"), "severity": "error"}
),
500,
)
response.headers["Content-Type"] = "application/json"
return response
if __name__ == '__main__':
app.run(debug=True)
So how to code the horizontal line ?
Check below modified JavaScript code to include a string of dash characters (
"-") that represents the line. This string is part of the data sent to the Python backend via a POST request. The Python code, using thewin32printlibrary, receives this data and sends it to the specified printer. Hope it helps