簡體   English   中英

使用node.js中的“網頁”Phantom模塊

[英]Using the 'webpage' Phantom module in node.js

我試圖在node.js進程中包裝PhantomJS腳本。 幻像腳本從命令行上提供的參數中獲取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);
                }

            });

        });
    }
});

但! 這不是合適的尺寸! 使用幻像'網頁'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

使用https://github.com/sgentle/phantomjs-node我使用幻像使用以下代碼在nodejs中創建了一個A4頁面:

phantom.create(function(ph){
    ph.createPage(function(page) {
        page.set("paperSize", { format: "A4", orientation: 'portrait', margin: '1cm' });
        page.open("http://www.google.com", function(status) {
            page.render("google.pdf", function(){
                console.log("page rendered");
                ph.exit();
            })
        })
    })

});

邊注:

page.set()函數接受您在rasterize.js示例中設置的任何變量。 了解如何在上面設置paperSize並將其與rasterize.js中的相關行進行比較

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM