Before PDFkit 0.5 - the following worked for me (generating a pdf via pdfkit/printing via ipp to 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);
});
}
As of PDFkit 0.5 - the output
method is deprecated - but I can't seem to find an example of using the new pipe
method with my scenario. If I'm not using a browser, do I still need a module like blob-stream?
Pipes can be used to connect multiple streams together. One of the most common example is to pipe the read and write stream together for the transfer of data from one file to the other. Node. js is often also tagged as an event driven framework, and it's very easy to define events in Node. js.
There are two ways to use PDFKit in the browser. The first is to create an app using an module bundler like Browserify or Webpack. The second is to create a standalone pdfkit script as explained here.
Since pdfkit PDFDocument now is a stream, you have to buffer the data coming from the buffer:
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();
Or you can use something like concat-stream module:
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With