Ada*_*ite 17 javascript rendering node.js phantomjs
我试图在node.js进程中包装PhantomJS脚本.幻像脚本从命令行上提供的参数中获取URL并输出pdf(非常类似于pahntom安装中包含的rasterize.js示例).
我的幻像脚本工作正常,只是我的雇主想要一个节点脚本,如果可能的话.没问题,我可以使用node-phantom节点模块来包装它.
但现在我遇到了绊脚石,我的幻像脚本有:
var page = require('webpage').create();
Run Code Online (Sandbox Code Playgroud)
因此,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);
}
});
Run Code Online (Sandbox Code Playgroud)
其中address是命令行中指定的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);
}
});
});
}
});
Run Code Online (Sandbox Code Playgroud)
但!这不是合适的尺寸!使用幻像'网页'create()函数创建的页面对象在传递URL之前如下所示:
在我的节点脚本中,我看起来像这样:
是否可以对属性进行硬编码以实现A4格式化?我错过了哪些属性?
我太近了!
Ari*_*yat 14
它应该是这样的:
var phantom=require('../node-phantom');
phantom.create(function(error,ph){
ph.createPage(function(err,page){
page.open(url ,function(err,status){
// do something
});
});
});
Run Code Online (Sandbox Code Playgroud)
你在这里的困惑是因为你想重用PhantomJS脚本中相同的概念和隐喻.它不起作用.我建议您花一些时间研究包含的node-phantom测试,请参阅https://github.com/alexscheelmeyer/node-phantom/tree/master/test.
使用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();
})
})
})
});
Run Code Online (Sandbox Code Playgroud)
边注:
page.set()函数接受您在rasterize.js示例中设置的任何变量.了解如何在上面设置paperSize并将其与rasterize.js中的相关行进行比较
归档时间: |
|
查看次数: |
29469 次 |
最近记录: |