pofider / phantom-html-to-pdf

Highly scalable html to pdf conversion using phantom workers
MIT License
159 stars 33 forks source link

Timeout error #43

Closed DKvistgaard closed 7 years ago

DKvistgaard commented 7 years ago

We are currently using phantom-html-to-pdf to generate a user report pdf, but on one specific linux server running nginx we're getting a 504 timeout error no matter what we try. It is as if it never gets to startup phantomjs. There aren't any errors (except timeout) in any of our logs or anything. We have a different server running almost the exact same setup, where the setup works completely fine. The server where it doesn't work is a more closed test server, where only a selected range of IP-adresses have access. Other than that we can't find any noticeable differences that should affect this package.

We have tried to specify a port range and opening access for localhost requests in that range in the nginx configuration, but without luck. All localhost access should already be accepted, but we tried it nonetheless. We also tried changing the node version around to see if that had any effect, which it didn't. Giving the package a larger timeout in the global setting didn't change anything but the amount of time it took before the timeout error page was displayed.

Our implementation looks like this:

var converter = require('phantom-html-to-pdf')({
    host: '127.0.0.1'
});

converter({
    html: html,
    header: '<div>{#pageNum}</div>',
    footer: '<div></div>',
    waitForJS: true,
    waitForJSVarName: 'PHANTOM_HTML_TO_PDF_READY',
    allowLocalFilesAccess: true,
    paperSize: {
        format: 'A4',
        orientation: 'landscape',
        margin: '0.5in',
        headerHeight: '0.3in',
        footerHeight: '0.7in'
    },
    settings: {
        javascriptEnabled: true
    },
    format: {
        quality: 100
    },
    phantomPath: require('phantomjs-prebuilt').path
}, function(err, pdf) {
    if (err) {
        console.log(err);
        return;
    }

    console.log(pdf.logs);
    console.log(pdf.numberOfPages);
    pdf.stream.pipe(res);
});

Here's some server information which could be useful:

We are currently running out of ideas and hoping that someone could help us figure out what we can do to fix this?

pofider commented 7 years ago

I see you have enabled flag waitForJS: true. Did you try to run it with false?

DKvistgaard commented 7 years ago

But ofcause! Don't know why I didn't try that. That helped me find the error which is caused by a wrong filepath..

Thanks for your help! :)

DKvistgaard commented 7 years ago

Oh well, seems I was a bit too quick to close this issue. I've resolved the issues that I found previously, but as soon as I enable the flag waitForJS: true it hangs up and returns a timeout again.

I've tried setting the window. PHANTOM_HTML_TO_PDF_READY = true; on document ready to be sure that nothing of my JS code was failing. Even that results in a timeout error. And to check if there were any errors in the rendering of the html, I tried to simply return the html instead of starting the PDF converter. This returned the HTML correctly and the window. PHANTOM_HTML_TO_PDF_READY was correctly set to true.

Any other ideas that might help solve my issue? :)

pofider commented 7 years ago

So the problem is in waitForJS?

I don't know what could be a problem from your description.

You can see how it works in code. We basically create artificial script tag with particular url

https://github.com/pofider/phantom-html-to-pdf/blob/master/lib/scripts/conversionScriptPart.js#L60

and if the phantomjs catches this request, we finish the rendering and print

https://github.com/pofider/phantom-html-to-pdf/blob/master/lib/scripts/conversionScriptPart.js#L28

DKvistgaard commented 7 years ago

Hi @pofider

We've finally had some time again to get our hands into this problem and we've found a solution to the problem! It actually turned out that our javascript file wasn't loaded, but we never got an error telling us that the phantom browser couldn't load the file. It did trigger the onResourceRequested function, but it never triggered the onResourceError function. So we had no idea it was failing before all of our many attempts to console.log something from our javascript didn't appear in any logs.

Thanks for the help once again. 👍