express-pdf
Advanced tools
Comparing version 0.0.0 to 1.0.0
60
index.js
@@ -9,9 +9,41 @@ var express = require('express'), | ||
function PDF(req, res, next){ | ||
function setHeader(res){ | ||
function setHeader(res, filename){ | ||
res.header('Content-Type', 'application/pdf'); | ||
res.header('Content-Disposition', 'inline; filename="' + opt.filename + '"'); | ||
res.header("Content-Transfer-Encoding: binary"); | ||
res.header('Content-Disposition', 'inline; filename="' + filename + '"'); | ||
res.header('Content-Transfer-Encoding', 'binary'); | ||
} | ||
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(); | ||
}) | ||
} | ||
}); | ||
} | ||
res.pdf = function(filename){ | ||
setHeader(this); | ||
var _this = this; | ||
return new Promise(function(resolve, reject){ | ||
try{ | ||
fs.statSync(filename); | ||
}catch(e){ | ||
reject(filename + ' does not exists'); | ||
return; | ||
} | ||
setHeader(_this, path.basename(filename)); | ||
var stream = fs.createReadStream(filename); | ||
stream.pipe(_this); | ||
stream.on('end', function(){ | ||
_this.end(); | ||
resolve(); | ||
}); | ||
stream.on('error', function(error){ | ||
reject(error); | ||
}); | ||
}); | ||
}; | ||
@@ -28,21 +60,5 @@ res.pdfFromHTML = function(opt) { | ||
if(opt.html !== undefined){ | ||
setHeader(_this); | ||
var html = fs.readFileSync(opt.html, 'utf-8'); | ||
pdf.create(html).toStream(function(err, stream){ | ||
if(err){ | ||
reject(err); | ||
}else{ | ||
stream.pipe(_this); | ||
resolve(); | ||
} | ||
}); | ||
sendHTMLPDF(_this, opt.filename, fs.readFileSync(opt.html, 'utf-8'), opt.options, reject, resolve); | ||
}else if(opt.htmlContent !== undefined){ | ||
pdf.create(opt.htmlContent).toStream(function(err, stream){ | ||
if(err){ | ||
reject(err); | ||
}else{ | ||
stream.pipe(_this); | ||
resolve(); | ||
} | ||
}); | ||
sendHTMLPDF(_this, opt.filename, opt.htmlContent, opt.options, reject, resolve); | ||
}else{ | ||
@@ -49,0 +65,0 @@ reject('html and htmlContent not set'); |
{ | ||
"name": "express-pdf", | ||
"version": "0.0.0", | ||
"version": "1.0.0", | ||
"description": "Express serving pdf file", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "jasmine-node spec/" | ||
}, | ||
@@ -21,3 +21,17 @@ "author": "Tan Li Hau", | ||
"var-clean": "^1.0.1" | ||
} | ||
}, | ||
"devDependencies": { | ||
"jasmine-node": "^1.14.5" | ||
}, | ||
"peerDependencies": { | ||
"express": "^4.13.4" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/tanhauhau/express-pdf.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/tanhauhau/express-pdf/issues" | ||
}, | ||
"homepage": "https://github.com/tanhauhau/express-pdf#readme" | ||
} |
@@ -1,3 +0,72 @@ | ||
# html-pdf | ||
# express-pdf | ||
***Work in progress*** | ||
[![Build Status](https://travis-ci.org/tanhauhau/express-pdf.svg?branch=master)](https://travis-ci.org/tanhauhau/express-pdf) | ||
[![npm version](https://badge.fury.io/js/express-pdf.svg)](https://badge.fury.io/js/express-pdf) | ||
[![Dependency status](https://david-dm.org/tanhauhau/express-pdf.svg)](https://david-dm.org) | ||
[![Downloads](https://img.shields.io/npm/dt/express-pdf.svg)](https://www.npmjs.com/package/express-pdf) | ||
[![Donate](https://img.shields.io/gratipay/user/tanhauhau.svg)](https://gratipay.com/~tanhauhau/) | ||
## Installation | ||
```bash | ||
npm install --save express-pdf | ||
``` | ||
```javascript | ||
var express = require('express'), | ||
app = express(), | ||
pdf = require('express-pdf'); | ||
//include pdf | ||
app.use(pdf()); | ||
app.use('/pdfFromHTML', function(req, res){ | ||
res.pdfFromHTML({ | ||
filename: 'generated.pdf', | ||
html: path.resolve(__dirname, './template.html'), | ||
options: {...} | ||
}); | ||
}); | ||
app.use('/pdfFromHTMLString', function(req, res){ | ||
res.pdfFromHTML({ | ||
filename: 'generated.pdf', | ||
htmlContent: '<html><body>ASDF</body></html>', | ||
options: {...} | ||
}); | ||
}); | ||
app.use('/pdf', function(req, res){ | ||
res.pdf(path.resolve(__dirname, './original.pdf')); | ||
}) | ||
``` | ||
## Options | ||
This library is built on top of [html-pdf](https://www.npmjs.com/package/html-pdf). | ||
Read [this](https://www.npmjs.com/package/html-pdf#options) for the full list of options available when generating the pdf from html. | ||
## License | ||
The MIT License (MIT) | ||
Copyright (c) 2016 Tan Li Hau | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
35251
10
156
1
0
73
1
5
1
1