PhantomJs打印SVG源代码而不是渲染它

时间:2015-07-14 08:26:59

标签: svg phantomjs rasterizing

我已经设置了一台新的Windows机器,我希望将一堆SVG文档光栅化为PNG图像。我简化了Ariya Hidayat的rasterize脚本:

var page = require('webpage').create(),
    system = require('system'),
    fs = require('fs'),
    svgPath, output;

if (system.args.length < 3 || system.args.length > 5) {
    console.log('Usage: rasterize.js URL output');
    phantom.exit(1);
} else {
    svgPath = fs.workingDirectory + '/' + system.args[1];
    output = system.args[2];
    page.viewportSize = { width: 600, height: 120 };

    if (!fs.isFile(svgPath)) {
        console.log(svgPath+' does not exist');
        phantom.exit(1);
    }

    page.onLoadFinished = function() {
        page.render(output);
        console.log('thumbnail created');
        phantom.exit(0);
    };

    page.open(svgPath);
}

以下是我如何调用脚本:bin\phantomjs js/headless/rasterize.js "simple.svg" "simple.svg.png" 2>&1

simple.svg包含以下数据:

<svg width="110" height="60" id="simple" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <rect x="10" y="10" height="50" width="100" style="stroke:#ff0000; fill: #0000ff"/>
</svg>

脚本执行完毕后(没有错误),simple.svg.png呈现如下: simple.svg.png

这真的很奇怪,我很确定缩略图是在上一台机器上正确生成的。为什么它只是渲染SVG的源代码?

1 个答案:

答案 0 :(得分:0)

通过Java的ProcesBuilder执行转换时,我得到了相同的结果,而通过cmdline启动它时工作正常。我试图用file://协议为源文件添加前缀,例如file:///D:/phantomjs/test.svg它在我的案例中按预期工作。