This repository has been archived by the owner on Aug 19, 2021. It is now read-only.
forked from TJkrusinski/NodePDF
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
135 lines (111 loc) · 2.92 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var child = require('./child.js');
var Emitter = require('events').EventEmitter;
var defaults = {
'viewportSize': {
'width': 2880,
'height': 1440
},
'paperSize': {
'format': 'A4',
'orientation': 'portrait',
'margin': {
'top': '1cm',
'right': '1cm',
'bottom': '1cm',
'left': '1cm'
}
},
'zoomFactor': 1,
'args': '',
'captureDelay': 400
};
//code from https://github.com/rxaviers/cldr
var merge = function() {
var destination = {},
sources = [].slice.call( arguments, 0 );
sources.forEach(function( source ) {
var prop;
for ( prop in source ) {
if ( prop in destination && Array.isArray( destination[ prop ] ) ) {
// Concat Arrays
destination[ prop ] = destination[ prop ].concat( source[ prop ] );
} else if ( prop in destination && typeof destination[ prop ] === "object" ) {
// Merge Objects
destination[ prop ] = merge( destination[ prop ], source[ prop ] );
} else {
// Set new values
destination[ prop ] = source[ prop ];
}
}
});
return destination;
};
/**
* Constructor interface
*/
var exports = module.exports = Pdf;
function Pdf (url, fileName, opts){
var self = this;
this.url = url;
this.fileName = fileName;
this.filePath = process.env.PWD || process.cwd() || __dirname;
this.options = merge(defaults, opts);
child.supports(function(support){
if (!support) self.emit('error', 'PhantomJS not installed');
if (support) self.run();
});
return this;
};
/**
* Inherit the event emitter
*/
Pdf.prototype = Object.create(Emitter.prototype);
/**
* Run the process
* @method run
*/
Pdf.prototype.run = function() {
var self = this;
var ps = child.exec(this.url, this.fileName, this.options);
ps.on('exit', function(c, d){
if (c != 0) return self.emit('error', 'PDF conversion failed with exit of '+c);
var targetFilePath = self.fileName;
if (targetFilePath[0] != '/') {
targetFilePath = self.filePath + '/' + targetFilePath;
};
self.emit('done', targetFilePath);
});
ps.stdout.on('data', function(std){
self.emit('stdout', std);
});
ps.stderr.on('data', function(std){
self.emit('stderr', std);
});
};
/**
* Use callback style rendering
* @function render
* @param {String} address
* @param {String} file
* @param {Options} address
* @param {Function} callback
*/
exports.render = function(address, file, options, callback) {
var filePath = process.env.PWD || process.cwd() || __dirname;
if (typeof options == 'function') {
callback = options;
options = defaults;
};
options = merge(defaults, options);
child.supports(function(support){
if (!support) callback(new Error('PhantomJS not installed'));
var ps = child.exec(address, file, options);
ps.on('exit', function(c, d){
if (c) return callback(new Error('Conversion failed with exit of '+c));
var targetFilePath = file;
if (targetFilePath[0] != '/')
targetFilePath = filePath + '/' + targetFilePath;
return callback(null, targetFilePath);
});
});
};