简体   繁体   中英

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

In Heroku a dyno's local file storage is not persistent (besides the git repo files obviously), so if you write a local file and the dyno restarts the file will be gone, and if you start another dyno it won't be able to "see" the file.

heroku run bash starts a new "one-off" dyno (can read about it here: https://devcenter.heroku.com/articles/one-off-dynos ), so the file will not be accessible that way.

If you want your data to persist, better use some database or persistent storage addon.

(quote from node.js createWriteStream doesn't create new file on Heroku )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM