0

I'm using the html-pdf module to generate pdfs, using the following code:

var ejs = require('ejs');
var fs = require('fs');
var pdf = require('html-pdf');

var build = function (template, data) {
    var html = ejs.compile(fs.readFileSync(template, 'utf8'));
    html = html(data);

    return html;
}

pdf.create(build('views/print_templates/pm_export_pdf.ejs',
                {rows:resolve(rows, user_rows, table_rows,
                 subproject_rows, milestone_rows, release_rows)}), pdfconfig)
.toFile(function (err, pdf) {
    if (err)
        return console.log(err); 

    res.sendFile(pdf.filename);
});

PDFconfig is the config variable for html-pdf. And resolve is my own db resolve function, which is not very relevant in this story.

When I run this locally on OSX 10.10 on my MacBook Pro this works like a charm. But when I run this on my server(centOS), I get the following error:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: write EPIPE
    at errnoException (net.js:904:11)
    at Object.afterWrite (net.js:720:19)

Has this something to do with permissions maybe? I'm not really sure what I'm doing wrong..

I run the following versions:

  • Node: 0.10.31
  • Express: 4.10.7
  • html-pdf: 1.2.0
Billy
  • 1,104
  • 8
  • 22
  • Are you running your app on your server as a standalone Node process, or through a proxy/nginx/Apache/...? – robertklep Jun 03 '15 at 08:54
  • I run it as a standalone process, with nodemon. I also tried running the process as sudo, without success. – Billy Jun 03 '15 at 09:00
  • `EPIPE` suggests that one side (probably the client) is closing the connection while the file is still being sent. Are you using a version of Express prior to 4.10? If so, [this issue](https://github.com/strongloop/express/issues/2433) may be what's causing your problem. – robertklep Jun 03 '15 at 09:03
  • Thanks for your effort, I already found that issue indeed, I'm using version 4.10.7 of Express. However, if this was the problem, why would this only happen on the CentOS server? I use the exact same versions of everything I use on my local machine. – Billy Jun 03 '15 at 09:07
  • There's just enough differences between OS X and Linux to perhaps cause these kinds of issues. Have you tried using `res.sendFile()` on a large static (not generated by `html-pdf`) file to see if that works better? – robertklep Jun 03 '15 at 09:11
  • I also use res.sendFile() for handling file previews in other parts of my application, I can send images and zips without any issue (30+ mb). – Billy Jun 03 '15 at 09:26
  • One last possible solution might be to see if, instead of using `.toFile()`, `.toStream()` works better: `pdf.create(...).toStream(function(err, stream) { ...; stream.pipe(res); })` (you should add error handling and perhaps set the `Content-Type` header) – robertklep Jun 03 '15 at 09:38
  • @robertklep Thanks Robert, I should add error handling indeed. I figured that my sonicwall drops an [RST, ACK] package as soon as I open a filestream.. So I think I have to take a look at my firewall.. Stupid things right? ;-) I will update this as an answer as soon as I am sure that this is the problem. – Billy Jun 03 '15 at 13:37

1 Answers1

2

For future purposes:

I figured that I had to rebuild my node-modules. I think I accidentally copied the node-modules folder over to the test server on centOS, and phantomjs had build it's dependencies for OSX. I'm still not sure how the error that it threw correspondents with that, but it fixed it.

Also be sure that you have FreeType and fontconfiglib installed.

Billy
  • 1,104
  • 8
  • 22