node.js - 节点幻影多个实例不呈现正确的页面
问题描述
我正在使用幻像来保存多个交易的 pdf,但有一天我注意到一些奇怪的事情,假设有两个页面 A 和 B 然后我想将其捕获为 pdf,但是当 A.pdf 和 B.pdf 创建时,两者其中显示页面 B。仅当我同时调用它们时才会发生。
示例代码:
function testPhantom(i)
{
var phantom = require('phantom');
phantom.create()
.then(function(ph){phInstance = ph; return ph.createPage();})
.then(function(page){
page.property('viewportSize', {width: "210mm", height: "297mm"});
page.property('paperSize', {format: 'A4', orientation: 'portrait', margin: '1cm'});
pageInstance = page;
if (i == 1)
{
return page.open('http://www.google.com/');
}
else if (i == 2)
{
return page.open('https://www.facebook.com/');
}
})
.then(function(status){
console.log(status);
return pageInstance.render('test'+i+'.pdf');
})
.then(function(){
// phInstance.exit();
})
.catch(function(error){
console.log(error);
// phInstance.exit();
});
}
testPhantom(1);
testPhantom(2);
使用此代码,它既可以是 google 也可以是 facebook。
如果我在函数上调用 exit,警告将显示为
warn: exit() was called before waiting for commands to finish. Make sure you are not calling exit() prematurely
或错误为
Error: Error reading from stdin: Error: This socket has been ended by the other party
我怎样才能有不同的幻影实例,所以他们会做对
解决方案
我认为你应该制作phInstance
一个pageInstance
局部变量。像这样:
var phInstance;
var pageInstance;
var phantom = require('phantom');
etc.
推荐阅读
- python - SyntaxError: 'break' outside loop,这是显示的错误
- html - 如果表格行到达页面末尾,则创建新的 DIN A4
- python - 如何在下面的条形图中删除额外的 X 标签?
- .net-core - .net 核心 API 中的混合身份验证
- python - 如何从dict中的值列表中获取每个值
- oracle - 尝试使用 11g 表单创建饼图时遇到问题
- javascript - 在主线程上创建图像位图?
- clang - 特定节点上的 AST 匹配器
- javascript - 在 JavaScript 中为对象的键添加引号和下划线
- powerbi - Power BI:根据另一个表的条件计算一个表的行数