在 Firebase Cloud Functions 中创建 PDF [英] Create PDF in Firebase Cloud Functions
问题描述
我是 javascript 新手,我正在尝试使用 pdfkit 从 firebase 函数制作 PDF 文件.下面是我的功能代码.
I am new to javascript and I'm trying to make a PDF file from a firebase function using pdfkit. Below is my function code.
const pdfkit = require('pdfkit');
const fs = require('fs');
exports.PDFTest = functions.https.onRequest((req, res) => {
var doc = new pdfkit();
var loremIpsum = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam in...';
doc.y = 320;
doc.fillColor('black')
doc.text(loremIpsum, {
paragraphGap: 10,
indent: 20,
align: 'justify',
columns: 2
});
doc.pipe( res.status(200) )
});
函数启动,但随后发生超时错误.这是在firebase中创建pdf文件的最佳方式吗?我有一些要制作成 pdf 文件的 html.
The function starts but then a timeout error happens. Is this the best way of going about creating a pdf file in firebase? I have some html that I want made into a pdf file.
推荐答案
也正在处理这个问题,为了将 PDF 保存在存储中,它的工作原理是这样的
Just working on this too, for saving the PDF in the storage it works like this
const myPdfFile = admin.storage().bucket().file('/test/Arbeitsvertrag.pdf');
const doc = new pdfkit();
const stream = doc.pipe(myPdfFile.createWriteStream());
doc.fontSize(25).text('Test 4 PDF!', 100, 100);
doc.end();
return res.status(200).send();
猜你应该等到流关闭并监听错误和事情,但这是我能够制作的第一个工作示例,现在正在研究如何将图像从存储中获取到 PDF 中.
Guess you should wait until the stream is closed and listen for Errors and Things, but this is the first working example I was able to make, now working on how to get a Image from the storage into the PDF.
这篇关于在 Firebase Cloud Functions 中创建 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!