Skip to content

Instantly share code, notes, and snippets.

@nulltask
Created November 22, 2013 17:13
Show Gist options
  • Save nulltask/7603477 to your computer and use it in GitHub Desktop.
Save nulltask/7603477 to your computer and use it in GitHub Desktop.
var fs = require('fs');
var parse = require('url').parse;
var uid = require('uid2');
var express = require('express');
var phantomjs = require('phantom');
var app = express();
app.use(express.logger('dev'));
phantomjs.create(function(p) {
app.get('/', function(req, res) {
var url = req.query.url || 'http://www.yahoo.co.jp/';
try {
var parsed = parse(url);
} catch (e) {
return res.send(400, e);
}
p.createPage(function(page) {
page.open(url, function(status) {
var uniqid = parsed.hostname + '-' + uid(32) + '.pdf';
page.render(uniqid, function() {
var stream = fs.createReadStream(uniqid);
stream.pipe(res);
});
});
});
});
});
app.listen(process.env.PORT || 3000);
{
"name": "phantom-pdf",
"version": "0.0.0",
"dependencies": {
"phantomjs": "~1.9.2-4",
"express": "~3.4.4",
"phantom": "~0.5.4",
"uid2": "0.0.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment