Skip to content

Commit d454deb

Browse files
committed
Migrate module to javascript
1 parent 02748ee commit d454deb

File tree

10 files changed

+480
-775
lines changed

10 files changed

+480
-775
lines changed

.npmignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Development Files
2-
src/
32
test/
43

54
# Example Files

lib/index.js

100644100755
Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
1-
// Generated by CoffeeScript 1.10.0
2-
(function() {
3-
var PDF;
1+
var PDF = require('./pdf')
42

5-
PDF = require('./pdf');
6-
7-
exports.create = function(html, options, callback) {
8-
var err, error, pdf;
3+
module.exports = {
4+
create: function createPdf (html, options, callback) {
95
if (arguments.length === 1) {
10-
return new PDF(html);
6+
return new PDF(html)
117
}
8+
129
if (arguments.length === 2 && typeof options !== 'function') {
13-
return new PDF(html, options);
10+
return new PDF(html, options)
1411
}
12+
1513
if (arguments.length === 2) {
16-
callback = options;
17-
options = {};
14+
callback = options
15+
options = {}
1816
}
17+
1918
try {
20-
pdf = new PDF(html, options);
21-
} catch (error) {
22-
err = error;
23-
return callback(err);
19+
var pdf = new PDF(html, options)
20+
} catch (err) {
21+
return callback(err)
2422
}
25-
return pdf.exec(callback);
26-
};
2723

28-
}).call(this);
24+
pdf.exec(callback)
25+
}
26+
}

lib/pdf.js

Lines changed: 113 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,146 +1,127 @@
1-
// Generated by CoffeeScript 1.10.0
2-
(function() {
3-
var PDF, Stream, assert, childprocess, err, error, fs, path, phantomjs;
1+
var fs = require('fs')
2+
var childprocess = require('child_process')
3+
var path = require('path')
4+
var assert = require('assert')
45

5-
fs = require('fs');
6+
try {
7+
var phantomjs = require('phantomjs')
8+
} catch (err) {
9+
console.log('html-pdf: Failed to load PhantomJS module.', err)
10+
}
611

7-
Stream = require('stream').Readable;
12+
/*
13+
* phantomjs version 1.8.1 and later should work.
14+
*
15+
* Create a PDF file out of an html string.
16+
*
17+
* Regions for the PDF page are:
18+
*
19+
* - Page Header -> document.getElementById('pageHeader')
20+
* - Page Content -> document.getElementById('pageContent')
21+
* - Page Footer -> document.getElementById('pageFooter')
22+
*
23+
* When no #pageContent is available, phantomjs will use document.body as pdf content
24+
*/
25+
module.exports = PDF
26+
function PDF (html, options) {
27+
this.html = html
28+
this.options = options || {}
29+
if (this.options.script) {
30+
this.script = path.normalize(this.options.script)
31+
} else {
32+
this.script = path.join(__dirname, 'scripts', 'pdf_a4_portrait.js')
33+
}
34+
35+
if (this.options.filename) this.options.filename = path.resolve(this.options.filename)
36+
if (!this.options.phantomPath) this.options.phantomPath = phantomjs && phantomjs.path
37+
this.options.phantomArgs = this.options.phantomArgs || []
38+
assert(this.options.phantomPath, "html-pdf: Failed to load PhantomJS module. You have to set the path to the PhantomJS binary using 'options.phantomPath'")
39+
assert(typeof this.html === 'string' && this.html.length, "html-pdf: Can't create a pdf without an html string")
40+
this.options.timeout = parseInt(this.options.timeout) || 30000
41+
}
42+
43+
PDF.prototype.toBuffer = function PdfToBuffer (callback) {
44+
this.exec(function execPdfToBuffer (err, res) {
45+
if (err) return callback(err)
46+
fs.readFile(res.filename, function readCallback (err, buffer) {
47+
if (err) return callback(err)
48+
fs.unlink(res.filename, function unlinkPdfFile (err) {
49+
if (err) return callback(err)
50+
callback(null, buffer)
51+
})
52+
})
53+
})
54+
}
855

9-
childprocess = require('child_process');
56+
PDF.prototype.toStream = function PdfToStream (callback) {
57+
this.exec(function (err, res) {
58+
if (err) return callback(err)
59+
try {
60+
var stream = fs.createReadStream(res.filename)
61+
} catch (err) {
62+
return callback(err)
63+
}
1064

11-
path = require('path');
65+
stream.on('end', function () {
66+
fs.unlink(res.filename, function (err) {
67+
if (err) console.log('html-pdf:', err)
68+
})
69+
})
1270

13-
assert = require('assert');
71+
callback(null, stream)
72+
})
73+
}
1474

15-
try {
16-
phantomjs = require('phantomjs');
17-
} catch (error) {
18-
err = error;
19-
console.log('html-pdf: Failed to load PhantomJS module.', err);
75+
PDF.prototype.toFile = function PdfToFile (filename, callback) {
76+
assert(arguments.length > 0, 'html-pdf: The method .toFile([filename, ]callback) requires a callback.')
77+
if (filename instanceof Function) {
78+
callback = filename
79+
filename = undefined
80+
} else {
81+
this.options.filename = path.resolve(filename)
2082
}
83+
this.exec(callback)
84+
}
2185

22-
module.exports = PDF = (function() {
23-
function PDF(html, options) {
24-
var base, base1;
25-
this.html = html;
26-
this.options = options != null ? options : {};
27-
if (this.options.script) {
28-
this.script = path.normalize(this.options.script);
29-
} else {
30-
this.script = path.join(__dirname, 'scripts', 'pdf_a4_portrait.js');
31-
}
32-
if (this.options.filename) {
33-
this.options.filename = path.resolve(this.options.filename);
34-
}
35-
if ((base = this.options).phantomPath == null) {
36-
base.phantomPath = phantomjs != null ? phantomjs.path : void 0;
37-
}
38-
if ((base1 = this.options).phantomArgs == null) {
39-
base1.phantomArgs = [];
40-
}
41-
assert(this.options.phantomPath, "html-pdf: Failed to load PhantomJS module. You have to set the path to the PhantomJS binary using 'options.phantomPath'");
42-
assert(typeof this.html === 'string' && this.html.length, "html-pdf: Can't create a pdf without an html string");
43-
this.options.timeout = parseInt(this.options.timeout) || 30000;
86+
PDF.prototype.exec = function PdfExec (callback) {
87+
var child = childprocess.spawn(this.options.phantomPath, [].concat(this.options.phantomArgs, [this.script]))
88+
var stdout = []
89+
var stderr = []
90+
var timeout = setTimeout(function execTimeout () {
91+
child.stdin.end()
92+
child.kill()
93+
if (!stderr.length) {
94+
stderr = [new Buffer('html-pdf: PDF generation timeout. Phantom.js script did not exit.')]
4495
}
96+
}, this.options.timeout)
4597

46-
PDF.prototype.toBuffer = function(callback) {
47-
return this.exec(function(err, res) {
48-
if (err) {
49-
return callback(err);
50-
}
51-
return fs.readFile(res.filename, function(err, buffer) {
52-
if (err) {
53-
return callback(err);
54-
}
55-
return fs.unlink(res.filename, function(err) {
56-
if (err) {
57-
return callback(err);
58-
}
59-
return callback(null, buffer);
60-
});
61-
});
62-
});
63-
};
98+
child.stdout.on('data', function (buffer) {
99+
return stdout.push(buffer)
100+
})
64101

65-
PDF.prototype.toStream = function(callback) {
66-
return this.exec(function(err, res) {
67-
var error1, stream;
68-
if (err) {
69-
return callback(err);
70-
}
71-
try {
72-
stream = fs.createReadStream(res.filename);
73-
} catch (error1) {
74-
err = error1;
75-
return callback(err);
76-
}
77-
stream.on('end', function() {
78-
return fs.unlink(res.filename, function(err) {
79-
if (err) {
80-
return console.log('html-pdf:', err);
81-
}
82-
});
83-
});
84-
return callback(null, stream);
85-
});
86-
};
102+
child.stderr.on('data', function (buffer) {
103+
stderr.push(buffer)
104+
child.stdin.end()
105+
return child.kill()
106+
})
87107

88-
PDF.prototype.toFile = function(filename, callback) {
89-
assert(arguments.length > 0, 'html-pdf: The method .toFile([filename, ]callback) requires a callback.');
90-
if (filename instanceof Function) {
91-
callback = filename;
92-
filename = void 0;
93-
} else {
94-
this.options.filename = path.resolve(filename);
108+
child.on('exit', function (code) {
109+
clearTimeout(timeout)
110+
if (code || stderr.length) {
111+
var err = new Error(Buffer.concat(stderr).toString() || 'html-pdf: Unknown Error')
112+
return callback(err)
113+
} else {
114+
try {
115+
var buff = Buffer.concat(stdout).toString()
116+
var data = (buff) != null ? buff.trim() : undefined
117+
data = JSON.parse(data)
118+
} catch (err) {
119+
return callback(err)
95120
}
96-
return this.exec(callback);
97-
};
98-
99-
PDF.prototype.exec = function(callback) {
100-
var child, stderr, stdout, timeout;
101-
child = childprocess.spawn(this.options.phantomPath, [].concat(this.options.phantomArgs, [this.script]));
102-
stdout = [];
103-
stderr = [];
104-
timeout = setTimeout(function() {
105-
child.stdin.end();
106-
child.kill();
107-
if (!stderr.length) {
108-
return stderr = [new Buffer('html-pdf: PDF generation timeout. Phantom.js script did not exit.')];
109-
}
110-
}, this.options.timeout);
111-
child.stdout.on('data', function(buffer) {
112-
return stdout.push(buffer);
113-
});
114-
child.stderr.on('data', function(buffer) {
115-
stderr.push(buffer);
116-
child.stdin.end();
117-
return child.kill();
118-
});
119-
child.on('exit', function(code) {
120-
var data, error1, ref;
121-
clearTimeout(timeout);
122-
if (code || stderr.length) {
123-
err = new Error(Buffer.concat(stderr).toString() || 'html-pdf: Unknown Error');
124-
return callback(err);
125-
} else {
126-
try {
127-
data = (ref = Buffer.concat(stdout).toString()) != null ? ref.trim() : void 0;
128-
data = JSON.parse(data);
129-
} catch (error1) {
130-
err = error1;
131-
return callback(err);
132-
}
133-
return callback(null, data);
134-
}
135-
});
136-
return child.stdin.write(JSON.stringify({
137-
html: this.html,
138-
options: this.options
139-
}) + '\n', 'utf8');
140-
};
141-
142-
return PDF;
143-
144-
})();
121+
return callback(null, data)
122+
}
123+
})
145124

146-
}).call(this);
125+
var res = JSON.stringify({html: this.html, options: this.options})
126+
return child.stdin.write(res + '\n', 'utf8')
127+
}

0 commit comments

Comments
 (0)