1

I'm trying to run the rasterize.js script from php in order to generate pdfs of a page on my side. I've modified it slightly to give better error messages.

In PHP I've got the following code:

exec($cmd.' '.$script.' '.$url.' '.$filename.' 0.8 '.$domain.' '.$session_id);

The full command that runs looks like this:

/var/www/site/scripts/phantomjs /var/www/site/scripts/rasterize.js http://domain.site.com/index.php/pdf /var/www/pdfs/sample.pdf 0.8 domain.site.com ftrs44g1esnpjerqcube8bban1

If I run the exact same code from the command line, the PDF is created but if I run it from php I get the error (generated from rasterize.js) "Host domain.site.com not found".

Any ideas on what might be causing the issue?

rasterize.js

var page = require('webpage').create(),
    system = require('system'),
    address, output, size;

phantom.addCookie({
    'name':     'laravel_session',   /* required property */
    'value':    system.args[5],  /* required property */
    'domain':   system.args[4],           /* required property */
    'path' :'/'
});

page.onResourceError = function(resourceError) {
    page.reason = resourceError.errorString;
    page.reason_url = resourceError.url;
};

if (system.args.length < 3 || system.args.length > 6) {
    console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom]');
    console.log('  paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"');
    phantom.exit(1);
} else {
    address = system.args[1];
    output = system.args[2];
    page.viewportSize = { width: 630, height: 891 };
    page.paperSize = { format: 'A4', orientation: 'portrait', margin: '1cm' };
    if (system.args.length > 3) {
        page.zoomFactor = system.args[3];
    }
    page.open(address, function (status) {
        if (status !== 'success') {
            console.log("Error opening url \"" + page.reason_url
                        + "\": " + page.reason);
            phantom.exit();
        } else {
            window.setTimeout(function () {
                page.render(output);
                phantom.exit();
            }, 200);
        }
    });
}

1 Answer 1

2

The issue was the VPS was behind a proxy. Using exec from PHP didn't copy the environment.

Just using putenv for the http_proxy and https_proxy variables solved it.

Sign up to request clarification or add additional context in comments.

1 Comment

You don't need to pass the domain. You can just use window.location.hostname

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.