nodejs wkhtmltopdf在docker中崩溃

时间:2019-02-28 06:00:39

标签: node.js docker wkhtmltopdf

以下是我在nodejs中使用wkhtmltopdf的代码。 通过访问localhost:4001/pdf可以正确下载pdf,并且该应用不会崩溃。

router.get('/', function(req, res, next) {
    let htmlContent = "<p>whatever</p>";
    wkhtmltopdf(htmlContent, {
        output: 'demo.pdf',
        pageSize: 'A4'
    });

    res.download('demo.pdf'); 
}

但是我将应用程序包装在docker映像中后,仍然可以正确下载pdf ,但是应用程序崩溃了,从而引发错误:QXcbConnection

这是dockerfile。

FROM node:8
WORKDIR /usr/src/app

RUN apt-get update && apt-get install -y wkhtmltopdf
COPY package*.json ./
RUN npm install

COPY . .
EXPOSE 4001
CMD [ "npm", "start" ]

这是错误

GET /pdf 304 27.978 ms - -
events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: QXcbConnection: Could not connect to display 

wkhtmltopdf exited with code 134
    at handleError (/usr/src/app/node_modules/wkhtmltopdf/index.js:142:16)
    at ChildProcess.<anonymous> (/usr/src/app/node_modules/wkhtmltopdf/index.js:114:7)
    at emitTwo (events.js:126:13)
    at ChildProcess.emit (events.js:214:7)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:198:12)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! pdfdownloader@0.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the pdfdownloader@0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2019-02-28T05_47_34_845Z-debug.log

0 个答案:

没有答案