首页 > 解决方案 > 无法使用 pdfkit 和 node.js 将图像从 firebase 写入 pdf

问题描述

尝试使用来自 firebase 的数据生成 pdf,但无法将图像写入 pdf。主函数是一个异步函数,该函数有等待操作符,用于获取图像。但面临以下错误:

错误:EOF 后的 stream.push()

请指教。

我尝试为用于获取图像的 axios 添加等待,甚至尝试将其写入异步函数中。然后我尝试在主函数之外编写整个内容并调用它,但再次显示 EOF 问题。

//this is the main function //
async function getPDF() {

...
...

//This is the part where I try to write images from firebase ///

await questionNumber.get().then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
            i++;
           var exteriorQuestionsData = doc.data();
 for(x=0;x<extFormDataAnswers[i-11].exteriorQuestionsImages.length;x++){

if(extFormDataAnswers[i-11].exteriorQuestionsImages!= undefined || extFormDataAnswers[i-11].exteriorQuestionsImages!= [] ) {

              img_url = extFormDataAnswers[i-11].exteriorQuestionsImages[x].uri
              axios.get(img_url, { responseType: 'arraybuffer' }).then(response => {
                var my_img = Buffer.from(response.data);
                pdf_doc.image(my_img,{ fit: [300, 300] })                 
                .fontSize(18)
                .fillColor('#426998')
                .font('Helvetica-Bold')
                .moveDown(0.2)
                .lineWidth(0.5); 
              });   
             }
           }   

            });
          });

....
....

预期的结果是图像应该显示在pdf中但遇到以下错误Error: stream.push() after EOF

标签: javascriptnode.jsfirebasepdfkit

解决方案


推荐阅读