|
1 | | -var fs, path, phantomjs, script, spawn; |
| 1 | +var PDF; |
2 | 2 |
|
3 | | -fs = require('fs'); |
| 3 | +PDF = require('./pdf'); |
4 | 4 |
|
5 | | -spawn = require('child_process').spawn; |
6 | | - |
7 | | -path = require('path'); |
8 | | - |
9 | | -phantomjs = require('phantomjs'); |
10 | | - |
11 | | -script = path.join(__dirname, 'scripts', 'pdf_a4_portrait.coffee'); |
12 | | - |
13 | | -exports.create = function(string, options, callback) { |
14 | | - var child, content, stderr, stdout, timeout; |
| 5 | +module.exports = function(html, options, callback) { |
| 6 | + var err, pdf; |
| 7 | + if (arguments.length === 1) { |
| 8 | + return new PDF(html); |
| 9 | + } |
| 10 | + if (arguments.length === 2 && typeof options !== 'function') { |
| 11 | + return new PDF(html, options); |
| 12 | + } |
15 | 13 | if (arguments.length === 2) { |
16 | 14 | callback = options; |
17 | 15 | options = {}; |
18 | 16 | } |
19 | | - if (!(string != null ? string.length : void 0)) { |
20 | | - return callback(new Error("html-pdf: Can't create a pdf without content")); |
| 17 | + try { |
| 18 | + pdf = new PDF(html, options); |
| 19 | + } catch (_error) { |
| 20 | + err = _error; |
| 21 | + return callback(err); |
21 | 22 | } |
22 | | - child = spawn(phantomjs.path, [options.script || script]); |
23 | | - stdout = []; |
24 | | - stderr = []; |
25 | | - timeout = setTimeout(function() { |
26 | | - child.stdin.end(); |
27 | | - child.kill(); |
28 | | - if (!stderr.length) { |
29 | | - return stderr = [new Buffer('html-pdf: PDF generation timeout. Phantom.js script did not exit.')]; |
30 | | - } |
31 | | - }, parseInt(options.timeout) || 30000); |
32 | | - child.stdout.on('data', function(buffer) { |
33 | | - return stdout.push(buffer); |
34 | | - }); |
35 | | - child.stderr.on('data', function(buffer) { |
36 | | - stderr.push(buffer); |
37 | | - child.stdin.end(); |
38 | | - return child.kill(); |
39 | | - }); |
40 | | - child.on('exit', function(code) { |
41 | | - var error, file, filename, isFileBuffer; |
42 | | - clearTimeout(timeout); |
43 | | - if ((stderr.length || code) > 0) { |
44 | | - error = new Error(Buffer.concat(stderr).toString() || 'html-pdf: Unknown Error'); |
45 | | - return callback(error); |
46 | | - } |
47 | | - file = Buffer.concat(stdout); |
48 | | - isFileBuffer = /^\%PDF/.test(file.slice(0, 4).toString()); |
49 | | - if (options.filename) { |
50 | | - return callback(null, file.toString()); |
51 | | - } else if (!isFileBuffer) { |
52 | | - filename = file.toString(); |
53 | | - return fs.readFile(filename, function(err, buffer) { |
54 | | - if (err) { |
55 | | - return callback(err); |
56 | | - } |
57 | | - return fs.unlink(filename, function(err) { |
58 | | - return callback(err, buffer); |
59 | | - }); |
60 | | - }); |
61 | | - } else { |
62 | | - return callback(null, file); |
63 | | - } |
64 | | - }); |
65 | | - content = { |
66 | | - html: string, |
67 | | - options: options |
68 | | - }; |
69 | | - return child.stdin.write(JSON.stringify(content) + '\n', 'utf8'); |
| 23 | + return pdf.exec(callback); |
| 24 | +}; |
| 25 | + |
| 26 | +module.exports.create = function() { |
| 27 | + return module.exports.apply(void 0, arguments); |
70 | 28 | }; |
0 commit comments