Have spent the past few days troubleshooting an issue with PhantomJS. While veryfing in development and test all was fine, the application running on WebSphere did produce PDF’s like a charm. But when we moved to stage that did not work. The strange thing was that all tests we did from command line was fine, but the application kept on producing 0kb files – the logs told us. Not empty files, empty PDF’s produced by PhantomJS will be 5kb, so this was another issue. So, a new version of the application was deployed which did not delete from the temp folder, .js and .html file was found, but no still no PDF.

I did from previous problems know that a 0kb file on Windows is not always a real file, more a pointer to a file that was there before. And after hours of work with no progress, other than improved logging and good test suites (but not that good since they did not test what caused the error), it turned out to be a .css import in the HTML causing the error. The import statement should be environment aware, but it turned out that it was referring to the development environment. This worked out fine in test since there was no firewall blocking the endpoint, but in stage it was. HTML-files with the import statement pointing to a blocked endpoint caused PhantomJS to produce nothing. Even if the code
console.log(prettyTime() + " [INFO] Now create pdf")
page.render(output, {format: 'pdf'});
console.log(prettyTime() + " [INFO] PDF has now been created")

did not throw any error. While writing this post I did search for page.render and error handling and found this post https://github.com/amir20/phantomjs-node/issues/290 Must admit that I do not understand what

I have changed this behavior to make it more clear. You can do catch() from promise.

really means. I will forward it to the developers. Anyhow, html-file with this error causes PhantomJS to produce nothing without any error. If we removed the import, all fine.