如何在热敏打印机的纸上打印水平线?

问题描述 投票:0回答:1

在 HTML5 中,我们使用

<hr>
标签来绘制水平线。现在,从
Javascript
我想将数据发布到 Python 项目,以便在 热敏打印机 上打印:

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) {
    ...
});

Python 代码:

文件打印.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

在后台进程中运行的文件 app.py waitinf 等待来自 Javascript 端的任何调用:

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)

那么水平线如何编码?

javascript python jquery thermal-printer
1个回答
0
投票

检查下面修改后的 JavaScript 代码以包含代表该行的破折号字符串 (

"-"
)。该字符串是通过 POST 请求发送到 Python 后端的数据的一部分。 Python 代码使用
win32print
库接收此数据并将其发送到指定的打印机。希望有帮助

let data = "Prestation : steak \n";
data += "Quantite : 3 \n";
data += "--------------------------------\n"; // This will be your horizontal line
data += "Prestation : coca \n";
data += "Quantite : 1";

$.post("http://localhost:5000/print", JSON.stringify({"printer": "some_printer_name", "payload": data}), function(response) {
    // Handle response
});
© www.soinside.com 2019 - 2024. All rights reserved.