1
我有一个带有SVG标记的示例测试页,我试图用PhantomJS将其另存为SVG文件。我可以使用PhantomJS进入测试页面,但是我无法弄清楚如何找到并保存svg。以下是我试图做到这一点,以及我卡在哪里。用PhantomJS保存SVG
- 了解它们与网页
- 试图找到SVG标签与document.getElementsByTagName( 'SVG')
- 什么从这里做什么?我可以看到幻影返回一个巨大的对象,但无法找到他们的网站上有关如何处理这件事。我该如何将这个对象转换成svg文件?
的testpage看起来是这样的:
<HTML>
This is a Testpage. You like it, don't you?
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<linearGradient id="myLinearGradient1"
x1="0%" y1="0%"
x2="0%" y2="100%"
spreadMethod="pad">
<stop offset="0%" stop-color="#00cc00" stop-opacity="1"/>
<stop offset="100%" stop-color="#006600" stop-opacity="1"/>
</linearGradient>
</defs>
<rect x="10" y="10" width="75" height="100" rx="10" ry="10"
style="fill:url(#myLinearGradient1);
stroke: #005000;
stroke-width: 3;" />
</svg>
</HTML>
和PhantomJS脚本
var page = require('webpage').create();
console.log('The default user agent is ' + page.settings.userAgent);
function getSVG(link, callback) {
page.open(link, function(status) {
if (status !== 'success') {
console.log('Unable to access network');
} else {
var ua = page.evaluate(function() {
return document.getElementsByTagName('svg');
});
// Don't know what to do here
}
phantom.exit();
callback();
});
}
getSVG('testpage', function() {
console.log('done');
});