marcbachmann / node-html-pdf

This repo isn't maintained anymore as phantomjs got dreprecated a long time ago. Please migrate to headless chrome/puppeteer.
MIT License
3.56k stars 543 forks source link

Spawn ENOENT #233

Open miller-carvalhaes opened 7 years ago

miller-carvalhaes commented 7 years ago

I'm having somre troubles to create PDF file.

let html = "<p> HELLO ! </p>";
let OPTIONS = {phantomPath: './node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs'};
pdf.create(html, OPTIONS).toFile('output.pdf', function (err, res) {
                if (err){
                    console.log( "MESSAGE => ", err.message);
                    console.log( "ERROR => ", err);
                } 
                console.log(res);
            });

{ Error: spawn ./node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs ENOENT
at exports._errnoException (util.js:1022:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
at onErrorNT (internal/child_process.js:359:16)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
code: 'ENOENT',
errno: 'ENOENT',
syscall: 'spawn ./node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs',
path: './node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs',
spawnargs: [ '/usr/src/app/node_modules/html-pdf/lib/scripts/pdf_a4_portrait.js' ] }

Using command-line example worked well.

yogieputra8 commented 7 years ago

+1 I'm facing the same issue here

vjsrinath commented 7 years ago

+1 Me too facing the same issue. BTW, I am running my application in docker. Is there any special attention needed.

justinneff commented 7 years ago

+1 Same issue. Also using docker. The same code works fine on Windows.

yogieputra8 commented 7 years ago

@vjsrinath @justinneff try my solution on this thread https://github.com/marcbachmann/node-html-pdf/issues/212#issuecomment-280120314

sohel-ahmed-ansari commented 7 years ago

@vjsrinath @justinneff I was facing the same issue in docker. Managed to solve it though. The cause was that i had a global version of phantomjs already installed on my machine. Docker doesnt have access to module installed globally.

Solution:

  1. Remove globally installed phantomjs (npm uninstall -g phantomjs)
  2. Make sure /usr/bin/phantomjs doesnt exist. If it does, delete it.
  3. Install phantomjs locally in the directory which is mounted on docker

Hope that works for you.

nastassia7 commented 7 years ago

I am getting the same error...

I have tried:

  1. uninstalling phantomjs globally & installing locally
  2. npm rebuild phantomjs-prebuilt
  3. versions of phantomjs-prebuilt 2.1.13 & 2.1.14
  4. installing tar & bzip2 globally

(Mac OS X, node 4.6.1, html-pdf 2.1.0, phantomjs-prebuilt 2.1.13)

JoeChapman commented 7 years ago

Use the following

const phantomPath = require('witch')('phantomjs-prebuilt', 'phantomjs');

pdf.create(options.template, {
    phantomPath: `${phantomPath}`,
    ...
})
vjsrinath commented 7 years ago

@sohel-ahmed-ansari the issue was happening when i was using the official node image based on alphine linux. Later I created my own image based on ubuntu, it worked. I didn't have to install phantomjs globally. Take a look at my Dockerfile

Thanks

sohel-ahmed-ansari commented 7 years ago

@vjsrinath Didn't get what you mean. I already had global phantomjs installed, which was causing this issue. Had to remove the global version and install locally to solve it

vjsrinath commented 7 years ago

@sohel-ahmed-ansari I think we arrived at an issue from two different places. I was facing this issue in my docker container that was created from node alphine linux image. When i switched the base image to ubuntu it worked for me.

kitko112 commented 7 years ago

I have this problem when I use 8.7.0-alpin in my docker file. I got my problem solved just using 8.7.0-wheezy instead, no source code changes.

pepethree commented 6 years ago

facing the same issue. command line example has the same error. works great on my dev site. any new fixes?

AlessandroAnnini commented 6 years ago

Why is this close? what's the official solution?

Fabryprog commented 6 years ago

8.7.0-wheezy is the solution!!!

AlessandroAnnini commented 6 years ago

ciao! @Fabryprog, tnx but i was facing the same problem as @pepethree i think, or, at least it wasn't working for me. my solution is now to use node:8.11.1

infinito84 commented 6 years ago

Hi after searching everywhere I found this workaround that works on docker nodejs based on alpine:

Before npm install or yarn install to put this code: RUN wget -qO- "https://github.com/dustinblackman/phantomized/releases/download/2.1.1a/dockerized-phantomjs.tar.gz" | tar xz -C /

elvizcacho commented 6 years ago

After trying @infinito84 solution and adding some extra dependencies I managed to make it work like this on this image node:8.11.3-alpine:

# Add support for https on wget
RUN apk update && apk add --no-cache wget && apk --no-cache add openssl wget && apk add ca-certificates && update-ca-certificates

# Add phantomjs
RUN wget -qO- "https://github.com/dustinblackman/phantomized/releases/download/2.1.1a/dockerized-phantomjs.tar.gz" | tar xz -C / \
    && npm config set user 0 \
    && npm install -g phantomjs-prebuilt

# Add fonts required by phantomjs to render html correctly
RUN apk add --update ttf-dejavu ttf-droid ttf-freefont ttf-liberation ttf-ubuntu-font-family && rm -rf /var/cache/apk/*
cristin commented 6 years ago

Thank you @elvizcacho for this solution. I can confirm it works when using PhantomJS in Docker.

There is still an issue when attaching a volume to the docker service: UnhandledPromiseRejectionWarning: Error: spawn Unknown system error -8

Has anybody encountered this issue before?

elvizcacho commented 6 years ago

@cristin Can you elaborate further what are you trying to do? If you are using html-pdf lib you have to set the phantomPath path on the option object to point to the global installed phantomjs as the example below:

const options = {
  format: 'Legal',
  orientation: 'portrait',
  phantomPath: '/usr/local/bin/phantomjs'
};
sudeepdk commented 5 years ago

This will work 100 % -- follow this it worked for me ...

const options = { format: 'Legal', orientation: 'portrait', phantomPath: '/usr/local/bin/phantomjs' };

https://www.vultr.com/docs/how-to-install-phantomjs-on-ubuntu-16-04

AlessandroAnnini commented 5 years ago

@sudeepdk It does not with alpine..

> Step 2/17 : RUN apk --update --no-cache add   freetype   fontconfig   build-essential   chrpath   libssl-dev   libxft-dev   libfreetype6-dev   libfreetype6   libfontconfig1-dev   libfontconfig1   wget
>  ---> Running in 3018631de6a1
> fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/main/x86_64/APKINDEX.tar.gz
> fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/community/x86_64/APKINDEX.tar.gz
> ERROR: unsatisfiable constraints:
>   build-essential (missing):
>     required by: world[build-essential]
>   libfontconfig1 (missing):
>     required by: world[libfontconfig1]
>   libfontconfig1-dev (missing):
>     required by: world[libfontconfig1-dev]
>   libfreetype6 (missing):
>     required by: world[libfreetype6]
>   libfreetype6-dev (missing):
>     required by: world[libfreetype6-dev]
>   libssl-dev (missing):
>     required by: world[libssl-dev]
> Removing intermediate container 3018631de6a1
fzvincent commented 5 years ago

I experienced this situation with the wrong path of

"runtimeExecutable": "/usr/local/bin/phantomjs",

after I correct it, the situation gone.

lipsakandhei commented 5 years ago

I was having the exact issue in windows. I solved the problem by :

  1. downloading, installing and extracting phantomjs for windows phantomjs-2.1.1-windows.zip from https://phantomjs.org/download.html.
  2. giving the path to the phantomjs.exe in my options variable: var options = { format: 'A4', "phantomPath": "C:\\Users\\HP\\Downloads\\phantomjs-2.1.1-windows\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe" , type: 'pdf',timeout: 30000};
vijaykondamudi commented 5 years ago

@Guys - Any help is appreciated. I am seeing this error which downloading PDF {"err":{"errno":"ENOENT","code":"ENOENT","syscall":"spawn /usr/src/app/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs","path":"/usr/src/app/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs","spawnargs":["--ignore-ssl-errors=yes","/usr/src/app/node_modules/html-pdf/lib/scripts/pdf_a4_portrait.js"]}}

jerryOkafor commented 4 years ago

After trying @infinito84 solution and adding some extra dependencies I managed to make it work like this on this image node:8.11.3-alpine:

# Add support for https on wget
RUN apk update && apk add --no-cache wget && apk --no-cache add openssl wget && apk add ca-certificates && update-ca-certificates

# Add phantomjs
RUN wget -qO- "https://github.com/dustinblackman/phantomized/releases/download/2.1.1a/dockerized-phantomjs.tar.gz" | tar xz -C / \
    && npm config set user 0 \
    && npm install -g phantomjs-prebuilt

# Add fonts required by phantomjs to render html correctly
RUN apk add --update ttf-dejavu ttf-droid ttf-freefont ttf-liberation ttf-ubuntu-font-family && rm -rf /var/cache/apk/*

This one worked for me.

mbarish-me commented 4 years ago

@jerryOkafor @elvizcacho I no longer get the phantomPath error but a new font error Fontconfig warning: line 5: unknown element "its:rules" Fontconfig warning: line 6: unknown element "its:translateRule" Any help appreciated

himanshu-alphonso commented 4 years ago

@jerryOkafor @elvizcacho I no longer get the phantomPath error but a new font error Fontconfig warning: line 5: unknown element "its:rules" Fontconfig warning: line 6: unknown element "its:translateRule" Any help appreciated

Facing the same issue.

aniruddhasm commented 4 years ago

@lipsakandhei I tried what you said but I am getting this error. Error: html-pdf: PDF generation timeout. Phantom.js script did not exit.

const options = {
  format: 'A4',
  phantomPath: ' C:\\Downloads\\ProjectName\\node_modules\\phantomjs-prebuilt\\lib\\phantom\\bin\\phantomjs.exe',
  renderDelay: 'manual'
}

Any assistance is greatly appreciated.

andhareP commented 4 years ago

I am facing same issue. Getting -{"errno":"ENOENT","code":"ENOENT","syscall":"spawn /app/node_modules/phantomjs-prebuilt/lib/phantom\bin\phantomjs.exe","path":"/app/node_modules/phantomjs-prebuilt/lib/phantom\bin\phantomjs.exe","spawnargs":["/app/node_modules/html-pdf/lib/scripts/pdf_a4_portrait.js"]

I tried providing phantom path in options too but didn't help in resolving this issue.

Any help is highly appreciated.

andhareP commented 4 years ago

@guys - Any help is appreciated. I am seeing this error which downloading PDF {"err":{"errno":"ENOENT","code":"ENOENT","syscall":"spawn /usr/src/app/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs","path":"/usr/src/app/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs","spawnargs":["--ignore-ssl-errors=yes","/usr/src/app/node_modules/html-pdf/lib/scripts/pdf_a4_portrait.js"]}}

Were you able to resolve this issue?

andhareP commented 4 years ago

+1 Me too facing the same issue. BTW, I am running my application in docker. Is there any special attention needed.

I am facing same issue while running in docker, how did you fix issue in your case?

dog1133299 commented 4 years ago

After trying @infinito84 solution and adding some extra dependencies I managed to make it work like this on this image node:8.11.3-alpine:

# Add support for https on wget
RUN apk update && apk add --no-cache wget && apk --no-cache add openssl wget && apk add ca-certificates && update-ca-certificates

# Add phantomjs
RUN wget -qO- "https://github.com/dustinblackman/phantomized/releases/download/2.1.1a/dockerized-phantomjs.tar.gz" | tar xz -C / \
    && npm config set user 0 \
    && npm install -g phantomjs-prebuilt

# Add fonts required by phantomjs to render html correctly
RUN apk add --update ttf-dejavu ttf-droid ttf-freefont ttf-liberation ttf-ubuntu-font-family && rm -rf /var/cache/apk/*

docker in node:alpin I change

&& npm install -g phantomjs-prebuilt

to

&& npm uninstall -g phantomjs \
&& npm install phantomjs

that worked for me. thanks!

selvarajprabhu commented 3 years ago

Hi after searching everywhere I found this workaround that works on docker nodejs based on alpine:

Before npm install or yarn install to put this code: RUN wget -qO- "https://github.com/dustinblackman/phantomized/releases/download/2.1.1a/dockerized-phantomjs.tar.gz" | tar xz -C /

worked fantastic

mcworox commented 3 years ago

Hi after searching everywhere I found this workaround that works on docker nodejs based on alpine:

Before npm install or yarn install to put this code: RUN wget -qO- "https://github.com/dustinblackman/phantomized/releases/download/2.1.1a/dockerized-phantomjs.tar.gz" | tar xz -C /

If you have the same problem with node alphine images, THIS IS THE SOLUTION

alihasnain1997 commented 2 years ago
  1. npm uninstall -g phantomjs

didn't worked for me

apurvns commented 2 years ago

Use the following

const phantomPath = require('witch')('phantomjs-prebuilt', 'phantomjs');

pdf.create(options.template, {
    phantomPath: `${phantomPath}`,
    ...
})

I was struggling with this issue since 2 days, many solutions suggesting running phantomjs script inside docker container and uninstalling globally installed one didn't worked either , this one worked. Thanks

alihasnain1997 commented 2 years ago

this is how it worked for me. Docker file:

FROM node:14-alpine

Add a work directory

WORKDIR /app

Cache and Install dependencies

COPY package.json .

Add support for https on wget

RUN apk update && apk add --no-cache wget && apk --no-cache add openssl wget && apk add ca-certificates && update-ca-certificates

Add phantomjs

RUN wget -qO- "https://github.com/dustinblackman/phantomized/releases/download/2.1.1a/dockerized-phantomjs.tar.gz" | tar xz -C / \ && npm config set user 0 \ && npm install -g phantomjs-prebuilt

Add fonts required by phantomjs to render html correctly

RUN apk add --update ttf-dejavu ttf-droid ttf-freefont ttf-liberation && rm -rf /var/cache/apk/*

RUN npm install

RUN npm install

Copy app files

COPY . ./

Expose port

EXPOSE $PORT

CMD [ "npm","start" ]

du-camiloj commented 2 years ago

this is how it worked for me. Docker file:

@alihasnain1997 Did you add any path in the phantomPath?

alihasnain1997 commented 2 years ago

this is how it worked for me. Docker file:

@alihasnain1997 Did you add any path in the phantomPath?

@du-camiloj nope didn't added any path

kasir-barati commented 2 years ago

Use the following

const phantomPath = require('witch')('phantomjs-prebuilt', 'phantomjs');

pdf.create(options.template, {
    phantomPath: `${phantomPath}`,
    ...
})

Hello @JoeChapman Why this way? In my case the phantomjs-prebuilt was in the /path/to/project/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs and the file /path/to/project/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs returned:

phantomjs: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.26, BuildID[sha1]=d0f2a21ff9e0b82113a2095e7cbca7dceaba88fb, stripped

Is there anything wrong with my file?

kasir-barati commented 2 years ago

I have this problem when I use 8.7.0-alpin in my docker file. I got my problem solved just using 8.7.0-wheezy instead, no source code changes.

@kitko112 wheezy has no 16.x images or even 12.x. Did you change the base image again to other linux distros?

kasir-barati commented 2 years ago

phantomPath @mbarish-me Did you try this solution: https://github.com/marcbachmann/node-html-pdf/issues/233#issuecomment-289195523

kasir-barati commented 2 years ago

From the future

I face it again. I am confused now

This Dockerfile fixed my issue:

FROM node:16.14.0-alpine3.15

WORKDIR /app
COPY package*.json ./

# Add support for https on wget
RUN apk update && apk add --no-cache wget && apk --no-cache add openssl wget && apk add ca-certificates && update-ca-certificates

# Add phantomjs
RUN wget -qO- "https://github.com/dustinblackman/phantomized/releases/download/2.1.1a/dockerized-phantomjs.tar.gz" | tar xz -C / \
    && npm config set user 0 \
    && npm install -g phantomjs-prebuilt

# Add fonts required by phantomjs to render html correctly
RUN apk add --update ttf-dejavu ttf-droid ttf-freefont ttf-liberation && rm -rf /var/cache/apk/*

# see this: https://github.com/marcbachmann/node-html-pdf/issues/563#issuecomment-712852134
RUN echo "" > /tmp/openssl.cnf

RUN npm install
# Rest of your Dockerfile, Instructions like COPY . /

Side note:

If npm install -g phantomjs-prebuilt does not work, get rid of this line && npm config set user 0

README fore sure

And that's it, No need to pass phantomPath option to create. We finished. I hope this help you. If your problem does not vanished, do not blame yourself. Computers are stupid enough. Nothing is wrong with you. Just try other solutions and give yourself 10 minute untrack. it will help you

Best wishes.

Dozalex commented 2 years ago

Pain. This Dockerfile fixed it for me. No need to pass phantomPath option.

FROM node:12

WORKDIR /var/www

RUN apt-get update && apt-get install -y autoconf autogen libgl1-mesa-glx

# Rest of your Dockerfile, Instructions like COPY . /

With other node versions i got different errors. Good luck.

akmur commented 2 years ago

@kasir-barati thanks for your solution, FYI I also had to add the following in my options for html-pdf

phantomPath: '/usr/local/bin/phantomjs',
kevinmarangoni commented 2 years ago

From the future

I face it again. I am confused now

This Dockerfile fixed my issue:

FROM node:16.14.0-alpine3.15

WORKDIR /app
COPY package*.json ./

# Add support for https on wget
RUN apk update && apk add --no-cache wget && apk --no-cache add openssl wget && apk add ca-certificates && update-ca-certificates

# Add phantomjs
RUN wget -qO- "https://github.com/dustinblackman/phantomized/releases/download/2.1.1a/dockerized-phantomjs.tar.gz" | tar xz -C / \
    && npm config set user 0 \
    && npm install -g phantomjs-prebuilt

# Add fonts required by phantomjs to render html correctly
RUN apk add --update ttf-dejavu ttf-droid ttf-freefont ttf-liberation && rm -rf /var/cache/apk/*

# see this: https://github.com/marcbachmann/node-html-pdf/issues/563#issuecomment-712852134
RUN echo "" > /tmp/openssl.cnf

RUN npm install
# Rest of your Dockerfile, Instructions like COPY . /

Side note:

If npm install -g phantomjs-prebuilt does not work, get rid of this line && npm config set user 0

README fore sure

And that's it, No need to pass phantomPath option to create. We finished. I hope this help you. If your problem does not vanished, do not blame yourself. Computers are stupid enough. Nothing is wrong with you. Just try other solutions and give yourself 10 minute untrack. it will help you

Best wishes.

Give this man an Oscar

aditya-786 commented 1 year ago

Use the following

const phantomPath = require('witch')('phantomjs-prebuilt', 'phantomjs');

pdf.create(options.template, {
    phantomPath: `${phantomPath}`,
    ...
})

I was struggling with this issue since 2 days, many solutions suggesting running phantomjs script inside docker container and uninstalling globally installed one didn't worked either , this one worked. Thanks

This worked for me. Thanks :)

claudrocker commented 1 year ago

From the future

I face it again. I am confused now

This Dockerfile fixed my issue:

FROM node:16.14.0-alpine3.15

WORKDIR /app
COPY package*.json ./

# Add support for https on wget
RUN apk update && apk add --no-cache wget && apk --no-cache add openssl wget && apk add ca-certificates && update-ca-certificates

# Add phantomjs
RUN wget -qO- "https://github.com/dustinblackman/phantomized/releases/download/2.1.1a/dockerized-phantomjs.tar.gz" | tar xz -C / \
    && npm config set user 0 \
    && npm install -g phantomjs-prebuilt

# Add fonts required by phantomjs to render html correctly
RUN apk add --update ttf-dejavu ttf-droid ttf-freefont ttf-liberation && rm -rf /var/cache/apk/*

# see this: https://github.com/marcbachmann/node-html-pdf/issues/563#issuecomment-712852134
RUN echo "" > /tmp/openssl.cnf

RUN npm install
# Rest of your Dockerfile, Instructions like COPY . /

Side note:

If npm install -g phantomjs-prebuilt does not work, get rid of this line && npm config set user 0

README fore sure

And that's it, No need to pass phantomPath option to create. We finished. I hope this help you. If your problem does not vanished, do not blame yourself. Computers are stupid enough. Nothing is wrong with you. Just try other solutions and give yourself 10 minute untrack. it will help you Best wishes.

Give this man an Oscar

From an alternate future (2023) and using the container image: node:16.20-alpine I had to add:

ENV OPENSSL_CONF=/etc/ssl/

before npm install.

Thank you guys! this a lifesaver post.

DaniSanT17 commented 1 year ago

Thanks @claudrocker, your solution is the only that work for me, for while at least.

mfalconi-perle commented 1 year ago

@claudrocker

running this script causes npm install to fail any ideas why? I even removed the script and now i cant even build anything and keep getting some error about puppeter