0

我正在涉足 Node 并试图获得一个基本的 Web 服务器设置,该设置将接受 HTML 并根据使用的路由返回 PDF 或图像。

以下在我的本地机器上运行时有效。我把它放到了两台不同的服务器上,一台使用 Apache,另一台使用 Nginx。在这两种情况下,它都无法返回图像或 PDF。PDF 路由返回 502,图像路由返回空图像。

我可能会以错误的方式处理某些事情,但我现在对我所缺少的有点不知所措。任何指针将不胜感激。

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/');

这是在服务器上为 PDF 路由记录的错误。没有为图像路由记录错误。

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)

我正在使用 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

请试试这个命令 我已经通过这个命令解决了

sudo apt-get install libfontconfig
于 2018-04-25T07:57:39.163 回答
-1

尝试将 onError 事件添加到管道

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