TypeError:无法读取生产中未定义的属性(阅读'pipe')(Heroku服务器)

chy5wohz  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(73)

我有整个事情在本地工作,但失败,只要它在heroku服务器上。

实现

await pdf.create(pdfTemplate(data)).toStream(async function (err, stream) {
      await stream.pipe(fs.createWriteStream(`${req.body.phone}.pdf`));
      const params = {
        Bucket: "icon-path-bucket",
        Body: stream,
        Key: req.body.phone,
        contentType: "application/pdf"
      }

字符串

错误(生产中)

2023-06-17T10:16:06.144530+00:00 app[web.1]:       await stream.pipe(fs.createWriteStream(`${req.body.phone}.pdf`));
2023-06-17T10:16:06.144530+00:00 app[web.1]:                    ^
2023-06-17T10:16:06.144530+00:00 app[web.1]: 
2023-06-17T10:16:06.144531+00:00 app[web.1]: TypeError: Cannot read properties of undefined (reading 'pipe')

wb1gzix0

wb1gzix01#

html-pdf已经被弃用了,所以我可以在heroku构建包https://github.com/jontewks/puppeteer-heroku-buildpack中使用puppeteer。具体实现如下:

let browser = null

    try {
      browser = await puppeteer.launch({
        args: ['--no-sandbox'],
      });

      const page = await browser.newPage();

      await page.setContent(pdfTemplate(data));

      await page.pdf({ path: `${req.body.phone}.pdf` });

      console.log('PDF created successfully:', `${req.body.phone}.pdf`);

      console.log('PDF created successfully.');

      const fileData = fs.readFileSync(`${req.body.phone}.pdf`);
      const params = {
        Bucket: "icon-path-bucket",
        Body: fileData,
        Key: req.body.phone,
        ContentEncoding: "base64",
        contentType: "application/pdf"
      }

      // console.log("loooooooooooonnnng body", params.Body)

      s3.upload(params, function (err, data) {
        if (err) {
          console.log('Error uploading file:', err);
        } else {
          database.collection("MfmRegistration").insertOne(
            {
              firstName: req.body.fName,
              lastName: req.body.lName,
              email: req.body.email,
              filePath: `/api/download/${data.key}`
            },
            (err, data) => {
              res.redirect(`/success?message=${req.body.phone}`);
            }
          );
        }
      });

      // Close the browser
      await browser.close();

    } catch (err) {

      console.error('Error generating PDF:', err);

    } finally {

      // Close the browser
      if (browser !== null) {
        await browser.close();
      }

    }

字符串
如果没有heroku构建包,即使它在本地工作,它仍然会在服务器上失败。或服务器的正确配置:参见https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#could-not-find-expected-browser-locally

相关问题