PhantomJs 2.0.0 - viewportSize / zoomFactor 不适用于 PDF

PhantomJs 2.0.0 - viewportSize / zoomFactor not working for PDF

我正在尝试使用 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 以方便安装

这个调整可以用来解决这个问题:

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);
            }
        });