Convert Blob data to Raw buffer in javascript or node

后端 未结 3 1366
猫巷女王i
猫巷女王i 2021-02-09 04:55

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:-

<
相关标签:
3条回答
  • 2021-02-09 05:01
               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);
    

    This is the final code which worked for me. The printer accepts uint8Array encoding format.

    0 讨论(0)
  • 2021-02-09 05:11

    Try:

    var blob = new Blob([array], {type: "application/pdf"});
    var buffer = new Buffer(blob, "binary");
    
    0 讨论(0)
  • 2021-02-09 05:16

    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.

    0 讨论(0)
提交回复
热议问题