PDFKIT直到我调用doc.end()才开始在http响应上开始流式传输

问题描述 投票:0回答:1

我正在使用pdfkit在运行时生成pdf,并将其返回到http响应中进行下载。我可以在浏览器端下载文件,但下载对话框不会立即打开。而是等到doc.end被调用。我猜pdfkit无法有效地推送流。还有其他人面对吗?如果是,请指导。

这是我正在尝试的示例代码

exports.testPdfKit = functions.https.onRequest((request, response) => {
    //create pdf document
    doc.pipe(response);

    response.set('Content-Disposition', `attachment;filename=testpdfstream.pdf`);
    response.writeHead(200, { 'Content-Type': 'application/pdf' })

    const bigText = "some big text"

    for (var i = 0; i < 1000; i++) {
        console.log('inside iteration -',i)
        doc.text(bigText);
        doc.addPage();
    }
    doc.end()
});

我正在Firebase函数上实现此功能,该函数在内部使用expressjs处理http请求。为了最终生成更大的文件,流媒体对我来说是必须的。

firebase express google-cloud-functions streaming pdfkit
1个回答
0
投票

HTTP函数无法流式传输函数的输入或输出。整个请求在一个内存块中传递,响应被收集并在一个内存块中发送回客户端。两者的最大大小均为10MB。没有针对此云功能限制的解决方法(但它确实可以帮助您更好地进行系统扩展)。

如果需要流媒体或Web套接字,则需要使用其他产品,例如应用程序引擎或计算引擎。

© www.soinside.com 2019 - 2024. All rights reserved.