marcbachmann/node-html-pdf

Cannot execute binary file

alexcroox opened this issue · 8 comments

Using AWS Lambda (works locally on OSX)

[Error: /var/task/node_modules/html-pdf/node_modules/phantomjs/lib/phantom/bin/phantomjs: /var/task/node_modules/html-pdf/node_modules/phantomjs/lib/phantom/bin/phantomjs: cannot execute binary file

One thing to note is the npm install is done on OSX before zipping up and uploading to Lambda (amazon linux). Is that what cause this issue?

Yes, that's most likely the issue. There are other binary files of phantomjs on linux.
If you're running docker somewhere, you can install it on there.

Sorry to bother @alexcroox, but what was the solution for this? Just included the binary files?

The binaries need to be built on aws lambda. (I guess there are docker images)
Yes, maybe you can include the binary that's in there.
If it doesn't work I guess you can use some docker image that replicates the lambda instance. Just execute npm install using that one. https://github.com/lambci/docker-lambda

I included the binary with the source zip upload to lambda

Managed to make it work. Just adding more info for future coders:

Lambdas need the compiled binary files to work. The simplest solution is to clone the phantom-lambda-template repo and include the phantomJS path in the html-pdf library. Step by step:

  1. Clone the phantom-lambda-template repo repo:
    $ git clone https://github.com/justengland/phantom-lambda-template.git
  2. Configure html-pdf to use a different phantomjs binary:
var options = {
  // "format": "A4",
  // "orientation": "landscape",
  base: '...',
  phantomPath: './phantom-lambda-template/phantomjs'
};

Relevant:

xkcd

Thanks @alexcroox and @marcbachmann for your help.

I ran into this issue when calling the linux binary manually with child_process.execFile. In my case it was not the phantomjs binary itself that was the issue. It was some other architecture specific node module (im guessing child_process) that was being zipped up in my app when I deployed with serverless. npm installing / deploying the app on a linux machine instead of OSX fixed this.

Using docker containers from https://github.com/lambci/docker-lambda solves the issue mentioned by @skylarmb .