Node-html-pdf: Failed to load PhantomJS module

Created on 2 Dec 2016  ยท  12Comments  ยท  Source: marcbachmann/node-html-pdf

Im using html-pdf on my website and it works very well on my computer (Ubuntu 14.04).

When i'm trying to put the app in production (Ubuntu 16.04) i have an error message :

AssertionError:
html-pdf: Failed to load PhantomJS module. You have to set the path to the PhantomJS binary using 'options.phantomPath'

pdf.create(pdfContents, { filename: pdfPath, format: 'Letter', orientation: 'portrait', type: 'pdf', timeout: 30000 }) .toFile(function(err, fileRes) { ...

It works well without setting phantomPath on my computer it's strange.
I tried to specify phantomPath like this :

pdf.create(pdfContents, { filename: pdfPath, format: 'Letter', phantomPath: './node_modules/phantomjs/bin/phantomjs', orientation: 'portrait', type: 'pdf', timeout: 30000 })

still working on my computer but in the server i get this error message :

Error: internal/child_process.js:289
var err = this._handle.spawn(options);
^
TypeError: Bad argument

I really don't know what to do, thanks for the help.

Most helpful comment

@glenn-allen thank you. I combined your solution with this: https://github.com/giakki/uncss/issues/165 and it works well.

Here's how I resolve my error:

  1. install this module html-pdf (mine is Ver2.1.0)
  2. Go to node_modules folder then delete folder phantomjs-prebuiltthat come along with this html-pdf installation, then do npm install [email protected] (thanks to @glenn-allen ๐Ÿ‘๐Ÿป).
  3. Do sudo apt-get install libfontconfig. (see this https://github.com/giakki/uncss/issues/165)

All 12 comments

FYI, I ran into this problem just recently.
For me the issue appeared to be a new version of the "phantomjs-prebuilt" module which html-pdf relies on.
A previous installation relied on version 2.1.13, however the fresh install used 2.1.14.
Uninstalling 2.1.14 (npm uninstall phantomjs-prebuilt) and installing 2.1.13 (npm install [email protected]) fixed it for me.

@glenn-allen thank you. I combined your solution with this: https://github.com/giakki/uncss/issues/165 and it works well.

Here's how I resolve my error:

  1. install this module html-pdf (mine is Ver2.1.0)
  2. Go to node_modules folder then delete folder phantomjs-prebuiltthat come along with this html-pdf installation, then do npm install [email protected] (thanks to @glenn-allen ๐Ÿ‘๐Ÿป).
  3. Do sudo apt-get install libfontconfig. (see this https://github.com/giakki/uncss/issues/165)

Try npm rebuild phantomjs-prebuilt, it worked for me.

2017-02-15 8:04 GMT+01:00 Yogie Putra notifications@github.com:

@glenn-allen https://github.com/glenn-allen I tried your solution
[image: screen shot 2017-02-15 at 2 01 42 pm]
https://cloud.githubusercontent.com/assets/6022596/22963775/6310a54c-f387-11e6-9d36-f9dc3442d614.png
then tried execute the app again, get this error:
[image: screen shot 2017-02-15 at 2 03 12 pm]
https://cloud.githubusercontent.com/assets/6022596/22963791/87c2dfea-f387-11e6-9de7-c4dec4557a8b.png

โ€”
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/marcbachmann/node-html-pdf/issues/212#issuecomment-279933124,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AFwFhBnZIK7rHqzhKAsrRzLJT5eWpg6Nks5rcqOCgaJpZM4LChTT
.

For me it was just to install on Ubuntu 16.04 64bits:

sudo apt-get install libfontconfig

And that error disappeared.

I am on ubuntu machine and on installing this module it did not install the phantomjs for me. I had to install the phantomjs manually and put it in my package.json. After installing the nodule it worked just fine.

@yogieputra8 how to get libfontconfig on centos 7.x

Hi, guys!

I work in two machines: one with 16.04 and the other with 18.04.

Whatever I do in one is mirrored in the other 'cause I use Dropbox.

I got the Phantom message when I try to use then json file to parametrize the pdf.create

If I run the app without the reference to the file ("pdf.create(content)") it works fine in both machines but when I put "pdf.create(content, "./prmsPDF.json")" it throws the error.

It seems I need to use the parameters file, because I can't make the darn thing to load images.

Can anybody help me?

Go to your node_modules folder through Terminal and do "sudo node ./install.js".
This worked for me

just uninstall and install the phantomjs-prebuilt globally (-g) which worked for me.
sudo npm uninstall -g phantomjs-prebuilt
sudo npm install -g phantomjs-prebuilt

@glenn-allen thank you. I combined your solution with this: uncss/uncss#165 and it works well.

Here's how I resolve my error:

  1. install this module html-pdf (mine is Ver2.1.0)
  2. Go to node_modules folder then delete folder phantomjs-prebuiltthat come along with this html-pdf installation, then do npm install [email protected] (thanks to @glenn-allen ๐Ÿ‘๐Ÿป).
  3. Do sudo apt-get install libfontconfig. (see this uncss/uncss#165)

Thank you so much! This has helped a ton.

@glenn-allen thank you. I combined your solution with this: uncss/uncss#165 and it works well.

Here's how I resolve my error:

1. install this module html-pdf (mine is `Ver2.1.0`)

2. Go to `node_modules` folder then delete folder `phantomjs-prebuilt `that come along with this html-pdf installation, then do `npm install [email protected]` (thanks to @glenn-allen ๐Ÿ‘๐Ÿป).

3. Do `sudo apt-get install libfontconfig`. (see this [uncss/uncss#165](https://github.com/uncss/uncss/issues/165))

Yes its working fine i tried it

If you are using a docker image like Alpine which cannot run apt-get
you can use RUN apk method and install phantom js by using installing curl & downloading it inside the docker building process.

#Add this to your docker file
RUN apk add --no-cache curl && \
    cd /tmp && curl -Ls https://github.com/dustinblackman/phantomized/releases/download/2.1.1/dockerized-phantomjs.tar.gz | tar xz && \
    cp -R lib lib64 / && \
    cp -R usr/lib/x86_64-linux-gnu /usr/lib && \
    cp -R usr/share /usr/share && \
    cp -R etc/fonts /etc && \
    curl -k -Ls https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2 | tar -jxf - &&\
    cp phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs && \
    rm -fR phantomjs-2.1.1-linux-x86_64 && \
    apk del curl

Also you can refer the discussion here:
https://gist.github.com/vovimayhem/6437c2f03b654e392ccf3e9903eba6af
I have added final commands that worked for me in comment below the gist: https://gist.github.com/vovimayhem/6437c2f03b654e392ccf3e9903eba6af#gistcomment-3601962

Was this page helpful?
0 / 5 - 0 ratings