最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - is it possible to print an image with node.js? - Stack Overflow

programmeradmin1浏览0评论

Ok, so I'm trying to print from a webpage (the typical "print" button, but I don't want the print dialog to appear) so I decided to use my already existing node.js backend to do the task (mainly because printing from browser is nearly impossible without the printing dialog).

I found the node-printer () module, and it works great, but only with text. I tried to send RAW data, but what it does is printing the raw characters. What I actually need is to print a logo, along with some turn information (this is for a customer care facility).

Also, the printer must be installed locally, so I can't use IPP.

Is there any way to print an image, or a combination of images and text with node.js? can it be done through node-printer or is there another way?

Ok, so I'm trying to print from a webpage (the typical "print" button, but I don't want the print dialog to appear) so I decided to use my already existing node.js backend to do the task (mainly because printing from browser is nearly impossible without the printing dialog).

I found the node-printer (https://github.com/tojocky/node-printer) module, and it works great, but only with text. I tried to send RAW data, but what it does is printing the raw characters. What I actually need is to print a logo, along with some turn information (this is for a customer care facility).

Also, the printer must be installed locally, so I can't use IPP.

Is there any way to print an image, or a combination of images and text with node.js? can it be done through node-printer or is there another way?

Share Improve this question asked May 2, 2014 at 22:21 ezabawezabaw 6452 gold badges8 silver badges21 bronze badges 4
  • node-printer calls lpr on posix with hardcoded arguments, so I don't think you will be able to print images with it. – Lennart Commented May 2, 2014 at 22:35
  • Are you trying to print to a printer that the client browser is connected to? Or print to a printer that the node.js server is connected to? – jfriend00 Commented May 2, 2014 at 22:36
  • node.js server will be running on the client machine also (is a system for printing turns) – ezabaw Commented May 2, 2014 at 22:56
  • 1 If you create local web app may be will useful to switch on node-webkit... – 3y3 Commented May 2, 2014 at 23:38
Add a comment  | 

3 Answers 3

Reset to default 8

I ended calling an exe to do the work for me. I use a child_process to call printhtml, which does all the printing work. My code ended this way:

var exec = require('child_process').exec;
exec('printhtml.exe file=file.html', function(err, data) {  
    console.log(data.toString());                       
}); 

Actually, you can print image using node-printer. This work for me

var Printer = require('node-printer');
var fs = require('fs');

// Get available printers list 
var listPrinter = Printer.list();

// Create a new Pinter from available devices 
var printer = new Printer('YOUR PRINTER HERE. GET IT FROM listPrinter');

// Print from a buffer, file path or text 
var fileBuffer = fs.readFileSync('PATH TO YOUR IMAGE');
var jobFromBuffer = printer.printBuffer(fileBuffer);

// Listen events from job 
jobFromBuffer.once('sent', function() {
    jobFromBuffer.on('completed', function() {
        console.log('Job ' + jobFromBuffer.identifier + 'has been printed');
        jobFromBuffer.removeAllListeners();
    });
});

I had success with the Node IPP package https://www.npmjs.com/package/ipp.

The example code on the docs, which uses another node module PDFKIT to convert your html/file into a PDF, does not work. See my answer here: Cannot print with node js ipp module for a working example.

发布评论

评论列表(0)

  1. 暂无评论