Convert blob data to source buffer in javascript or node

I am using the jsPDF plugin that generates a PDF and saves it on the local file system. Now in jsPDF.js there is a piece of code that generates data in blob format in the form: -

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

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

Here is a sample code for this

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() reads a PDF file and generates raw buffer data.

Now I want to convert the "Blob" data to a "raw buffer" so that I can print the PDF.

+4
2

:

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

            var arrayBuffer, uint8Array;
            var fileReader = new FileReader();
            fileReader.onload = function() {
                arrayBuffer = this.result;
                uint8Array  = new Uint8Array(arrayBuffer);

                var printer = require("./js/controller/lib");
                printer.printDirect({
                    data: uint8Array,
                    printer:'Deskjet_3540',
                    type: 'PDF',
                    success: function(id) {
                        console.log('printed with id ' + id);
                    },
                    error: function(err) {
                        console.error('error on printing: ' + err);
                    }
                })
            };
            fileReader.readAsArrayBuffer(blob);

, . uint8Array.

+3

Source: https://habr.com/ru/post/1619050/


All Articles