Thursday, 13 April 2017

nodejs html-pdf not working on heroku

I've a fully functioning nodejs code that on some user actions generates a pdf, emails the pdf to user and uploads the pdf to dropbox. It works perfectly on localhost but as i deploy my code to heroku, i get some errors. I found the error was apparently because of pdf generation. I've used html-pdf for generating pdf from my ejs template. The code is below:

if (req.body.text == "Approved"){
          ejs.renderFile('./views/template.ejs', {user: user}, function(err, result) {
          if (result) {
             var filename = user.number+ ".pdf";
             var path = './public/files/'+filename ;
             var options = { filename: path, format: 'Legal', orientation: 'portrait', directory: './public/files/',type: "pdf" };
             html = result;
             pdf.create(html, options).toFile(function(err, res) {
             if (err) return console.log(err);
                  console.log(res);
             });
             user.path = path;
             user.save()
             var dbx = new dropbox({ accessToken: mytoken });
             fs.readFile( path,function (err, contents) {
                 if (err) {
                   console.log('Error: ', err);
                 }
                 dbx.filesUpload({ path: "/"+filename ,contents: contents })
                 .then(function (response) {
                        console.log("done")
                        console.log(response);
                  })
                  .catch(function (err) {
                         console.log(err);
                  });
              });

              var mailOptions = {
                                   from: '"EMediCare"',
                                   to: user.email, // list of receivers
                                   subject: 'Confirmation: Quote received', // Subject line
                                   attachments : [{
                                   filename: filename,
                                   path : path
                                   }]
                                 };
              transporter.sendMail(mailOptions, (error, info) => {
              if (error) {
                   return console.log(error);
              }
              console.log('Message %s sent: %s', info.messageId, info.response);

              });
            }
                    // render or error
            else {
                       res.end('An error occurred');
                       console.log(err);
            }

    });
}
        else {
            user.path = null;
            user.save();
        }

My sample template.ejs is :

<html>
<head>
<title>my pdf </title>
</head>
<body>
   <%= user.first_name%> 
   <span><%= user.last_name%></span>
 </body> 

When i checked my log on heroku it says it cannot open the file



via scrpaingnoob

No comments:

Post a Comment