0

I'm dabbling with Node and trying to get a basic web server setup that'll accept HTML and return either a PDF or an image depending on the route used.

The below works when running it on my local machine. I've popped it onto two different servers, one using Apache and the other using Nginx. In both of those it's failing to return an image or a PDF. The PDF route returns a 502 and the image route returns an empty image.

It's possible I'm going about something the wrong way, but I'm somewhat at a loss right now as to what I'm missing. Any pointers would be greatly appreciated.

var url = require('url');
var http = require('http');
var wkhtmltox = require('wkhtmltox');
var converter = new wkhtmltox();

// Locations of the binaries can be specified, but this is
// only needed if the programs are located outside your PATH
// converter.wkhtmltopdf   = '/opt/local/bin/wkhtmltopdf';
// converter.wkhtmltoimage = '/opt/local/bin/wkhtmltoimage';

http.createServer(function (req, res) {
  console.log(url.parse(req.url, true).query);
  if (req.url == "/pdf") {
    res.writeHead(200, {'Content-Type': 'application/pdf'});
    converter.pdf(req, url.parse(req.url, true).query).pipe(res);
  } else if (req.url == "/image") {
    res.writeHead(200, {'Content-Type': 'image/png'});
    converter.image(req, { format: "png" , quality: 75 }).pipe(res);
  } else {
    res.end('Control is an illusion.');
  }
}).listen(1337, '127.0.0.1');

console.log('Server running at http://127.0.0.1:1337/');

This is the error logged on the server for the PDF route. No error is logged for the image route.

Error: This socket has been ended by the other party
    at Socket.writeAfterFIN [as write] (net.js:285:12)
    at performWork (/var/www/app_name/node_modules/wkhtmltox/lib/wkhtmltox.js:98:22)
    at wkhtmltox.pdf (/var/www/app_name/node_modules/wkhtmltox/lib/wkhtmltox.js:113:16)
    at Server.<anonymous> (/var/www/app_name/index.js:16:14)
    at emitTwo (events.js:106:13)
    at Server.emit (events.js:191:7)
    at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:543:12)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:105:23)

I was testing using curl.

curl -d @file_to_render.html -s "http://localhost:1337/pdf" -o test.pdf
curl -d @file_to_render.html -s "http://localhost:1337/image" -o test.png
4

2 に答える 2

-1

パイプに onError イベントを追加してみてください

converter.image(req, { format: "png" , quality: 75 }).pipe(res).on('error', function(e){ console.log(e); });
于 2016-07-16T16:01:05.260 に答える