html5-to-pdf
Version:
HTML5 to PDF converter
52 lines (45 loc) • 1.63 kB
text/coffeescript
_ = require 'lodash'
path = require 'path'
fs = require 'fs'
express = require 'express'
enableDestroy = require 'server-destroy'
Nightmare = require 'nightmare'
debug = require('debug')('html5-to-pdf:nightmare')
class Generator
constructor: () ->
= new Nightmare {}
build: (callback) =>
(error, url) =>
return callback error if error?
.goto url
()
.evaluate , .get('inputBody')?.toString()
.wait .get('renderDelay')
.pdf .get('outputPath'), .get('options')
.end()
.then =>
?.destroy?()
callback null
.catch (error) =>
?.destroy?()
callback error
includeAssets: =>
_.each .get('include'), ({ type, filePath }={}) =>
.inject type, filePath
return
addBody: (html) ->
body = document.querySelector "body"
body.removeChild document.querySelector "p"
container = document.createElement "div"
container.innerHTML = html
body.appendChild container
return document
startServer: (callback) =>
return callback null, .get('templateUrl') if .get('templateUrl')?
app = express()
app.use express.static(.get('templatePath'))
= app.listen 0, (error) =>
return callback error if error?
enableDestroy
callback null, "http://localhost:#{@_server.address().port}"
module.exports = Generator