在Firebase云功能上使用PDFMake的承诺

Gau*_*ani 7 pdfkit node.js promise firebase pdfmake

我正在使用PDFMake(一种变体PDFKit)使用实时数据库触发器在Firebase云功能上生成PDF.该函数从数据库获取所有相关数据,然后将其传递给应该生成PDF的函数.

所有这些都是使用Promises完成的.一切正常,直到实际生成PDF.

这是我的主要事件监听器中的代码:

exports.handler = (admin, event, storage) => {
  const quotationData = event.data.val();
  // We must return a Promise when performing async tasks inside Functions
  // Eg: Writing to realtime db
  const companyId = event.params.companyId;
  settings.getCompanyProfile(admin, companyId)
  .then((profile) => {
    return quotPdfHelper.generatePDF(fonts, profile, quotationData, storage);
  })
  .then(() => {
    console.log('Generation Successful. Pass for email');
  })
  .catch((err) => {
    console.log(`Error: ${err}`);
  });
};
Run Code Online (Sandbox Code Playgroud)

要生成PDF,这是我的代码:

exports.generatePDF = (fonts, companyInfo, quotationData, storage) => {
  const printer = new PdfPrinter(fonts);
  const docDefinition = {
    content: [
      {
        text: [
          {
            text: `${companyInfo.title}\n`,
            style: 'companyHeader',
          },
          `${companyInfo.addr_line1}, ${companyInfo.addr_line2}\n`,
          `${companyInfo.city} (${companyInfo.state}) - INDIA\n`,
          `Email: ${companyInfo.email} • Web: ${companyInfo.website}\n`,
          `Phone: ${companyInfo.phone}\n`,
          `GSTIN: ${companyInfo.gst_registration_number}  • PAN: AARFK6552G\n`,
        ],
        style: 'body',
         //absolutePosition: {x: 20, y: 45}
      },
    ],
    styles: {
      companyHeader: {
        fontSize: 18,
        bold: true,
      },
      body: {
        fontSize: 10,
      },
    },
    pageMargins: 20,
  };
  return new Promise((resolve, reject) => {
    // const bucket = storage.bucket(`${PROJECT_ID}.appspot.com`);
    // const filename = `${Date.now()}-quotation.pdf`;
    // const file = bucket.file(filename);
    // const stream = file.createWriteStream({ resumable: false });
    const pdfDoc = printer.createPdfKitDocument(docDefinition);
    // pdfDoc.pipe(stream);

    const chunks = [];
    let result = null;

    pdfDoc.on('data', (chunk) => {
      chunks.push(chunk);
    });
    pdfDoc.on('error', (err) => {
      reject(err);
    });
    pdfDoc.on('end', () => {
      result = Buffer.concat(chunks);
      resolve(result);
    });
    pdfDoc.end();
  });
};
Run Code Online (Sandbox Code Playgroud)

这里可能出现的问题是阻止承诺,从而使报价代码按预期执行?

在firebase日志中,我看到的只是 Function execution took 3288 ms, finished with status: 'ok'

rro*_*and 4

根据执行时间和没有错误,看起来您已成功为 PDF 创建缓冲区,但实际上并未从函数返回它。

.then((profile) => {
  return quotPdfHelper.generatePDF(fonts, profile, quotationData, storage);
})
.then(() => {
  console.log('Generation Successful. Pass for email');
})
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,您将结果传递到下一个then块,但随后从该块返回未定义。该 Promise 链的最终结果将是未定义的。要传递结果,您需要将其返回到 Promise 链的末尾:

.then((profile) => {
  return quotPdfHelper.generatePDF(fonts, profile, quotationData, storage);
})
.then(buffer => {
  console.log('Generation Successful. Pass for email');
  return buffer;
})
Run Code Online (Sandbox Code Playgroud)