在Firebase Cloud Functions中创建PDF [英] Create PDF in Firebase Cloud Functions

查看:101
本文介绍了在Firebase Cloud Functions中创建PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆