pofider / phantom-html-to-pdf

Highly scalable html to pdf conversion using phantom workers
MIT License
159 stars 33 forks source link

ECONNREFUSED when running into a Docker Container #50

Closed jmcollin78 closed 7 years ago

jmcollin78 commented 7 years ago

Hi there, I can't make phantom-html-to-pdf works in a dockerized environment. I run the nodeJS docker image named node:7.4-alpine which runs an application containing phantom-html-to-pdf. Each time I try to generate a pdf I've got an exception:

 [2017-01-24 07:27:21.245] [ERROR] cld-apps.services.invoices - Could not generate invoice pdf cause "{ Error: connect ECONNREFUSED 127.0.0.1:34315
  at Object.exports._errnoException (util.js:1022:11)
  at exports._exceptionWithHostPort (util.js:1045:20)
  at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)

  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 34315 }"
[2017-01-24 07:27:21.247] [ERROR] cld-apps.core.express - { Error: connect ECONNREFUSED 127.0.0.1:34315
  at Object.exports._errnoException (util.js:1022:11)
  at exports._exceptionWithHostPort (util.js:1045:20)
  at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)
From previous event:
  at exports.previewInvoiceDoc (/home/clduser/cld-apps/packages/invoices/server/controllers/ctrlInvoiceDoc.js:439:4)
  at Layer.handle [as handle_request] (/home/clduser/cld-apps/node_modules/express/lib/router/layer.js:95:5)
  at next (/home/clduser/cld-apps/node_modules/express/lib/router/route.js:131:13)
  at /home/clduser/cld-apps/packages/system/server/oauth2/middlewares/authorization.js:184:12
  at _combinedTickCallback (internal/process/next_tick.js:67:7)
  at process._tickCallback (internal/process/next_tick.js:98:9)
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 34315 }

Of course this works well outside a container.

Any clues to make it works ?

EDIT: My piece of code:

var conversion = require('phantom-html-to-pdf')({
    /* number of allocated phantomjs processes */
    numberOfWorkers: jmcnetConfig.getInt('commons.phantomJS.poolSize', 2),
    /* timeout in ms for html conversion, when the timeout is reached, the phantom process is recycled */
    timeout: jmcnetConfig.getInt('commons.phantomJS.timeout', 10000),
    /* directory where are stored temporary html and pdf files, use something like npm package reaper to clean this up */
    tmpDir: jmcnetConfig.get('commons.phantomJS.tempDir')
});
  ...
  conversion({
    html: helperInvoice.getHtmlInvoiceFromTemplate(context, templateName, template.template, 'server-messages'),
    header: helperInvoice.getHtmlHeader(context, templateName, template, 'server-messages'),
    footer: helperInvoice.getHtmlFooter(context, templateName, template, 'server-messages'),
    paperSize: {
        format: template.paperSize,
        margin: template.margin,
        orientation: template.orientation,
        headerHeight: template.headerHeight,
        footerHeight: template.footerHeight
    }
}, function (err, pdf) {
    if (err) {
        log.error('Could not generate invoice pdf cause "%s"', dbg(err));  <--- we see this log
        deferred.reject(err);
    } else deferred.resolve(pdf);
});
pofider commented 7 years ago

It is difficult to run phantomjs on alpine based image, because there are too many things missing. You can try to google it out and try something like this to make it working, but at least I was not successful with it.

https://github.com/ncopa/docker-phantomjs-alpine

Perhaps you need to use a bigger image, here is this package working on ubuntu based docker

https://github.com/jsreportonline/phantom-pdf/blob/master/Dockerfile

Please let me know if you make it working on alpine, I would gladly use it.

chevin99 commented 7 years ago

@jmcollin78 did you end up finding a way to make it work with Docker?

jmcollin78 commented 7 years ago

Yes ! The solution was not to use to -alpine base image.