2013-05-18 60 views
9

設置爲PAPERSIZE PDF打印在幻影中生成PDF,我可以設置的紙張尺寸是這樣的:在卡斯帕

page.paperSize = { 
    height: '8.5in', 
    width: '11in', 
    orientation: 'landscape', 
    border: '0.4in' 
}; 

則page.render(輸出)功能適當地生成PDF。換句話說,大小是正確的,並且它有許多頁面的大小。

我不能在卡斯帕工作(我不確定是否支持)。舉例如下:

var casper = require('casper').create({ 
    paperSize: { 
     height: '8.5in', 
     width: '11in', 
     orientation: 'landscape', 
     border: '0.4in' 
    }, 
    logLevel: 'debug', 
    verbose: true 
}); 

....this.capture('print.pdf'); ... 

用一個很長的頁面創建PDF。設置viewportSize不能解決問題。

有什麼辦法從Casperjs中訪問pageSize對象嗎?

回答

16

您可以通過casper.page.paperSize訪問paperSize,但您需要在調用casper.start()之後設置此值,否則casper.page將等於null。

下面是一個例子:

var casper = require("casper").create(); 
casper.start(); 

casper.page.paperSize = { 
    width: '11in', 
    height: '8.5in', 
    orientation: 'landscape', 
    border: '0.4in' 
}; 

casper.thenOpen('http://www.facebook.com/', function() { 
    this.capture('test.pdf'); 
    this.echo('created pdf.'); 
}); 

casper.run(); 
+0

那偉大工程。謝謝 – Jeff

+0

@hexid有沒有辦法在casperjs將頁面保存爲PDF之前將自定義頁眉和頁腳添加到PDF中? – Anagio

+0

@Anagio你可以查看[this](http://stackoverflow.com/q/17125955/395353)問題的例子。 – hexid