Comparing version 0.0.1 to 0.0.2
59
index.js
@@ -6,4 +6,4 @@ var phantom = require("node-phantom-simple"); | ||
var fs = require('fs'); | ||
var _session; | ||
function pdfMaker(template, data, pdfPath, option) { | ||
@@ -29,3 +29,3 @@ | ||
generatePdf(html, pdfPath, option); | ||
createSession(html, pdfPath, option); | ||
@@ -57,3 +57,3 @@ }); | ||
var html = ejs.render(file, data); | ||
generatePdf(html, pdfPath, option); | ||
createSession(html, pdfPath, option); | ||
}); | ||
@@ -66,6 +66,21 @@ | ||
function generatePdf(html, pdfPath, option) { | ||
phantom.create({ | ||
path: phantomjs.path | ||
}, function (err, session) { | ||
function createSession(html, pdfPath, option) { | ||
if (_session) { | ||
createPage(_session, html, pdfPath, option); | ||
} else { | ||
phantom.create({ | ||
path: phantomjs.path | ||
}, function (err, session) { | ||
if (err) { | ||
throw err; | ||
} | ||
_session = session; | ||
createPage(session, html, pdfPath, option) | ||
}); | ||
} | ||
} | ||
function createPage (session, html, pdfPath, option) { | ||
session.createPage(function (err, page) { | ||
if (err) { | ||
@@ -75,27 +90,21 @@ throw err; | ||
session.createPage(function (err, page) { | ||
_.forEach(option, function (val, key) { | ||
page.set(key, val); | ||
}); | ||
page.set('content', html, function (err) { | ||
if (err) { | ||
throw err; | ||
} | ||
}); | ||
_.forEach(option, function (val, key) { | ||
page.set(key, val); | ||
}); | ||
page.set('content', html, function (err) { | ||
if (err) { | ||
page.onLoadFinished = function (status) { | ||
page.render(pdfPath, function (error) { | ||
page.close(); | ||
page = null; | ||
if (error) { | ||
throw err; | ||
} | ||
}); | ||
page.onLoadFinished = function (status) { | ||
page.render(pdfPath, function (error) { | ||
page.close(); | ||
page = null; | ||
if (error) { | ||
throw err; | ||
} | ||
}); | ||
}; | ||
}) | ||
}; | ||
}); | ||
@@ -102,0 +111,0 @@ } |
{ | ||
"name": "pdf-maker", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "easy pdf generation using phantom.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# pdf-maker | ||
An easy way to generate pdf files from ejs templates or html files | ||
#Usage | ||
``` | ||
##Install | ||
```js | ||
npm i --save pdf-maker | ||
``` | ||
#For generating pdf from ejs templates | ||
##Usage | ||
``` | ||
var pdfMaker = require('pdf-maker'); | ||
##For generating pdf from ejs templates | ||
var template = 'path/to/ejsTemplate.ejs'; | ||
var data = { | ||
name: 'node.js' | ||
```js | ||
var pdfMaker = require('pdf-maker'); | ||
var template = 'path/to/ejsTemplate.ejs'; | ||
var data = { | ||
name: 'node.js' | ||
}; | ||
var pdfPath = '/path/of/pdf/file.pdf'; | ||
var option = { | ||
{ | ||
paperSize: { | ||
format: 'A4', | ||
orientation: 'portrait', | ||
border: '1.8cm' | ||
} | ||
}; | ||
var pdfPath = '/path/of/pdf/file.pdf'; | ||
var option = { | ||
{ | ||
paperSize: { | ||
format: 'A4', | ||
orientation: 'portrait', | ||
border: '1.8cm' | ||
} | ||
}; | ||
}; | ||
}; | ||
pdfMaker(template, data, pdfPath, option); | ||
pdfMaker(template, data, pdfPath, option); | ||
``` | ||
@@ -38,25 +41,25 @@ | ||
#For generating pdf from html files. | ||
##For generating pdf from html files. | ||
``` | ||
var pdfMaker = require('pdf-maker'); | ||
```js | ||
var pdfMaker = require('pdf-maker'); | ||
var template = 'path/to/htmlFile.html'; | ||
var pdfPath = '/path/of/pdf/file.pdf'; | ||
var option = { | ||
{ | ||
paperSize: { | ||
format: 'A4', | ||
orientation: 'portrait', | ||
border: '1.8cm' | ||
} | ||
}; | ||
var template = 'path/to/htmlFile.html'; | ||
var pdfPath = '/path/of/pdf/file.pdf'; | ||
var option = { | ||
{ | ||
paperSize: { | ||
format: 'A4', | ||
orientation: 'portrait', | ||
border: '1.8cm' | ||
} | ||
}; | ||
}; | ||
pdfMaker(template, pdfPath, option); | ||
pdfMaker(template, pdfPath, option); | ||
``` | ||
#Roadmap | ||
##Roadmap | ||
Adding test cases. | ||
Removing external dependencies. |
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
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
5413
5
88
64