Node-html-pdf: Docker ENOENT error on alpine

Created on 27 Nov 2018  路  8Comments  路  Source: marcbachmann/node-html-pdf

Hi,
I am trying with this dockerfile:

FROM mhart/alpine-node:11.2.0

RUN npm -g config set user root
ENV NODE_PATH=/usr/lib/node_modules

RUN npm install -g html-pdf
RUN npm list -g --depth 0

WORKDIR /usr/src/app
COPY package.json package-lock.json ./

ENV NODE_ENV=production
RUN npm install --only=production
RUN npm audit fix

COPY . .
EXPOSE 8080
CMD ["node", "index.js"]

It installs everything correctly but when I try to use html-pdf I am still getting this error:

{
  Error: spawn /node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs ENOENT
  at Process.ChildProcess._handle.onexit(internal/child_process.js: 246: 19)
  at onErrorNT(internal /child_process.js: 421: 16)
  at process.internalTickCallback(internal/process/next_tick.js: 72: 19)
  errno: 'ENOENT',
  code: 'ENOENT',
  syscall: 'spawn /node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs',
  path: '/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs',
  spawnargs: ['/node_modules/html-pdf/lib/scripts/pdf_a4_portrait.js']
}

I am not setting any phantomPath to html-pdf create options.

I am trying this on https://zeit.co/

RUN npm list -g --depth 0 is returning an empty list

If this problem is unsolvable I could use another image!
I have a working container using node:8.11.1 but it is a 800Mb image and I need something smaller.
What would you use?

Most helpful comment

great

Great console.log("Great")

All 8 comments

Probably my answer could be helpful for somebody.

I've tried to run html-pdf on quay.io/aptible/nodejs and experienced not exactly the same issue, but relative one. It also was ENOENT for phantomjs.

I haven't noticed at once, but in my case it tried to run phantomjs.exe instead of phantomjs that of course doesn't work in linux container. So setting phantomPath in config object for pdf.create helped.
But of course it is still a hack...

great

great

Great console.log("Great")

Has anyone solved this issue

I was facing same issue. Resolve after fontConfg installing in .dockerfile

RUN apk --update add fontconfig ttf-dejavu

links
https://github.com/marcbachmann/node-html-pdf/issues/212#issuecomment-279933124
https://github.com/gliderlabs/docker-alpine/issues/360

I reached the below link trying to solve my issue as mentioned by @sameerAhmad9291
@yogieputra8's comment: https://github.com/marcbachmann/node-html-pdf/issues/212#issuecomment-279933124
But there it has commands(apt-get) than do not work in alpine based docker image
Hence we need to download and install phantomjs on our own. I mentioned this in a comment in that issue
My comment: https://github.com/marcbachmann/node-html-pdf/issues/212#issuecomment-764752380 and also mentioning it again below


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

Related issues

rayax86 picture rayax86  路  5Comments

Mortuie picture Mortuie  路  4Comments

jimit-hothi picture jimit-hothi  路  4Comments

OussamaRomdhane picture OussamaRomdhane  路  4Comments

wobsoriano picture wobsoriano  路  3Comments