在PDFkit 0.5之前 - 以下对我有用(通过pdfkit生成pdf /通过ipp打印到CUPS):
var ipp = require("ipp");
var PDFDocument = require("pdfkit");
var doc = new PDFDocument;
doc.text("Hello World");
doc.output(function(pdf)){
var printer = ipp.Printer("http://127.0.0.1:631/printers/1");
var file = {
"operation-attributes-tag":{
"requesting-user-name": "User",
"job-name": "Print Job",
"document-format": "application/pdf"
},
data: new Buffer(pdf, "binary")
};
printer.execute("Print-Job", file, function (err, res) {
console.log("Printed: "+res.statusCode);
});
}
从PDFkit 0.5开始 - 不推荐使用output
方法 - 但我似乎无法找到在我的方案中使用新pipe
方法的示例。如果我没有使用浏览器,我还需要像blob-stream这样的模块吗?
答案 0 :(得分:10)
由于pdfkit PDFDocument现在是一个流,你必须缓冲来自缓冲区的数据:
var ipp = require("ipp");
var PDFDocument = require("pdfkit");
var doc = new PDFDocument;
doc.text("Hello World");
var buffers = [];
doc.on('data', buffers.push.bind(buffers));
doc.on('end', function () {
var printer = ipp.Printer("http://127.0.0.1:631/printers/1");
var file = {
"operation-attributes-tag":{
"requesting-user-name": "User",
"job-name": "Print Job",
"document-format": "application/pdf"
},
data: Buffer.concat(buffers)
};
printer.execute("Print-Job", file, function (err, res) {
console.log("Printed: "+res.statusCode);
});
});
doc.end();
或者您可以使用类似concat-stream模块的内容:
var ipp = require("ipp");
var PDFDocument = require("pdfkit");
var concat = require("concat-stream");
var doc = new PDFDocument;
doc.text("Hello World");
doc.pipe(concat(function (data) {
var printer = ipp.Printer("http://127.0.0.1:631/printers/1");
var file = {
"operation-attributes-tag":{
"requesting-user-name": "User",
"job-name": "Print Job",
"document-format": "application/pdf"
},
data: data
};
printer.execute("Print-Job", file, function (err, res) {
console.log("Printed: "+res.statusCode);
});
}));
doc.end();