我正在尝试使用Phantomjs将html导出为pdf。除PDF中html对象的大小外,其他所有东西都运行良好。当我使用viewportSize或zoomFactor时,根本没有效果。
var page = require('webpage').create();
page.viewportSize = { width: 800, height: 600};
page.zoomFactor = 2;
page.open("http://www.google.com", function start(status) {
page.render('test.pdf');
phantom.exit();
});
这似乎是2.0.0中的已知问题参见https://github.com/ariya/phantomjs/issues/12685
使用PhantomJs 1.9.8版效果很好。请参阅https://gist.github.com/julionc/7476620以方便安装
此调整可用于解决此问题:
[Comment下面的代码在rasterize.js
/*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(1);
} else {
window.setTimeout(function () {
page.render(output);
phantom.exit();
}, 200);
}
});*/
在同一位置code
下添加。
page.zoomFactor = parseFloat('0.4');
page.open(address, function (status) {
if (status !== 'success') {
phantom.exit(1);
} else {
window.setTimeout(function () {
page.evaluate(function(zoom) {
return document.querySelector('body').style.zoom = zoom;
}, page.zoomFactor);
page.render(output);
phantom.exit();
}, 200);
}
});