forked from fitraditya/node-pdf2img
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
125 lines (110 loc) · 3.58 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
const fs = require('fs');
const util = require('util');
const path = require('path');
const chai = require('chai');
chai.expect();
chai.should();
const Pdf2img = require('../index.js');
const mkdir = util.promisify(fs.mkdir);
const rmdir = util.promisify(fs.rmdir);
const unlink = util.promisify(fs.unlink);
const readdir = util.promisify(fs.readdir);
const writeFile = util.promisify(fs.writeFile);
console.error = () => null;
const input = `${__dirname}${path.sep}test.pdf`;
const testDir = `${__dirname}${path.sep}testdir.pdf`;
const outputDir = `${__dirname}${path.sep}output`;
const fakeFile = `${__dirname}${path.sep}fake.pdf`;
const converter = new Pdf2img();
converter.setOptions({
outputdir: outputDir,
outputname: 'test'
});
const checkMessages = (info, fileType = 'jpg') => {
let n = 1;
for (const file of info.message) {
file.page.should.equal(n);
file.name.should.equal(`test_${n}.${fileType}`);
fs.statSync(file.path).isFile().should.be.true;
if (n === 3) {
return true;
}
n++;
}
};
const checkSingleMessage = (info, page, fileType = 'jpg') => {
info.message.length.should.equal(1);
const file = info.message[0];
file.page.should.equal(page);
file.name.should.equal(`test_${page}.${fileType}`);
fs.statSync(file.path).isFile().should.be.true;
};
describe('# Pdf2Img', function () {
this.timeout(100000);
it('should create jpg files', function () {
return converter.convert(input).then((info) => {
info.result.should.equal('success');
checkMessages(info);
});
});
it('should create png files', function () {
converter.setOptions({
type: 'png'
});
return converter.convert(input).then((info) => {
info.result.should.equal('success');
checkMessages(info, 'png');
});
});
it('should create jpg file only for given page', () => {
converter.setOptions({
type: 'jpg',
page: 1
});
return converter.convert(input).then((info) => {
info.result.should.equal('success');
checkSingleMessage(info, 1);
});
});
it('should create png file only for given page', () => {
converter.setOptions({
type: 'png',
page: 2
});
return converter.convert(input).then((info) => {
info.result.should.equal('success');
checkSingleMessage(info, 2, 'png');
});
});
it('should throw an error on invalid input path', () => {
return converter.convert('').catch((err) => {
chai.expect(err.message).to.equal('Invalid input file path.');
});
});
it('should throw an error on unsupported file type', () => {
return converter.convert(`${__dirname}${path.sep}index.js`).catch((err) => {
chai.expect(err.message).to.equal('Unsupported file type.');
});
});
it('should throw an error when the input file is not a file', () => {
return mkdir(testDir)
.then(() => converter.convert(testDir))
.catch((err) => chai.expect(err.message).to.equal('Input file not found.'));
});
it('should throw an error when trying to get the number of pages of a fake file', () => {
return writeFile(fakeFile, 'Hello there, I am not really a pdf file', 'utf8')
.then(() => converter.convert(fakeFile))
.catch((err) => chai.expect(err.message).to.contain('Command failed'));
});
after(() => {
return rmdir(testDir)
.then(() => {
return readdir(outputDir).then((files) => {
const promises = files.map((file) => unlink(`${outputDir}${path.sep}${file}`));
return Promise.all(promises);
});
})
.then(() => rmdir(outputDir))
.then(() => unlink(fakeFile));
});
});