pofider / phantom-html-to-pdf

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

Debug: internal, implementation, error #11

Closed PaddyMann closed 9 years ago

PaddyMann commented 9 years ago

Now trying to run my server on an AWS instance running ubuntu, and hitting:

Debug: internal, implementation, error
    TypeError: Uncaught error: Cannot read property 'stream' of undefined
    at /var/apps/pdf-generator/index.js:88:14
    at /var/apps/pdf-generator/node_modules/phantom-html-to-pdf/lib/dedicatedProcessStrategy.js:31:24
    at ChildProcess.exithandler (child_process.js:641:7)
    at ChildProcess.EventEmitter.emit (events.js:98:17)
    at maybeClose (child_process.js:743:16)
    at Process.ChildProcess._handle.onexit (child_process.js:810:5)

Here's my simple server:

'use strict';

var Hapi = require('hapi');
var htmlToPdf = require('phantom-html-to-pdf')({
  numberOfWorkers: 6,
  strategy: 'dedicated-process'
});

var server = new Hapi.Server();
server.connection({
  // host: 'localhost',
  port: 3000
});

// Inert required to return a file: http://hapijs.com/tutorials/serving-files
server.register(require('inert'), function (err) {
  // Add the route
  server.route([
    {
      method: 'GET',
      path: '/pdf',
      handler: function (request, reply) {
        generatePdf(request.url.query.url, reply);
      }
    }
  ]);

  // Start the server
  server.start(function () {
    console.log('Server running at:', server.info.uri);
  });
});

function generatePdf (url, reply) {
  console.log('Rendering', url);

  htmlToPdf({
    url: url
  }, function(err, pdf) {
    reply(pdf.stream).type('application/pdf');
  });
}

Any ideas what could be leading to this issue?

Unlike locally, changing the strategy doesn't seem to make any difference :(

pofider commented 9 years ago

Could you please log what is the actual error?

function generatePdf (url, reply) {
  console.log('Rendering', url);
  htmlToPdf({
    url: url
  }, function(err, pdf) {
    //add missing error handling
    if (err)
       console.log(err);
    reply(pdf.stream).type('application/pdf');
  });
}
PaddyMann commented 9 years ago

Yup apologies - had intended to add that but had a server issue directly after the first post...

The error is: { [Error: socket hang up] code: 'ECONNRESET' }

pofider commented 9 years ago

Unfortunately I am out of the office until Sunday, so I cannot try to verify this. The test for url based conversion: https://github.com/pofider/phantom-html-to-pdf/blob/master/test/test.js#L51

phantomjs may need to install some additional packages to work properly on ubuntu, you can try this:

sudo apt-get install build-essential chrpath git-core libssl-dev libfontconfig1-dev
PaddyMann commented 9 years ago

Just a quick heads up - my attention has been diverted onto other areas this week, but I'll be coming back onto this (and items in my other issue threads) next week! Thanks for your help this wk.

PaddyMann commented 9 years ago

Apologies, this was a schoolboy error on my part. Uploaded the node-modules dir rather than rerunning npm install on linux. It's now working!