Socket
Socket
Sign inDemoInstall

express-pdf

Package Overview
Dependencies
138
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.0 to 1.0.0

.travis.yml

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.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc