Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 91 lines (61 sloc) 2.098 kB
#!/usr/bin/env coffee
express = require 'express'
phantom = require 'phantom'
jade = require 'jade'
fs = require 'fs'
_ = require 'lodash'
app = express()
app.use express.bodyParser()
makeContent = jade.compile fs.readFileSync 'content.jade'
getDisposition = (options) ->
if options.attachment
value = 'attachment; '
else
value = ''
return value + 'filename="' + options.filename + '"'
formats =
JPG: 'image/jpeg'
defaultFormat = 'PNG'
phantom.create (ph) ->
app.post '/renderize/', (request, response, next) ->
unless request.body?
response.writeHead 400,
'Content-Type': 'application/json'
'Content-Disposition': 'error.json'
response.end JSON.stringify
error: 'The following params are required: "content"'
options = _.merge request.body
options.width ?= 400
options.height ?= 320
if options.format?
options.format = options.format.toUpperCase()
else
options.format = defaultFormat
lowerFormat = options.format.toLowerCase()
unless options.contentType?
if formats[options.format]?
options.contentType = formats[options.format]
else
options.contentType = 'image/' + lowerFormat
unless options.filename?
options.filename = 'renderized.' + lowerFormat
unless options.attachment?
options.attachment = false
if _.isString options.attachment
index = ['1', 'true', 'yes'].indexOf options.attachment.toLowerCase()
if index isnt -1
options.attachment = true
else
options.attachment = false
ph.createPage (page) ->
page.set 'viewportSize', width: options.width, height: options.height
page.setContent makeContent
content: options.content
styles: options.styles
page.renderBase64 options.format, (content) ->
response.writeHead 200,
'Content-Type': options.contentType
'Content-Disposition': getDisposition options
imageBuffer = new Buffer content, 'base64'
response.end imageBuffer, 'binary'
app.listen 9040
Jump to Line
Something went wrong with that request. Please try again.