因此,目前,我正在尝试从Firebase数据库中的许多数据生成PDF(使用Node.js,PDFKit和GCF)。这些项目之一是图像URL,但PDF套件需要使用base64格式。目前正在创建PDF,但是我遇到了大约6个“忽略已完成函数的异常”的调用。
我认为这与base64函数有关,但我似乎无法确定问题出在哪里……该函数在触发onCreate之后返回其结果,但我认为“ await”是要停止这个?有帮助/想法吗?
exports.onAppComplete = functions.database.ref("users/{appId}").onCreate((snapshot, context) =>{
var appId = context.params.appId;
var appDb = admin.database().ref(`Applications/${appId}`);
return Promise.all([appDb.once('value'),appId]).then(([applicationDb, appId])=>{
var application = {
appId:appId,
dogId:applicationDb.child("dogId").val(),
userId: applicationDb.child("appWith").val(),
};
const Logo = admin.database().ref(`users/logouri`);
return Promise.all([Logo.once('value')]).then(([Logo])=>{
const PDFDocument = require('pdfkit');
let doc = new PDFDocument({ margin: 72 });
doc.size = [595.28, 841.89];
doc
.image('./images/logo.png', 50, 45, { width: 100 })
.fillColor("#444444")
.moveDown(1);
var logoUri = Logo.val();
console.log("pictureURL",logoUri);
toBase64(logoUri,50,50,doc);
const myPdfFile = admin.storage().bucket().file('/test/Arbeitsvertrag.pdf');
const stream = doc.pipe(myPdfFile.createWriteStream());
doc.end();
return console.log('Successfully made App PDF:', "OK");
})
});
});
function toBase64(url, x, y, pdfdoc) {
return new Promise(async (resolve, reject)=>{
var https = require('https');
var req = await https.get(url, (res) => {
res.setEncoding('base64');
let body = "data:" + res.headers["content-type"] + ";base64,";
console.log("BODY", body)
res.on('data', (d) => {
body += d;
});
res.on('end', () => {
console.log("END", body)
pdfdoc.image(body, x, y);
resolve(res);
});
});
req.on('error', err => {
console.error('error!');
reject(err);
});
});
}
我正在使用Node.js 8,并且在本地加载了一个单独的徽标。
答案 0 :(得分:0)
在doug提示之后...这是回答此问题的代码,事实证明Promise.all是关键:
exports.onAppComplete = functions.database.ref("users/{appId}").onCreate((snapshot, context) =>{
var appId = context.params.appId;
var appDb = admin.database().ref(`Applications/${appId}`);
return Promise.all([appDb.once('value'),appId]).then(([applicationDb, appId])=>{
var application = {
appId:appId,
dogId:applicationDb.child("dogId").val(),
userId: applicationDb.child("appWith").val(),
};
const Logo = admin.database().ref(`users/logouri`);
return Promise.all([Logo.once('value')]).then(([Logo])=>{
const PDFDocument = require('pdfkit');
let doc = new PDFDocument({ margin: 72 });
doc.size = [595.28, 841.89];
doc
.image('./images/logo.png', 50, 45, { width: 100 })
.fillColor("#444444")
.moveDown(1);
var logoUri = Logo.val();
console.log("pictureURL",logoUri);
return Promise.all([toBase64(logoUri),doc,application]).then(([logoURIBase64, doc, application])=>{
doc.rect(0,0,590,70,40).fillAndStroke("#339999");
doc.image(logoURIBase64,50,50,{width:100});
const myPdfFile = admin.storage().bucket().file('/test/Arbeitsvertrag.pdf');
const stream = doc.pipe(myPdfFile.createWriteStream());
doc.end();
return console.log('Successfully made App PDF:', "OK");
});
const myPdfFile = admin.storage().bucket().file('/test/Arbeitsvertrag.pdf');
const stream = doc.pipe(myPdfFile.createWriteStream());
doc.end();
return console.log('Successfully made App PDF:', "OK");
})
});
});
function toBase64(url) {
return new Promise(async (resolve, reject)=>{
var https = require('https');
var req = await https.get(url, (res) => {
res.setEncoding('base64');
let body = "data:" + res.headers["content-type"] + ";base64,";
console.log("BODY", body)
res.on('data', (d) => {
body += d;
});
res.on('end', () => {
console.log("END", body)
pdfdoc.image(body, x, y);
resolve(body);
});
});
req.on('error', err => {
console.error('error!');
reject(err);
});
});
}