我编写了一个 firebase 云函数,它使用节点的 pdfkit 模块生成 pdf 文件。但是上传到云存储的文件没有任何页面。我不知道天气问题与 pdfkit、目录或 pip 有关。这是我的功能的完整代码。
import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
import * as PDFDocument from 'pdfkit'
import * as fs from 'fs'
import * as mkdirp from 'mkdirp-promise'
import * as path from 'path'
import * as os from 'os'
admin.initializeApp();
export const downloadPdf = functions.https.onCall(async(data, context) => {
const acc = data.accountNo;
const bank = data.bank;
const doc = new PDFDocument();
const filePath = "PDF_A"+Date.now()+".pdf";
const baseFileName = path.basename(filePath, path.extname(filePath));
const fileDir = path.dirname(filePath);
const PDFFilePath = path.normalize(path.format({dir: fileDir, name: baseFileName, ext: '.pdf'}));
const tempLocalFile = path.join(os.tmpdir(), filePath);
const tempLocalDir = path.dirname(tempLocalFile);
const tempLocalPDFFile = path.join(os.tmpdir(), PDFFilePath);
await mkdirp(tempLocalDir);
const pdfStream = fs.createWriteStream(tempLocalFile);
doc.pipe(pdfStream);
try {
const dataSnapshot = await admin.database().ref(`/Transactions/${bank}/${acc}`).once('value');
doc.fontSize(18);
doc.text('Transactions',100,100).moveDown();
dataSnapshot.forEach(childSnapshot => {
doc.fontSize(14);
//Using a standard PDF font
doc.text(`Transaction id:${childSnapshot.key}`,100,100).moveDown();
return false
});
doc.end();
const bucket = admin.storage().bucket();
await pdfStream.addListener('finish', function() {
// HERE PDF FILE IS DONE
});
await bucket.upload(tempLocalPDFFile, { destination: PDFFilePath });
fs.unlinkSync(tempLocalPDFFile);
fs.unlinkSync(tempLocalFile);
return { url: 'someurl' };
//console.log('PDF uploaded to Storage at', PDFFilePath);
} catch (error) {
//throw new functions.https.HttpsError('Unknown', error.message);
return { url: error.message };
}
});
我该如何解决这个问题?