0

I'm trying to generate a PDF from an HTML file from the frontend and the users may download the PDF (never be stored on the server).

For this I am using the module: html5-to-pdf

My code is like this:

var pdf = require('html5-to-pdf');
var fs = require('fs');

router.post('/pdf', function(req, res) {
  var html = req.body.html;

  if (!html) {
    return res.sendStatus(400);
  }

  pdf().from.string(html).to.buffer({
    renderDelay: 1000
  }, function(err, data) {
    if (err) {
      return res.sendStatus(500);
    }

    var file = fs.createWriteStream('myDocument.pdf');
    file.write(data, function(err) {
      if (err) {
        res.sendStatus(500);
      }

      res.download('myDocument');
    });
  });

});

Whenever I download a file size of 0Bytes and also creates the file on the server

Someone could help me?

2 Answers 2

1

Maybe it send file before write finish

file.on('end',function(){
    res.download('myDocument');
})
Sign up to request clarification or add additional context in comments.

1 Comment

the callback is 'finish' not 'end'
0

The problem is that html5-to-pdf used phantom to generate the PDF, so it phantom deploys a little server at "localhost" and the port 0, the fact is that OpenShift not recognize "localhost"[1] and if instead of using "localhost" variable is used: process.env.OPENSHIFT_NODEJS_IP the application works correctly.

[1] https://github.com/amir20/phantomjs-node/tree/v1#use-it-in-restricted-enviroments

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.