我正在开发一个使用html-pdf
的Node.js应用程序,它在创建给定html
字符串的pdf文件时使用PhantomJS
。
一切正常,但是当我想在Docker
中构建应用程序时,会出现以下错误:
ERROR: Error: spawn /usr/src/app/node_modules/phantomjs-prebuilt/lib/phantom\bin\phantomjs.exe ENOENT
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:785:11)
at Socket._write (net.js:797:8)
at writeOrBuffer (_stream_writable.js:352:12)
at Socket.Writable.write (_stream_writable.js:303:10)
at PDF.PdfExec [as exec] (/usr/src/app/node_modules/html-pdf/lib/pdf.js:141:15)
at PDF.PdfToFile [as toFile] (/usr/src/app/node_modules/html-pdf/lib/pdf.js:83:8)
at /usr/src/app/src/createPDF.js:87:42
at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:63:3)
Emitted 'error' event on Socket instance at:
at emitErrorNT (internal/streams/destroy.js:100:8)
at emitErrorCloseNT (internal/streams/destroy.js:68:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
errno: -32,
code: 'EPIPE',
syscall: 'write'
}
下面是我使用的Dockerfile
:
FROM node:latest
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . .
RUN npm install --production
RUN wget -O /tmp/phantomjs-2.1.1-linux-x86_64.tar.bz2 https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
RUN mkdir -p /usr/local/lib/node_modules/phantomjs/lib/phantom/
RUN tar xvjf /tmp/phantomjs-2.1.1-linux-x86_64.tar.bz2 -C /tmp/phantomjs
RUN mv /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64/* /usr/local/lib/node_modules/phantomjs/lib/phantom/
RUN rm -rf /tmp/phantomjs-2.1.1-linux-x86_64.tar.bz && rm -rf /tmp/phantomjs
EXPOSE 8080
CMD ["npm","run","serve"]
我不喜欢手动安装PhantomJS
,但这是我在网上找到的唯一解决方案。
我还查看了其他问题,并尝试在选项中通过phantom
的path
,但不起作用。
发布于 2020-08-26 18:20:59
对于将来面临这个问题的任何人,这里是我设法解决这个问题的方法:
FROM node:latest
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . .
RUN npm install --production
RUN npm install -g phantomjs --unsafe-perm
EXPOSE 8080
CMD ["npm","run","serve"]
然后使用requireg
在选项中传递phantomjs
的全局路径
var options = { phantomPath: require('requireg')('phantomjs').path }
https://stackoverflow.com/questions/63594283
复制相似问题