Node.js : How to add print job to printer

血红的双手。 提交于 2019-12-09 19:14:12

问题


I am developing a web application using node.js where i have a scenario to print some pdf files located in my local.

Ex:

var ipp = require('ipp');
var PDFDocument = require('pdfkit');

//make a PDF document
var doc = new PDFDocument({margin:0});
doc.text(".", 0, 780);

doc.output(function(pdf){
    var printer = ipp.Printer("http://NPI977E4E.local.:631/ipp/printer");
    var msg = {
        "operation-attributes-tag": {
            "requesting-user-name": "William",
            "job-name": "My Test Job",
            "document-format": "application/pdf"
        },
        data: pdf
    };
    printer.execute("Print-Job", msg, function(err, res){
        console.log(res);
    });
});

I have referred the above example, but in that PDF is created but in my case i want to print the existing PDF files.

Any suggestions ???


回答1:


var fs = require('fs');

fs.readFile('filename.pdf', function(err, data) { 
  if (err)
    throw err;

  var printer = ipp.Printer("http://YOUR.PRINTER.SERVER.HOSTNAME:631/ipp/printer");
  var msg = {
    "operation-attributes-tag": {
      "requesting-user-name": "William",
      "job-name": "My Test Job",
      "document-format": "application/pdf"
    },
    data: data
  };
  printer.execute("Print-Job", msg, function(err, res){
    console.log(res);
  });
});



回答2:


If I understand you correctly, you want to print local PDF files, and printing works already?

Node.js has the fs api you can use to retrieve a PDF file. http://nodejs.org/api/fs.html

https://npmjs.org/package/ipp To me it doesn't seem you have to use a PDFkit object as data -property in your operation. You can just use data you can read with fs.



来源:https://stackoverflow.com/questions/16187936/node-js-how-to-add-print-job-to-printer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!