簡體   English   中英

nodejs html-pdf在heroku上不起作用

[英]nodejs html-pdf not working on heroku

我有一個功能完備的nodejs代碼,該代碼在某些用戶操作上會生成pdf,將pdf通過電子郵件發送給用戶,並將pdf上傳到保管箱。 它在localhost上完美運行,但是當我將代碼部署到heroku ,出現一些錯誤。 我發現錯誤顯然是由於pdf生成。 我已經使用html- pdf從我的ejs模板生成pdf。 代碼如下:

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();
        }

我的樣例template.ejs是:

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

當我在heroku上檢查我的日志時,它說它無法打開文件

在Heroku中,dyno的本地文件存儲不是持久性的(顯然git repo文件除外),因此,如果您編寫本地文件並且dyno重新啟動,則該文件將消失,並且如果您啟動另一個dyno,它將無法“請參閱”文件。

heroku run bash啟動一個新的“一次性” dyno(可在此處閱讀: https : //devcenter.heroku.com/articles/one-off-dynos ),因此該文件將無法通過這種方式訪問​​。

如果要保留數據,最好使用一些數據庫或持久性存儲插件。

(來自node.js的引用createWriteStream不會在Heroku上創建新文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM