将PDF数据转换为打印机自动检测的八位字节流



我有一台打印机,它只接受IPP上的application/octet-stream。我的程序下载二进制PDF数据,我需要将其转换为应用程序/八字节流,这将(假定)让打印机决定打印什么。但是,当我发送数据时,它只是将二进制数据打印为文本,而不是格式化的PDF。我使用的是带有npm包'ipp'的node。

我有一个类似的问题,在这个链接!我找到了一个工作的例子,我修改了一点这样的工作(混合一些pdf !

这是我的工作版本(节点v16.17.0 | npm 8.15.0 | windows 11)

var ipp = require("ipp");
var concat = require("concat-stream");
var PDFDocument = require('pdfkit');
const doc = new PDFDocument();
// Pipe its output somewhere, like to a file or HTTP response
// Render some text
doc
.fontSize(25)
.text('Some text with an embedded font!', 100, 100);
// Add an image, constrain it to a given size, and center it vertically and horizontally
doc.image('./my-image.png', {
fit: [250, 300],
align: 'center',
valign: 'center'
});
doc.pipe(concat(function (data) {
//I used this url with a Brother printer, because the 631 port had some weird problem
var printer = ipp.Printer("http://<ip address>:80/ipp",{version:'2.0'});

var file = {
"operation-attributes-tag":{
"requesting-user-name": "User",
"job-name": "Print Job",
"document-format": "application/octet-stream"
},
data: data
};
printer.execute("Print-Job", file, function (err, res) {
//in case of error
console.log("Error: ",err);
console.log('res',res);
});
}));
//This last line is very important!
doc.end();

注意你要检查的版本你的打印机是否支持

我检查了这个代码:(我失去了链接,我发现这个,所以这就是为什么没有引用它)

var ipp = require('ipp');
var uri = "http://<ip address>:80/ipp";
var data = ipp.serialize({
"operation":"Get-Printer-Attributes",
"operation-attributes-tag": {
"attributes-charset": "utf-8",
"attributes-natural-language": "en",
"printer-uri": uri
}
});

ipp.request(uri, data, function(err, res){
if(err){
return console.log(err);
}
console.log(JSON.stringify(res,null,2));
})

最新更新