在node.js中使用“网页”幻影模块

我想在一个node.js过程中包装一个PhantomJS脚本。 幻影脚本从命令行提供的参数中获取一个url,并输出一个pdf(与pahntom安装中包含的rasterize.js示例非常相似)。

我的幻影脚本工作正常,这只是我的雇主希望如果可能的节点脚本。 没问题,我可以使用node-phantom节点模块来包装它。

但是现在我碰到了一个绊脚石,我的幻影脚本有:

var page = require('webpage').create(); 

因此,node.js试图find一个名为“网页”的模块,“网页”模块内置到幻影安装中,因此节点找不到它。 据我所知,没有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,output是另一个参数,即文件的名称和types。

谁能帮我吗? 这是一个相当抽象的,所以如果我是诚实的,我不会期望太多,值得一试。

谢谢。

编辑 – 约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); } }); }); } }); 

但! 这不是正确的尺寸! 在通过URL之前,使用幻像“网页”的create()函数创build的页面对象如下所示:

幻影返回页面

而我的节点脚本,看起来像这样:

我的页面

是否可以硬编码的属性来实现A4格式? 我缺less哪些属性?

我好亲近

它应该是这样的:

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

这里的困惑是因为你想重用PhantomJS脚本中的相同概念和隐喻。 这种方式不行。 我build议你花一些时间学习包含节点幻像的testing,见https://github.com/alexscheelmeyer/node-phantom/tree/master/test

使用https://github.com/sgentle/phantomjs-node我用下面的代码使用phantom在nodejs中创build了一个A4页面&#xFF1A;

 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示例中设置的任何variables。 看看paperSize是如何设置的,并将其与rasterize.js中的相关行进行比较