我正在尝试将PhantomJS脚本包装在node.js进程中。 phantom脚本从命令行中提供的参数中获取一个url,然后输出pdf(与pahntom安装中随附的rasterize.js示例非常相似)。

我拥有的幻象脚本可以正常工作,只是我的雇主尽可能地希望使用节点脚本。没问题,我可以使用node-phantom节点模块包装它。

但是现在我遇到了一个绊脚石,我的幻影脚本具有:

var page = require('webpage').create();

因此,node.js试图找到一个名为“网页”的模块,“网页”模块内置于幻像安装中,因此节点无法找到它。据我所知,没有npm模块称为“网页”。

“网页”的用法如下:
page.open(address, function (status) {

    if (status !== 'success') {

        // --- Error opening the webpage ---
        console.log('Unable to load the address!');

    } else {

        // --- Keep Looping Until Render Completes ---
        window.setTimeout(function () {
            page.render(output);
            phantom.exit();
        }, 200);
    }
});

其中address是在命令行上指定的url,输出是另一个参数,即文件的名称和类型。

谁能帮我吗?这是非常抽象的内容,因此,老实说,我期望不高,值得一试。

谢谢。

编辑-大约2小时后

我现在有扔出PDF的文件:
var phanty = require('node-phantom');

var system = require('system');

phanty.create(function(err,phantom) {

    //var page = require('webpage').create();

    var address;
    var output;
    var size;

    if (system.args.length < 4 || system.args.length > 6) {

        // --- Bad Input ---

        console.log('Wrong usage, you need to specify the BLAH BLAH BLAH');
        phantom.exit(1);

    } else {

        phantom.createPage(function(err,page){

            // --- Set Variables, Web Address, Output ---
            address = system.args[2];
            output = system.args[3];
            page.viewportSize = { width: 600, height: 600 };


            // --- Set Variables, Web Address ---
            if (system.args.length > 4 && system.args[3].substr(-4) === ".pdf") {

                // --- PDF Specific ---
                size = system.args[4].split('*');
                page.paperSize = size.length === 2 ? { width: size[0], height: size[1], margin: '0px' }
                                                   : { format: system.args[4], orientation: 'portrait', margin: '1cm' };
            }

            // --- Zoom Factor (Should Never Be Set) ---
            if (system.args.length > 5) {
                page.zoomFactor = system.args[5];
            } else {
                page.zoomFactor = 1;
            }

            //----------------------------------------------------

            page.open(address ,function(err,status){

                if (status !== 'success') {

                    // --- Error opening the webpage ---
                    console.log('Unable to load the address!');

                } else {

                    // --- Keep Looping Until Render Completes ---
                    process.nextTick(function () {
                        page.render(output);
                        phantom.exit();
                    }, 200);
                }

            });

        });
    }
});

但!这不是正确的尺寸!使用幻像“webpage” create()函数创建的页面对象在传递URL之前如下所示:

而我的节点脚本中的我的样子是这样的:

是否可以对属性进行硬编码以实现A4格式?我缺少什么属性?

我好亲密!

最佳答案

应该是这样的:

var phantom=require('../node-phantom');
phantom.create(function(error,ph){
  ph.createPage(function(err,page){
    page.open(url ,function(err,status){
      // do something
    });
  });
});

您在这里的困惑是因为您想重复使用PhantomJS脚本中的相同概念和隐喻。它不是那样工作的。我建议您花一些时间研究所包含的node-phantom测试,请参阅https://github.com/alexscheelmeyer/node-phantom/tree/master/test

关于javascript - 在node.js中使用 'webpage' Phantom模块,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12957239/

10-09 22:43