Узел Escpos распечатать изображение со второй попытки - PullRequest
0 голосов
/ 08 апреля 2019

Здравствуйте, я создаю экспресс-сервис, который печатает на почтовый принтер по запросу http.Проблема в том, что мое решение не печатает изображения на первом отпечатке, а на втором и так далее.

Я использую 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();
    });
};
...