Search code examples
javascriptnode.jsprintingthermal-printer

How do i print an lbl file using Nodejs?


I'm currently working on trying to print a .lbl file using Nodejs. I've been digging around on npm and google but, I'm not quite sure what is the best route to go. I was thinking of modifying one the npm to incorporate the printers i need. However, if there is a general NPM out there that would be better.

Some background info for anyone interested: I would like to use the printer I have selected from the dropdown. Once select call the .lbl file and print the correct quantity from a box next to my printer drop down.

I would need to replace some values on the label with various things i have selected elsewhere on my screen before it prints.

I am using a Datamax-O'neil printer here but there may be different printers at other places.

My questions to you: Is there an node packages anyone might recommend? I have looked at node-printer, cordova-plugin-thermal-printer, and dymo along with more. The listed three seem be sort of in the right direction.

Do you know of any example I might be able to look at that might point me the right direction?

Sorry if this a duplicate question, I appreciate all the help and thank you in advance.

Anthony


Solution

  • I ended up doing this. Though currently it is still a work in progress but it will give anyone who may be experiencing the same issue some help or at least maybe give you a direction of some kind. I used this tool to help me with the replacement part. Hope it helps.

    const cmd = require('node-cmd'),
      fs = require('fs');
    
    print: (entity) => {
    pool.open(cn, (err, conn) => {
        let quantityToPrint = entity.quantityToPrint; //Get Quantity from inputbox
    
        let getNeededData = () => {
            return new Promise((resolve, reject) => {
                sql = `select * from Table where Blah = 'BlahBlah'`
    
                conn.query(sql, (err, data) => {
                    var obj = {};
                    obj.ReplacementValue1 = (data.length > 0 && typeof data[0].ReplacementValue1 !== 'undefined') ? data[0].ReplacementValue1 : '';
                    obj.ReplacementValue2 = (data.length > 0 && typeof data[0].ReplacementValue2 !== 'undefined') ? data[0].ReplacementValue2 : '';
                    obj.ReplacementValue3 = (data.length > 0 && typeof data[0].ReplacementValue3 !== 'undefined') ? data[0].ReplacementValue3 : '';
                    resolve(obj);
                })
            })
        }
        let getPrintLabel = (obj) => {
            return new Promise((resolve, reject) => {
    
                let printer = entity.printer, //Printer I call from a drop down
                    labelFile = 'labels/lablefile.lbl',
                    inputLine;
                var filename = 'tempLabel_' + new Date().getTime() + '.lbl';
                var gvOutFile = 'labels/temp/templabel_' + new Date().getTime() + '.lbl';
    
                fs.createReadStream(labelFile).pipe(fs.createWriteStream(gvOutFile, { options: { flags: 'r+', mode: 666, encoding: 'utf8' } }));
                setTimeout(function () {
                    var data = fs.readFileSync(gvOutFile, 'utf8');
                    var newValue = data.replace(/(\[ValueToReplace1\])/gim, month + '/' + day + '/' + years)
                        .replace(/(\[ValueToReplace1\])/gim, obj.ReplacementValue1)
                        .replace(/(\[ValueToReplace2\])/gim, obj.ReplacementValue2)
                        .replace(/(\[ValueToReplace3\])/gim, obj.ReplacementValue3)
                        //NOTE, You may need to change how you are replacing. I replace brackets in my lbl file.
    
                    fs.writeFileSync(gvOutFile, newValue, { options: { flags: 'r+', mode: 666, encoding: 'utf8' } });
                    cmd.run('lp -s -c -d ' + printer + ' ' + gvOutFile);
                }, (2 * 1000))// 2 Seconds
    
                resolve();
            })
        }
        for (var item = 0, x = quantityToPrint; item < x; item++) {
            getNeededData().then(getPrintLabel).then((data) => {
                pool.close(function () { });
                entity.res.json(data);
            })
        }
    })
    }