-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
82 lines (69 loc) · 1.67 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var sizeOf = require('image-size')
var fs = require("fs")
var PDFDocument = require('pdfkit')
var dimension = require("./lib/dimension.js")
// image resource
var Resource = function(image){
this.path = image
this.size = sizeOf(image)
}
var calcMaxSize = function(imageSet){
var maxWidth = 0
var maxHeight = 0
imageSet.forEach(function(res){
var size = res.size
maxWidth = Math.max(maxWidth, size.width)
maxHeight = Math.max(maxHeight, size.height)
})
return [maxWidth, maxHeight]
}
var createDoc = function(imgs, options){
var images = imgs.map(function(img){
var res = new Resource(img)
return res
})
// auto scaling
if(!options.size){
options.size = calcMaxSize(images)
}
var doc = new PDFDocument(options)
var pageSize = {
width: doc.page.width,
height: doc.page.height
}
// generate document
images.forEach(function(res, i){
if(i > 0){
doc.addPage()
}
var size = dimension.calcSize(pageSize, res.size)
var offset = dimension.calcOffset(pageSize, size)
doc.image(res.path, offset.x, offset.y, size)
})
return doc
}
var PDFImagePack = function(options){
this.options = options || {}
}
PDFImagePack.prototype.createDoc = function(images){
return createDoc(images, this.options)
}
PDFImagePack.prototype.output = function(images, output, cb){
var doc = this.createDoc(images)
var stream = undefined
if(typeof output == "string"){
stream = fs.createWriteStream(output)
}else{
stream = output
}
doc.pipe(stream)
// bind event
stream.on('error', function(err){
cb(err)
})
stream.on('finish', function(){
cb(null, doc)
})
doc.end()
}
module.exports = PDFImagePack