express-pdf
Advanced tools
Comparing version 1.2.1 to 1.2.2
46
index.js
@@ -8,2 +8,10 @@ var express = require('express'), | ||
function notFound(res){ | ||
res.sendStatus(404); | ||
res.end(); | ||
} | ||
function internalError(res){ | ||
res.sendStatus(500); | ||
res.end(); | ||
} | ||
function setHeader(res, filename){ | ||
@@ -14,14 +22,16 @@ res.header('Content-Type', 'application/pdf'); | ||
} | ||
function sendHTMLPDF(res, filename, content, options, reject, resolve){ | ||
setHeader(res, filename); | ||
pdf.create(content, options).toStream(function(err, stream){ | ||
if(err){ | ||
reject(err); | ||
}else{ | ||
stream.pipe(res); | ||
stream.on('end', function(){ | ||
res.end(); | ||
resolve(); | ||
}) | ||
} | ||
function sendHTMLPDF(res, filename, content, options){ | ||
return new Promise(function(resolve, reject){ | ||
setHeader(res, filename); | ||
pdf.create(content, options).toStream(function(err, stream){ | ||
if(err){ | ||
reject(err); | ||
}else{ | ||
stream.pipe(res); | ||
stream.on('end', function(){ | ||
res.end(); | ||
resolve(); | ||
}) | ||
} | ||
}); | ||
}); | ||
@@ -35,2 +45,3 @@ } | ||
if(err){ | ||
notFound(_this); | ||
return reject(filename + ' does not exists'); | ||
@@ -63,7 +74,14 @@ } | ||
fs.readFile(opt.html, 'utf-8', function(err, data){ | ||
sendHTMLPDF(_this, opt.filename, data, opt.options, reject, resolve); | ||
if(err){ | ||
notFound(_this); | ||
return reject(opt.html + ' does not exists'); | ||
} | ||
sendHTMLPDF(_this, opt.filename, data, opt.options) | ||
.then(resolve, reject); | ||
}); | ||
}else if(opt.htmlContent !== undefined){ | ||
sendHTMLPDF(_this, opt.filename, opt.htmlContent, opt.options, reject, resolve); | ||
sendHTMLPDF(_this, opt.filename, opt.htmlContent, opt.options) | ||
.then(resolve, reject); | ||
}else{ | ||
internalError(_this); | ||
reject('html and htmlContent not set'); | ||
@@ -70,0 +88,0 @@ } |
{ | ||
"name": "express-pdf", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "Express serving pdf file", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -39,3 +39,3 @@ var express = require('express'), | ||
it('should return a promise', function(done){ | ||
it('should return a resolved promise', function(done){ | ||
var app = express(), server; | ||
@@ -48,8 +48,16 @@ app.use(pdf); | ||
expect(promise).toEqual(jasmine.any(Promise)); | ||
promise.then(function(){ | ||
expect(true).toEqual(true); | ||
finish(); | ||
}, function(){ | ||
expect('should not call catch').toBe(true); | ||
finish(); | ||
}); | ||
}); | ||
server = app.listen(8888); | ||
function finish(){ | ||
done(); | ||
server.close(); | ||
}); | ||
server = app.listen(8888); | ||
} | ||
http.get('http://127.0.0.1:8888/test/3', function(res){ | ||
@@ -59,3 +67,3 @@ }); | ||
it('should return a promise', function(done){ | ||
it('should return a resolved promise', function(done){ | ||
var app = express(), server; | ||
@@ -72,10 +80,108 @@ app.use(pdf); | ||
promise.then(function(){ | ||
expect(true).toEqual(true); | ||
finish(); | ||
}, function(){ | ||
expect('should not call catch').toBe(true); | ||
finish(); | ||
}); | ||
}); | ||
server = app.listen(8888); | ||
function finish(){ | ||
done(); | ||
server.close(); | ||
} | ||
http.get('http://127.0.0.1:8888/test/4', function(res){ | ||
}); | ||
}); | ||
it('should return a rejected promise', function(done){ | ||
var app = express(), server; | ||
app.use(pdf); | ||
app.get('/test/6', function(req, res){ | ||
var promise = res.pdf(path.resolve(__dirname, './fixture/missing.pdf')); | ||
expect(promise).not.toBeUndefined(); | ||
expect(promise).toEqual(jasmine.any(Promise)); | ||
promise.then(function(){ | ||
expect('then should not be called').toBe(true); | ||
finish(); | ||
}, function(error){ | ||
expect(true).toBe(true); | ||
expect(error).not.toBeUndefined(); | ||
expect(error).toMatch(/missing\.pdf does not exists$/); | ||
finish(); | ||
}); | ||
}); | ||
server = app.listen(8888); | ||
http.get('http://127.0.0.1:8888/test/4', function(res){ | ||
function finish(){ | ||
done(); | ||
server.close(); | ||
} | ||
http.get('http://127.0.0.1:8888/test/6', function(res){ | ||
}); | ||
}); | ||
it('should return a rejected promise', function(done){ | ||
var app = express(), server; | ||
app.use(pdf); | ||
app.get('/test/7', function(req, res){ | ||
var promise = res.pdfFromHTML({ | ||
filename: 'file.pdf', | ||
html: path.resolve(__dirname, './fixture/missing.html') | ||
}); | ||
expect(promise).not.toBeUndefined(); | ||
expect(promise).toEqual(jasmine.any(Promise)); | ||
promise.then(function(){ | ||
expect('then should not be called').toBe(true); | ||
finish(); | ||
}, function(error){ | ||
expect(true).toBe(true); | ||
expect(error).not.toBeUndefined(); | ||
expect(error).toMatch(/missing\.html does not exists$/); | ||
finish(); | ||
}); | ||
}); | ||
server = app.listen(8888); | ||
function finish(){ | ||
done(); | ||
server.close(); | ||
} | ||
http.get('http://127.0.0.1:8888/test/7', function(res){ | ||
}); | ||
}); | ||
it('should return a rejected promise', function(done){ | ||
var app = express(), server; | ||
app.use(pdf); | ||
app.get('/test/7', function(req, res){ | ||
var promise = res.pdfFromHTML({ | ||
filename: 'file.pdf', | ||
}); | ||
expect(promise).not.toBeUndefined(); | ||
expect(promise).toEqual(jasmine.any(Promise)); | ||
promise.then(function(){ | ||
expect('then should not be called').toBe(true); | ||
finish(); | ||
}, function(error){ | ||
expect(true).toBe(true); | ||
expect(error).not.toBeUndefined(); | ||
expect(error).toMatch(/^html and htmlContent not set$/); | ||
finish(); | ||
}); | ||
}); | ||
server = app.listen(8888); | ||
function finish(){ | ||
done(); | ||
server.close(); | ||
} | ||
http.get('http://127.0.0.1:8888/test/7', function(res){ | ||
}); | ||
}); | ||
}); |
39170
274