2012-10-18 23 views
14

我想在一個node.js過程中包裝一個PhantomJS腳本。幻影腳本從命令行提供的參數中獲取一個url並輸出pdf(與pahntom安裝中包含的rasterize.js示例非常相似)。在node.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); 
    } 
}); 

其中地址是命令行,並輸出指定的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); 
       } 

      }); 

     }); 
    } 
}); 

但是!這不是合適的尺寸!

phantom returned page

而我的在我的節點腳本,看起來像這樣:

my page

Page對象使用虛擬「網頁」創建()函數看起來像這樣它通過URL之前創建

是否可以對屬性進行硬編碼以實現A4格式化?我缺少哪些屬性?

我好親近!

+0

你在這個亞當身上得到了什麼?我很好奇。 –

+0

不幸的是到最後。我說服我的團隊使用虛擬進程,而不是工作正常。 –

+0

出於好奇,最近兩個屏幕截圖使用了什麼工具? –

回答

11

它應該是這樣的:

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

你這裏混淆是因爲你想從你的PhantomJS腳本重複使用相同的概念和隱喻。它不這樣工作。我建議你花一些時間研究節點幻像的測試,參見https://github.com/alexscheelmeyer/node-phantom/tree/master/test

+1

感謝您的回答。不能相信幻影的創造者回答:)。我非常接近釘住這一點。我現在得到了PDF輸出,但它不是正確的尺寸(A4)。因爲'網頁'接收到所有的設置並將它們存儲在頁面var中,所以我需要使用從'網頁'導出爲decorateNewPage(opts,page)的函數,而不是ph.createPage(function(err,page)。這是可能的嗎? –

+0

檢查我的更新問題,我非常接近但無法破解它 –

5

使用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如何設置,並將其與光柵化中的相關行進行比較。js

+0

如何使用['phantomjs-prebuilt'](https://www.npmjs.com/package/ phantomjs)? – Rayon

+0

@Rayon看看這個模塊我最終制作了https://github.com/rknell/url2pdf我最近在一個大型項目的生產中使用了它,並且非常穩定,只是遠離直接的HTML PDF轉換,如果你正在使用圖像 –