Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problem on Ubuntu 14 (On windows all good) #91

Open
ivanvoitovych opened this issue Mar 22, 2016 · 2 comments
Open

problem on Ubuntu 14 (On windows all good) #91

ivanvoitovych opened this issue Mar 22, 2016 · 2 comments

Comments

@ivanvoitovych
Copy link

I have this code:

{% autoescape false %}

/**
 * Set up page and script parameters
 */
var page       = require('webpage').create(),
    system     = require('system'),
    response   = {},
    debug      = [],
    logs       = [],
    procedure  = {};

/**
 * Global variables
 */


/**
 * Define width & height of capture
 */

{% if input.getType() == 'capture' or input.getType() == 'pdf' %}

var rectTop    = {{ input.getRectTop() }},
    rectLeft   = {{ input.getRectLeft() }},
    rectWidth  = {{ input.getRectWidth() }},
    rectHeight = {{ input.getRectHeight() }};

if(rectWidth && rectHeight) {

    debug.push(new Date().toISOString().slice(0, -5) + ' [INFO] PhantomJS - Set capture clipping size ~ top: ' + rectTop + ' left: ' + rectLeft + ' ' + rectWidth + 'x' + rectHeight);

    page.clipRect = {
        top: rectTop,
        left: rectLeft,
        width: rectWidth,
        height: rectHeight
    };
}

{% endif %}

/**
 * Define paper size.
 */

{% if input.getType() == 'pdf' %}

    var paperWidth       = '{{ input.getPaperWidth() }}',
        paperHeight      = '{{ input.getPaperHeight() }}',
        paperFormat      = '{{ input.getFormat() }}',
        paperOrientation = '{{ input.getOrientation() }}',
        paperMargin      = '{{ input.getMargin() }}';

    if(paperWidth && paperHeight) {

        debug.push(new Date().toISOString().slice(0, -5) + ' [INFO] PhantomJS - Set page size ~ width: ' + paperWidth + ' height: ' + paperHeight + ' margin: ' + paperMargin);

        page.paperSize = { 
            width:  paperWidth, 
            height: paperHeight, 
            margin: paperMargin 
        };

    } else {

        debug.push(new Date().toISOString().slice(0, -5) + ' [INFO] PhantomJS - Set page size ~ format: ' + paperFormat + ' orientation: ' + paperOrientation + ' margin: ' + paperMargin);

        page.paperSize = { 
            format:      paperFormat, 
            orientation: paperOrientation, 
            margin:      paperMargin 
        };

    }

{% endif %}

/**
 * Define viewport size.
 */

var viewportWidth  = {{ input.getViewportWidth() }},
    viewportHeight = {{ input.getViewportHeight() }};

if(viewportWidth && viewportHeight) {

    debug.push(new Date().toISOString().slice(0, -5) + ' [INFO] PhantomJS - Set viewport size ~ width: ' + viewportWidth + ' height: ' + viewportHeight);

    page.viewportSize = {
        width: viewportWidth,
        height: viewportHeight
    };
}




/**
 * Define custom headers.
 */

var headers = {{ input.getHeaders('json') }};

page.customHeaders = headers ? headers : {};



/**
 * Page settings
 */
page.settings.resourceTimeout = 30000; // 30 seconds


/**
 * On resource timeout
 */

page.onResourceTimeout = function (error) {



 console.log(e.errorCode);   // it'll probably be 408 
  console.log(e.errorString); // it'll probably be 'Network timeout on resource'
  console.log(e.url);         // the url whose request timed out

return procedure.execute(status);
};

/**
 * On resource received
 */
page.onResourceReceived = function (resource) {

if(!response.status) {
    response = resource;
}


};

/**
 * Handle page errors
 */
page.onError = function (msg, trace) {

var error = {
    message: msg,
    trace: []
};

trace.forEach(function(t) {
    error.trace.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function + ')' : ''));
});

logs.push(error);


};

/**
 * Handle global errors
 */
phantom.onError = function(msg, trace) {

var stack = [];

trace.forEach(function(t) {
    stack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function + ')' : ''));
});

response.status  = 500;
response.content = msg;
response.console = stack;

system.stdout.write(JSON.stringify(response, undefined, 4));
phantom.exit(1);


};

/**
 * Open page
 */
page.open ('{{ input.getUrl() }}', '{{ input.getMethod() }}', '{{ input.getBody() }}', function (status) {

 function waitFor( page, selector, expiry, callback ) {

         var result = page.evaluate(
            function (selector) {
                return document.querySelector( selector );
            }, selector
        );

         if ( result ) {
            window.setTimeout(
                function () {
                    callback( true );
                },
                50
            );
            return;
        }


        var finish = (new Date()).getTime();
        if ( finish > expiry ) {
            callback( false );
            return;
        }

         window.setTimeout(
            function () {
                waitFor( page, selector, expiry, callback );
            },
            250
        );
    }

         waitFor(
    page,
    "#endresolving", 
    (new Date()).getTime() + 30000, 
    function (founded) {


        if ( founded ) {
        debug.push(new Date().toISOString().slice(0, -5) + ' status: '+status+' [INFO] PhantomJS - resolved all');

            return procedure.execute(status);
        } else {
        debug.push(new Date().toISOString().slice(0, -5)+ ' status: '+status + ' [INFO] PhantomJS - cant resolve');
             return procedure.execute(status);
        }
    }
);
});

/**
 * Execute procedure
 */
procedure.execute = function (status) {

if (status === 'success') {

    try {

        response.content = page.evaluate(function () {
            return document.getElementsByTagName('html')[0].innerHTML
        });
         debug.push(new Date().toISOString().slice(0, -5) + ' content: '+response.content.length+' code:'+response.status );

        if(!response.status)
        {
        response.status = 200;
        }
    } catch(e) {

        response.status  = 500;
        response.content = e.message;
        debug.push(new Date().toISOString().slice(0, -5) + ' error: '+e.message );

    }
}

response.console = logs;
system.stderr.write(debug.join('\\n') + '\\n');
system.stdout.write(JSON.stringify(response, undefined, 4));

phantom.exit();


};

{% endautoescape %}

line: debug.push(new Date().toISOString().slice(0, -5) + ' content: '+response.content.length+' code:'+response.status ); - returning code 200 and length 75400 but in $response empty fields (Ubuntu only)

@jonnnnyw
Copy link
Owner

If are using the PhantomJS binary that came bundled with the library, I would recommend compiling PhantomJS for your system just to see if it could be that. Some people have been having trouble with the generic Linux binary.

@ivanvoitovych
Copy link
Author

didn`t help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants