Created
October 23, 2013 10:44
-
-
Save chemzqm/7116407 to your computer and use it in GitHub Desktop.
后端导出svg为png到前端, image.js为express路由,rasterize.js为phantomjs运行文件,client.js为前端发送请求生成png文件请求的代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi,我觉得如果没有这种导出的需求的话,就根本没必要把这些东西放在后端做。
比如复杂的svg绘图。