简体   繁体   English

nodejs html-pdf在heroku上不起作用

[英]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. 我有一个功能完备的nodejs代码,该代码在某些用户操作上会生成pdf,将pdf通过电子邮件发送给用户,并将pdf上传到保管箱。 It works perfectly on localhost but as i deploy my code to heroku , i get some errors. 它在localhost上完美运行,但是当我将代码部署到heroku ,出现一些错误。 I found the error was apparently because of pdf generation. 我发现错误显然是由于pdf生成。 I've used html-pdf for generating pdf from my ejs template. 我已经使用html- pdf从我的ejs模板生成pdf。 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 : 我的样例template.ejs是:

<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 当我在heroku上检查我的日志时,它说它无法打开文件

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中,dyno的本地文件存储不是持久性的(显然git repo文件除外),因此,如果您编写本地文件并且dyno重新启动,则该文件将消失,并且如果您启动另一个dyno,它将无法“请参阅”文件。

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. heroku run bash启动一个新的“一次性” dyno(可在此处阅读: https : //devcenter.heroku.com/articles/one-off-dynos ),因此该文件将无法通过这种方式访问​​。

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 ) (来自node.js的引用createWriteStream不会在Heroku上创建新文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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