错误:在服务器中生成ENOENT html pdf nodejs



使用html pdf创建pdf时遇到问题。在我的本地运行良好,但在服务器上无法工作。我下面的代码:

const fileName = './public/offering/' + dayjs().format('YYYY-MM-DD') + '-' + name[0] + '.pdf';
const htmlToPdfOptions = {
type: 'PDF',
height: '1200px',
width: '816px',
renderDelay: 2000,
format: 'Letter',
phantomPath: require('requireg')('phantomjs-prebuilt').path,
};
pdf.create(file, htmlToPdfOptions).toFile(fileName, async function (err: any, result: any) {
if (err) return console.log(err, 'xxxxxxxxxxxxxx this error');
console.log(result)
});

在我的码头上,就像这个

FROM node:14-alpine
RUN npm install --global pm2
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . .
COPY package*.json ./
COPY . .
RUN npm install && npm run build
RUN npm install -g phantomjs --unsafe-perm
COPY . .
EXPOSE 8500
CMD ["pm2-runtime", "start", "src/index.js"]

服务器错误为

Error: spawn /usr/src/app/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:269:19)
at onErrorNT (internal/child_process.js:467:16)
at processTicksAndRejections (internal/process/task_queues.js:82:21) {
errno: -2,
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: [
'--local-url-access=false',
'/usr/src/app/node_modules/html-pdf/lib/scripts/pdf_a4_portrait.js'
]
}

有人能帮我吗?因为我已经在网上尝试了一些解决方案,但什么都不起作用,或者任何人都可以给我参考?。示例:Docker中的Nodejs+PhantomJS:错误:spawn-ENOENT

您的Dockerfile有一些奇怪的copie命令。这应该可以解决你的问题

FROM node:14-alpine
RUN npm install --global pm2
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . .
RUN npm install && npm run build
RUN npm install -g phantomjs --unsafe-perm
EXPOSE 8500
CMD ["pm2-runtime", "start", "src/index.js"]

您还应该将node_modules添加到.dockerignore文件中。

最新更新