我正在使用一个插件jsPDF,它生成PDF并将其保存到本地文件系统。现在在jsPDF.js中,有一些代码会以blob格式生成pdf数据,如下:
var blob = new Blob([array], {type: "application/pdf"});
而且还将blob数据保存到本地文件系统。现在不再需要保存,我需要使用插件node-printer打印PDF。
这里是一些示例代码:
var fs = require('fs'),
var dataToPrinter;
fs.readFile('/home/ubuntu/test.pdf', function(err, data){
dataToPrinter = data;
}
var printer = require("../lib");
printer.printDirect({
data: dataToPrinter,
printer:'Deskjet_3540',
type: 'PDF',
success: function(id) {
console.log('printed with id ' + id);
},
error: function(err) {
console.error('error on printing: ' + err);
}
})
fs.readFile()
函数读取 PDF 文件并以原始缓冲区格式生成数据。
现在我想将 'Blob' 数据转换为 '原始缓冲区',以便打印 PDF。
await blob.arrayBuffer().then((arrayBuffer) => Buffer.from(arrayBuffer, "binary"))
。 - Noelle L.