I want to generate a pdf in google cloud functions (node) using pdfkit. For information purposes: I am drafting relevant information out of a dialogflow context - but from testing with hardcoded variables, this is not part of the problem. The generated pdf is uploaded to a google storage bucket.
The problem is the functionality to "sign" the document. This signature is saved as a base64 encoded string ('data:image/png;base64,…'). From the documentation of pdfkit, it should be possible to display this string as an image with doc.image(). Trying the pdfkit live demo with a base64 string, generated with my front-end, everything is working fine. However, trying to generate the pdf in cloud functions, I receive an unhandled rejection as a log entry and the response to dialogflow is not generated (which works fine, when i leave out the signature).
Any thoughts, why the image can't be displayed? Many thanks in advance!
var id = uuid.v4();
const myPdfFile = admin.storage().bucket().file(`pdf/${id}.pdf`);
var doc = new PDFDocument({ size: 'A4' });
testHeader.create(doc);
base64 = "data:image/png;base64,…"
doc.image(base64, {width: 200})
const stream = doc.pipe(myPdfFile.createWriteStream(
{ metadata: { contentType: 'application/pdf' } }
));
doc.end();
stream.on('finish', function () {
console.log("stream finished");
return getSignedUrl.getUrl(agent, myPdfFile)
});
stream.on('error', function () {
return new Error("stream failed")
});
Whereas the function getSignedUrl.getUrl(agent, file) the following code contains:
const options = {
version: 'v2', // defaults to 'v2' if missing.
action: 'read',
expires: Date.now() + 1000 * 60 * 60, // one hour
};
var url = await file.getSignedUrl(options)
let payload = {}
agent.add(new Payload(agent.UNSPECIFIED, payload, { rawPayload: true, sendAsMessage: true }));
And here are the logs: logs