2013-12-17 30 views
0

我使用NodeJS的PhantomJS網橋。 我想呈現595x842像素大小的PDF文件。NodeJS的PhantomJS網橋:paperSize不能正常工作

var phantom = require('phantom'); 
phantom.create(function(ph) { 
    ph.createPage(function(page) { 
     page.set('paperSize', {width: '595px', height: '842px', border: '0px' }); 
     page.open("http://localhost:3000", function(status) { 
      if (status !== 'success') { 
       console.log('Unable to access the network!'); 
      } else { 
       page.render('filename.pdf'); 
      } 
      ph.exit(); 
     }); 
    }); 
}); 

但是,最終我得到235x331px PDF文件。我不明白爲什麼。也許有人可以幫助並解釋我如何呈現必要的文件大小?

+0

嘗試使用:page.paperSize = {width:'595px',height:'842px',border:'0px'}; –

+0

它沒有幫助。 PhantomJS的語法方法,但不適用於NodeJs模塊「NodeJS的PhantomJS橋」。 – form3

+1

https://github.com/sgentle/phantomjs-node聲明set/get不能直接調用。這可能適合你:ph.page.set('paperSize',{width:'595px',height:'842px',border:'0px'}); –

回答

4

回顧repo tutorial我發現這一點:

屬性不能獲得/直接設置,而是使用page.get( '版本', 回調)或page.set( 'viewportSize',{寬度:640,高度:480})等 嵌套目的可以通過包括在鑰匙的點,如 page.set( 'settings.loadImages' 被訪問,假)

所以,我試圖以下代碼:

var phantom = require('phantom'); 

phantom.create(function(ph) { 

    ph.createPage(function(page) { 

     page.set('viewportSize', { width : 595, height : 842}); 
     page.open("http://localhost:3000", function(status) { 
      if (status !== 'success') { 
       console.log('Unable to access the network!'); 
      } else { 
       page.render('filename.pdf'); 
      } 
      ph.exit(); 

     }); 
    }); 
}); 

希望這對你有用,就像它爲我做的一樣。