【问题标题】:Node Escpos print image on second try节点 Escpos 在第二次尝试时打印图像
【发布时间】:2019-08-29 13:48:39
【问题描述】:

您好,我正在创建一个快速服务,可根据 http 请求打印到 pos 打印机。问题是我的解决方案不会在第一次打印时打印图像,但在第二次打印时会打印,依此类推。

我正在使用https://github.com/song940/node-escpos

这是我的 server.js

var print = require('./print')
var express = require('express')
var cors = require('cors')
var bodyParser = require('body-parser');

var app = express();

var corsOptions = {
    origin: '*',
    optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
  }

app.get('/print', cors(corsOptions), (req, res) => {
    print.demoPrint();
    return res.send(req.body);
});

app.listen(3000, () => {
    console.log('Express server is started')
});

这是我的 print.js

const escpos = require('escpos');
const device = new escpos.USB(0x1504,0x003d);
const options = { encoding: "GB18030"};
const printer = new escpos.Printer(device);
const fs = require('fs')

const printSettings = JSON.parse(fs.readFileSync('printConfig.json', 'utf8'));

exports.demoPrint = () => {
    device.open(function() {
        printSettings.forEach((currentLine) => {
            if(currentLine.printType === "text") {
                console.log("PRINTING TEXT");
                printer
                .font(currentLine.font)
                .align(currentLine.align)
                .style('bu')
                .size(currentLine.size, currentLine.size)
                .text(currentLine.text)
            }
            else if(currentLine.printType === "image") {
                escpos.Image.load(currentLine.path, (image) => {
                    console.log("PRINTING IMAGE");
                    printer
                        .align(currentLine.align)
                        .size(currentLine.size, currentLine.size)
                        .image(image)
                });
            }
            else if(currentLine.printType === "barcode") {
                console.log("PRINTING BARCODE");
                printer
                .align(currentLine.align)
                .barcode(currentLine.text, 'CODE39', {
                    height: 64,
                    font: 'B'
                });
            }
        });
        printer
        .text('\n')
        .text('\n')
        .cut()
        .close();
    });
};

【问题讨论】:

    标签: express escpos


    【解决方案1】:

    由于尚未回答此问题,我将提供对我有用的解决方案。问题是图像在我第一次提交请求时开始加载。在我第二次提交请求时,图像已经加载并成功打印。

    我通过添加一个在加载图像之前不允许打印的检查解决了这个问题。

    【讨论】:

    • 您能分享一下“支票”的样子吗?有一个 load() 函数 escpos.ESCPOSImage.load()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-09-26
    • 1970-01-01
    • 2013-08-18
    • 2020-12-11
    • 1970-01-01
    • 2021-03-12
    • 2019-08-30
    相关资源
    最近更新 更多