Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Blob data to Raw buffer in javascript or node

I am using a plugin jsPDF which generates PDF and saves it to local file system. Now in jsPDF.js, there is some piece of code which generates pdf data in blob format as:-

var blob = new Blob([array], {type: "application/pdf"});

and further saves the blob data to local file system. Now instead of saving I need to print the PDF using plugin node-printer.

Here is some sample code to do so

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);
    }
})

The fs.readFile() reads the PDF file and generates data in raw buffer format.

Now what I want is to convert the 'Blob' data into 'raw buffer' so that I can print the PDF.

like image 411
Kamaldeep Singh Avatar asked Dec 08 '15 14:12

Kamaldeep Singh


1 Answers

For me, it worked with the following:

const buffer=Buffer.from(blob,'binary');

So, this buffer can be stored in Google Cloud Storage and local disk with fs node package.

I used blob file, to send data from client to server through ddp protocol (Meteor), so, when this file arrives to server I convert it to buffer in order to store it.

like image 119
Ivan Cabrera Avatar answered Oct 25 '22 05:10

Ivan Cabrera