Skip to content

Instantly share code, notes, and snippets.

@branneman
Created June 13, 2013 13:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save branneman/5773699 to your computer and use it in GitHub Desktop.
Save branneman/5773699 to your computer and use it in GitHub Desktop.
Node.js — Serve generated PNG from SVG, specifying dimensions in the url/filename. This node.js express server generates 'checkmark.svg.107x94.png' from 'checkmark.svg' with Inkscape CLI
var fs = require('fs'),
exec = require('child_process').exec,
express = require('express'),
app = express();
app.get('/static/img/*.svg.*.png', function(req, res) {
var pngFile = 'src' + req.url,
svgFile = pngFile.substring(0, pngFile.indexOf('.svg') + 4);
if (!fs.existsSync(svgFile)) {
return res.render('404', {
baseUrl: (new Array(req.url.split('/').length)).join('../')
});
}
if (fs.existsSync(pngFile)) {
return res.sendfile(pngFile);
}
var size = pngFile.split('.')[pngFile.split('.').length - 2].split('x'),
cmd = '"C:\\Program Files (x86)\\Inkscape\\inkscape.com" -z -e "' + pngFile + '" -w ' + size[0] + ' -h ' + size[1] + ' "' + svgFile + '"';
exec(cmd, function(err, stdout) {
if (err) throw err;
res.sendfile(pngFile);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment