I am trying to wrap a PhantomJS script in a node.js process. The phantom script grabs a url from the arguments provided on the command line and outputs a pdf (much similar to the rasterize.js example included with the pahntom install).
我试图在node.js进程中包装PhantomJS脚本。幻像脚本从命令行上提供的参数中获取URL并输出pdf(非常类似于pahntom安装中包含的rasterize.js示例)。
The phantom script I have works fine, it's just my employer wants a node script if possible. No problem, I can use the node-phantom node module to wrap it.
我的幻像脚本工作正常,只是我的雇主想要一个节点脚本,如果可能的话。没问题,我可以使用node-phantom节点模块来包装它。
But now I've hit a stumbling block, my phantom script has:
但现在我遇到了绊脚石,我的幻像脚本有:
var page = require('webpage').create();
So, node.js is trying to find a module called 'webpage', the 'webpage' module is built into the phantom install so node can't find it. As far as I can tell, there is no npm module called 'webpage'.
因此,node.js正在尝试查找名为“网页”的模块,“网页”模块内置于幻像安装中,因此节点无法找到它。据我所知,没有名为“网页”的npm模块。
'webpage' is used like this:
'网页'使用如下:
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);
}
});
where address is the url specified on the command line and output is another argument, the name and type of the file.
其中address是命令行中指定的url,输出是另一个参数,即文件的名称和类型。
Can anyone help me out? This is quite an abstract one so I'm not expecting much if I'm honest, worth a try though.
谁能帮我吗?这是一个非常抽象的,所以如果我诚实的话,我并没有期待太多,值得一试。
Thanks.
谢谢。
EDIT - Approx 2hrs later
编辑 - 约2小时后
I now have this which throws out a PDF:
我现在有这个抛出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);
}
});
});
}
});
But! It's not the right size! The page object created using the phantom 'webpage' create() function looks like this before it's passed the URL:
但!这不是合适的尺寸!使用幻像'网页'create()函数创建的页面对象在传递URL之前如下所示:
Whereas mine in my node script, looks like this:
而我的节点脚本中的我看起来像这样:
Is it possible to hard code the properties to achieve A4 formatting? What properties am I missing?
是否可以对属性进行硬编码以实现A4格式化?我错过了哪些属性?
I'm so close!
我太近了!
2 个解决方案
#1
13
It should be something like:
它应该是这样的:
var phantom=require('../node-phantom');
phantom.create(function(error,ph){
ph.createPage(function(err,page){
page.open(url ,function(err,status){
// do something
});
});
});
Your confusion here is because you want to reuse the same concepts and metaphors from your PhantomJS script. It does not work that way. I suggest that you spend some time studying the included tests of node-phantom, see https://github.com/alexscheelmeyer/node-phantom/tree/master/test.
你在这里的困惑是因为你想重用PhantomJS脚本中相同的概念和隐喻。它不起作用。我建议您花一些时间研究包含的node-phantom测试,请参阅https://github.com/alexscheelmeyer/node-phantom/tree/master/test。
#2
6
Using https://github.com/sgentle/phantomjs-node I have made an A4 page in nodejs using phantom with the following code:
使用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();
})
})
})
});
Side Note:
边注:
the page.set() function takes any variable that you would set in the rasterize.js example. See how paperSize is set above and compare it to the relevant lines in rasterize.js
page.set()函数接受您在rasterize.js示例中设置的任何变量。了解如何在上面设置paperSize并将其与rasterize.js中的相关行进行比较
#1
13
It should be something like:
它应该是这样的:
var phantom=require('../node-phantom');
phantom.create(function(error,ph){
ph.createPage(function(err,page){
page.open(url ,function(err,status){
// do something
});
});
});
Your confusion here is because you want to reuse the same concepts and metaphors from your PhantomJS script. It does not work that way. I suggest that you spend some time studying the included tests of node-phantom, see https://github.com/alexscheelmeyer/node-phantom/tree/master/test.
你在这里的困惑是因为你想重用PhantomJS脚本中相同的概念和隐喻。它不起作用。我建议您花一些时间研究包含的node-phantom测试,请参阅https://github.com/alexscheelmeyer/node-phantom/tree/master/test。
#2
6
Using https://github.com/sgentle/phantomjs-node I have made an A4 page in nodejs using phantom with the following code:
使用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();
})
})
})
});
Side Note:
边注:
the page.set() function takes any variable that you would set in the rasterize.js example. See how paperSize is set above and compare it to the relevant lines in rasterize.js
page.set()函数接受您在rasterize.js示例中设置的任何变量。了解如何在上面设置paperSize并将其与rasterize.js中的相关行进行比较