Skip to content

Instantly share code, notes, and snippets.

@chemzqm
Created October 23, 2013 10:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chemzqm/7116407 to your computer and use it in GitHub Desktop.
Save chemzqm/7116407 to your computer and use it in GitHub Desktop.
后端导出svg为png到前端, image.js为express路由,rasterize.js为phantomjs运行文件,client.js为前端发送请求生成png文件请求的代码
var chart = highchart(this.el, opts).highcharts();
var svg = chart.getSVG();
request
.post('/image')
.send({
xml: svg
})
.set('Accept', 'application/json')
.end(function(err, res) {
if(err) throw err;
if(res.error) throw new Error(res.error.status);
window.open('/image/' + res.body.name);
})
var express = require('express');
var childProcess = require('child_process');
var phantomjs = require('phantomjs');
var fs = require ('fs');
var path = require ('path');
var tmp = require ('tmp');
var binPath = phantomjs.path;
var app = module.exports = express();
app.post('/image', function(req, res, next) {
//xml is the svg string posted in field xml
var xml = req.body.xml;
var str = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n' + xml;
tmp.file({
template: path.join(__dirname, 'tmp/tmp-XXXXXXXX.svg')
}, function (err, filePath, fd) {
if (err) return next(err);
var target = filePath.replace(/\.svg$/, '.png');
fs.writeFile(filePath, str, function(err) {
if(err) return next(err);
var childArgs = [ path.join(__dirname, 'rasterize.js'), filePath, target];
childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) {
if(err) return next(err);
res.json({
name: path.basename(target)
})
})
})
});
})
app.get('/image/:filename', function(req, res, next) {
var fileName = req.params.filename;
var filePath = path.join(__dirname, 'tmp', fileName);
res.download(filePath, function(err) {
if(err) return next(err);
})
})
var page = require('webpage').create(),
system = require('system'),
address, output, size;
if (system.args.length < 3 || system.args.length > 5) {
console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom]');
console.log(' paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"');
phantom.exit(1);
} else {
address = system.args[1];
output = system.args[2];
page.viewportSize = { width: 600, height: 600 };
if (system.args.length > 3 && system.args[2].substr(-4) === ".pdf") {
size = system.args[3].split('*');
page.paperSize = size.length === 2 ? { width: size[0], height: size[1], margin: '0px' }
: { format: system.args[3], orientation: 'portrait', margin: '1cm' };
}
if (system.args.length > 4) {
page.zoomFactor = system.args[4];
}
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
phantom.exit();
} else {
window.setTimeout(function () {
page.render(output);
phantom.exit();
}, 200);
}
});
}
@chemzqm
Copy link
Author

chemzqm commented Oct 24, 2013

ubuntu下需要执行sudo apt-get install xfonts-wqy解决乱码问题

@joostshao
Copy link

hi,我觉得如果没有这种导出的需求的话,就根本没必要把这些东西放在后端做。
比如复杂的svg绘图。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment