Jeffry Antonio
Jeffry Antonio

Reputation: 33

node phantom multiple instance doesn't rendering right page

I am using phantom to save pdf of multiple transaction, but one day I notice some weird thing, let say there is two page A and B then I want to capture it as pdf, but when A.pdf and B.pdf created, both of them display page B. Only happen when I call them at the same time.

Sample Code :

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);

With this code it will either both google or both facebook.

If I call exit on the function, warning will displayed as

warn: exit() was called before waiting for commands to finish. Make sure you are not calling exit() prematurely

or error as

Error: Error reading from stdin: Error: This socket has been ended by the other party

How can I have difference instance of phantom so they will do right

Upvotes: 3

Views: 64

Answers (1)

Oscar Yuandinata
Oscar Yuandinata

Reputation: 1035

I think you should make phInstance and pageInstance a local variable. Like this:

var phInstance;
var pageInstance;
var phantom = require('phantom');
etc.

Upvotes: 2

Related Questions