-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
30 lines (25 loc) · 798 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const hummus = require('hummus');
const memoryStreams = require('memory-streams');
/**
* Merges an array of PDF buffers into one single PDF buffer
*
* @param {Buffer[]} buffers - a list of PDF buffers to be merged
*
* @returns {Buffer} merged pdf buffer
*/
exports.merge = async buffers => {
const [first] = buffers;
const outStream = new memoryStreams.WritableStream();
const firstPdfStream = new hummus.PDFRStreamForBuffer(first);
const pdfWriter = hummus.createWriterToModify(
firstPdfStream,
new hummus.PDFStreamForResponse(outStream),
);
buffers.shift();
buffers.forEach(buffer => {
const newPdfStream = new hummus.PDFRStreamForBuffer(buffer);
pdfWriter.appendPDFPagesFromPDF(newPdfStream);
});
pdfWriter.end();
return outStream.toBuffer();
};