使用node.js中的“网页”Phantom模块 [英] Using the 'webpage' Phantom module in node.js

查看:111
本文介绍了使用node.js中的“网页”Phantom模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在node.js进程中包装PhantomJS脚本。幻像脚本从命令行上提供的参数中获取URL并输出pdf(非常类似于pahntom安装中包含的rasterize.js示例)。



幻像脚本我工作正常,只是我的雇主想要一个节点脚本,如果可能的话。没问题,我可以使用。


I am trying to wrap a PhantomJS script in a node.js process. The phantom script grabs a url from the arguments provided on the command line and outputs a pdf (much similar to the rasterize.js example included with the pahntom install).

The phantom script I have works fine, it's just my employer wants a node script if possible. No problem, I can use the node-phantom node module to wrap it.

But now I've hit a stumbling block, my phantom script has:

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

So, node.js is trying to find a module called 'webpage', the 'webpage' module is built into the phantom install so node can't find it. As far as I can tell, there is no npm module called 'webpage'.

'webpage' is used like this:

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

where address is the url specified on the command line and output is another argument, the name and type of the file.

Can anyone help me out? This is quite an abstract one so I'm not expecting much if I'm honest, worth a try though.

Thanks.

EDIT - Approx 2hrs later

I now have this which throws out a 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);
                }

            });

        });
    }
});

But! It's not the right size! The page object created using the phantom 'webpage' create() function looks like this before it's passed the URL:

Whereas mine in my node script, looks like this:

Is it possible to hard code the properties to achieve A4 formatting? What properties am I missing?

I'm so close!

解决方案

It should be something like:

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

Your confusion here is because you want to reuse the same concepts and metaphors from your PhantomJS script. It does not work that way. I suggest that you spend some time studying the included tests of node-phantom, see https://github.com/alexscheelmeyer/node-phantom/tree/master/test.

这篇关于使用node.js中的“网页”Phantom模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆