forked from simonstewart/swagger-to-PDF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
87 lines (75 loc) · 2.55 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
const fs = require('fs');
const converter = require('./converter');
const pdf = require('phantom-html2pdf');
function convertToHtml(jsonFileList) {
const splitJSONFiles = jsonFileList.split(',');
const html = splitJSONFiles
.map(filename => {
const swaggerJSON = JSON.parse(fs.readFileSync(filename.trim(), 'utf8'));
return converter.convertToHtml(swaggerJSON.document || swaggerJSON);
})
.join("<div style='page-break-after:always' />");
const htmlFileName =
splitJSONFiles.length === 1
? splitJSONFiles[0].substr(0, splitJSONFiles[0].lastIndexOf('.')) + '.html'
: './output.html';
fs.writeFile(htmlFileName, html, err => {
if (err) {
console.log('err:' + err);
} else {
console.log('Done writing ' + htmlFileName);
}
});
}
function convertToPdf(jsonFileList) {
const splitJSONFiles = jsonFileList.split(',');
const html = splitJSONFiles
.map(filename => {
const swaggerJSON = JSON.parse(fs.readFileSync(filename.trim(), 'utf8'));
return converter.convertToHtml(swaggerJSON.document || swaggerJSON);
})
.join("<div style='page-break-after:always' />");
const pdfFileName =
splitJSONFiles.length === 1
? splitJSONFiles[0].substr(0, splitJSONFiles[0].lastIndexOf('.')) + '.pdf'
: './output.pdf';
let config = {
css: './normalize.css',
html
};
pdf.convert(config, function(err, result) {
if (err) console.log('err:' + err);
else {
/* Using a buffer and callback */
result.toBuffer(function(returnedBuffer) {
console.log('return buffer');
});
/* Using a readable stream */
var stream = result.toStream();
/* Using the temp file path */
var tmpPath = result.getTmpPath();
/* Using the file writer and callback */
result.toFile(pdfFileName, function(err) {
if (err) {
console.log('err:' + err);
} else {
console.log('Done writing ' + pdfFileName);
}
});
}
});
}
(function() {
exports = {
convertToHtml,
convertToPdf,
converter
};
if (!module.parent) {
if (process.argv.length >= 4 && process.argv[3] === 'html') {
convertToHtml(process.argv[2]);
} else {
convertToPdf(process.argv[2]);
}
}
})();