我想在一個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);
}
});
});
}
});
但是!這不是合適的尺寸!
而我的在我的節點腳本,看起來像這樣:
是否可以對屬性進行硬編碼以實現A4格式化?我缺少哪些屬性?
我好親近!
你在這個亞當身上得到了什麼?我很好奇。 –
不幸的是到最後。我說服我的團隊使用虛擬進程,而不是工作正常。 –
出於好奇,最近兩個屏幕截圖使用了什麼工具? –