将Mutilple pdf blobs合并为一个

时间:2018-02-19 22:35:07

标签: javascript blob

好的,所以我将html转换为pdf,从我的后端返回的pdf我转换为类型为:'application / pdf'的新Blob,这一切都正常。我现在想要将多个Blob合并为一个。我正在使用以下功能来执行此操作。

function ConcatenateBlobs(blobs, type, callback) {
                var buffers = [];

                var index = 0;

                function readAsArrayBuffer() {
                    if (!blobs[index]) {
                        return concatenateBuffers();
                    }
                    var reader = new FileReader();
                    reader.onload = function(event) {
                        buffers.push(event.target.result);
                        index++;
                        readAsArrayBuffer();
                    };
                    reader.readAsArrayBuffer(blobs[index]);
                }

                readAsArrayBuffer();

                function concatenateBuffers() {
                    var byteLength = 0;
                    buffers.forEach(function(buffer) {
                        byteLength += buffer.byteLength;
                    });

                    var tmp = new Uint16Array(byteLength);
                    var lastOffset = 0;
                    buffers.forEach(function(buffer) {
                        // BYTES_PER_ELEMENT == 2 for Uint16Array
                        var reusableByteLength = buffer.byteLength;
                        if (reusableByteLength % 2 != 0) {
                            buffer = buffer.slice(0, reusableByteLength - 1)
                        }
                        tmp.set(new Uint16Array(buffer), lastOffset);
                        lastOffset += reusableByteLength;
                    });

                    var blob = new Blob([tmp.buffer], {
                        type: type
                    });

                    callback(blob);
                }
            }

但出于某种原因,我只是将数组中的最后一个pdf显示为结果。

0 个答案:

没有答案