Skip to content Skip to sidebar Skip to footer

Saving Buffer/stream That Comes From Nodejs Server

I am not sure what I am doing wrong. I have a html content and want to save it as pdf. I use html-pdf (from npm) and a download library http://danml.com/download.html Actually when

Solution 1:

I've solved the problem.

Firstly I converted buffer to base64

const base64 = buffer.toString('base64')

and then converted base64 to blob by using the following code

functionbase64toBlob (base64Data, contentType) {

    contentType = contentType || '';
    var sliceSize = 1024;
    var byteCharacters = atob(base64Data);
    //var byteCharacters = decodeURIComponent(escape(window.atob(base64Data)))var bytesLength = byteCharacters.length;
    var slicesCount = Math.ceil(bytesLength / sliceSize);
    var byteArrays = newArray(slicesCount);

    for (var sliceIndex = 0; sliceIndex < slicesCount; ++sliceIndex) {
        var begin = sliceIndex * sliceSize;
        var end = Math.min(begin + sliceSize, bytesLength);

        var bytes = newArray(end - begin);
        for (var offset = begin, i = 0 ; offset < end; ++i, ++offset) {
            bytes[i] = byteCharacters[offset].charCodeAt(0);
        }
        byteArrays[sliceIndex] = newUint8Array(bytes);
    }
    returnnewBlob(byteArrays, { type: contentType });
}

and then

again I've used my download method (from download.js library) as follow

download(newBlob([base64toBlob(base64PDF,"application/pdf")]),
         voucherCode +'.pdf', "application/pdf");

then everything is fine :)

Post a Comment for "Saving Buffer/stream That Comes From Nodejs Server"