2017-11-06 10 views
6

私は、PDFMake(PDFKitの変形)を使用して、リアルタイムデータベーストリガを使用してFirebase Cloud機能でPDFを生成しています。この関数は、データベースから関連するすべてのデータを取得し、それをPDFを生成する関数に渡します。Firebase Cloud機能でPDFMakeとの約束を使用

すべてこれは約束を使用して行われます。 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}`); 
    }); 
}; 

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

間違っている可能性が何をここに約束し、それによって二重引用符を防止しています意図したとおりに実行されるコードですか? firebaseログオン

、私が見るすべてはあなたが成功したPDFのためのバッファを作成しているが、あなたが実際にそれを返していないように見える、実行時間やエラーの欠如に基づいてFunction execution took 3288 ms, finished with status: 'ok'

答えて

3

です関数。上記のコードで

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

、あなたは次のthenブロックに結果を渡しますが、そのブロックから、undefinedを返します。このPromiseチェーンの最終結果は未定義です。結果を渡すには、Promiseチェーンの最後に返すことをお勧めします。

.then((profile) => { 
    return quotPdfHelper.generatePDF(fonts, profile, quotationData, storage); 
}) 
.then(buffer => { 
    console.log('Generation Successful. Pass for email'); 
    return buffer; 
}) 
関連する問題