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 545 forks source link

docker node:12.20.0-alpine3.9,write EPIPE #612

Open zbuzhi92 opened 3 years ago

zbuzhi92 commented 3 years ago

/usr/src/app # html-pdf index.html index.pdf events.js:291 throw er; // Unhandled 'error' event ^

Error: write EPIPE at afterWriteDispatched (internal/stream_base_commons.js:156:25) at writeGeneric (internal/stream_base_commons.js:147:3) at Socket._writeGeneric (net.js:787:11) at Socket._write (net.js:799:8) at doWrite (_stream_writable.js:403:12) at writeOrBuffer (_stream_writable.js:387:5) at Socket.Writable.write (_stream_writable.js:318:11) at PDF.PdfExec [as exec] (/usr/local/lib/node_modules/html-pdf/lib/pdf.js:136:22) at PDF.PdfToFile [as toFile] (/usr/local/lib/node_modules/html-pdf/lib/pdf.js:83:8) at htmlpdf (/usr/local/lib/node_modules/html-pdf/bin/index.js:29:29) Emitted 'error' event on Socket instance at: at errorOrDestroy (internal/streams/destroy.js:108:12) at onwriteError (_stream_writable.js:418:5) at onwrite (_stream_writable.js:445:5) at internal/streams/destroy.js:50:7 at Socket._destroy (net.js:680:5) at Socket.destroy (internal/streams/destroy.js:38:8) at afterWriteDispatched (internal/stream_base_commons.js:156:17) at writeGeneric (internal/stream_base_commons.js:147:3) at Socket._writeGeneric (net.js:787:11) at Socket._write (net.js:799:8) { errno: 'EPIPE', code: 'EPIPE', syscall: 'write' }

zbuzhi92 commented 3 years ago

write EPIPE

zbuzhi92 commented 3 years ago

write EPIPE

phuoc-le commented 3 years ago

Please add the scripts to the Dockerfile: RUN apk add fontconfig ttf-dejavu 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

I did it and it works well. (node:12.20-alpine3.12)

marcbachmann commented 3 years ago

Yes, looks like the bundled phantomjs doesn't work on newer linux versions anymore. A manual install is necessary.

crazyoptimist commented 3 years ago

Worked for me with node:14-alpine Thanks!

lorenzogrv commented 3 years ago

Can confirm @phuoc-le solution worked for me with node:12-alpine Thanks :love_you_gesture:

jonesmac commented 3 years ago

FWIW - I didn't need these commands:

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 &&

Once the container got the dependencies from dockerized-phantomjs, it all worked as expected.

zander-br commented 3 years ago

Example using node:14-alpine

FROM node:14-alpine

WORKDIR /app
COPY package.json ./
RUN npm install
COPY . .

ENV PHANTOMJS_VERSION=2.1.1
ENV PHANTOMJS_PATH=/usr/local/bin/phantomjs
RUN apk update && apk add --no-cache fontconfig curl curl-dev && \
    cd /tmp && curl -Ls https://github.com/dustinblackman/phantomized/releases/download/${PHANTOMJS_VERSION}/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-${PHANTOMJS_VERSION}-linux-x86_64.tar.bz2 | tar -jxf - && \
    cp phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs

CMD ["npm", "start"]

Code

const pdf = require('html-pdf');
const fs = require('fs');

const html = fs.readFileSync('./businesscard.html', 'utf8');
const options = { 
  format: 'Letter',
};
const createOptions = process.env.PHANTOMJS_PATH ? 
  {...options, phantomPath: process.env.PHANTOMJS_PATH} 
  : options;
pdf.create(html, createOptions).toFile('./businesscard.pdf', function(err, res) {
  if (err) return console.log(err);
  console.log(res); // { filename: '/app/businesscard.pdf' }
});