Blob不能正常流。NextJS+Vercel+PDFMake。
创始人
2024-12-22 19:00:11
0

问题出现在使用PDFMake库生成PDF时。由于生成PDF的过程需要较长的时间,因此将其作为Blob返回。但是,在NextJS + Vercel中,由于应用程序部署在服务器上,客户端无法直接访问生成的Blob URL。因此,客户端会尝试发送HTTP请求以获取文件内容,但由于Blob尚未完全生成,因此请求将等待直到Blob可用或超时。

解决这个问题的办法是,使用Node.js的readable streams功能。它提供了将大型文件分成类似数据块的功能。这样,我们可以将PDFMake生成的Blob pipe到res(响应)对象中。当客户端通过访问响应对象时,它会立即开始增量地接收Blob数据,而不需要等待Blob生成完成。

以下是一个可以解决Blob流问题的示例:

const PdfPrinter = require('pdfmake')
const stream = require('stream')

export default async (req, res) => {
  const fonts = {
    Roboto: {
      normal: 'fonts/Roboto-Regular.ttf',
      bold: 'fonts/Roboto-Medium.ttf',
      italics: 'fonts/Roboto-Italic.ttf',
      bolditalics: 'fonts/Roboto-MediumItalic.ttf',
    },
  }

  const printer = new PdfPrinter(fonts)
  const docDefinition = {
    content: [
      { text: 'Hello World!', fontSize: 22 },
    ],
  }

  const pdfStream = printer.createPdfKitDocument(docDefinition).pipe(new stream.PassThrough())
  res.setHeader('Content-Type', 'application/pdf')
  res.setHeader('Content-Disposition', 'attachment; filename=hello-world.pdf')

  pdfStream.pipe(res)
}

相关内容

热门资讯

aapoker辅助!aapok... 这是一款非常优秀的aapoker辅助 ia辅助检测软件,能够让你了解到aapoker辅助中牌率当中全...
透视辅助!德州私人局怎么透视,... 透视辅助!德州私人局怎么透视,pokerworld修改器(确实有挂)1、德州私人局怎么透视系统规律教...
aapoker发牌机制!aa ... 1、aapoker发牌机制!aa poker下载地址,(aapOKER)竟然是真的有挂,详细教程(有...
透视智能ai!pokerrrr... 透视智能ai!pokerrrr2辅助,epoker透视底牌(一贯存在有挂);1、让任何用户在无需ep...
aapoker辅助工具!aap... 1、aapoker辅助工具!aapoker德州线上扑克辅助工具,(aaPoker)确实是有挂,详细教...
透视脚本!德州透视是真的吗,p... 透视脚本!德州透视是真的吗,pokemmo辅助脚本(总是真的有挂)1、点击下载安装,德州透视是真的吗...
aapoker俱乐部!aapo... aapoker俱乐部!aapoker有手游版吗,(Aapoker)总是是真的有挂,详细教程(有挂辅助...
透视好友!智星菠萝透视,pok... 透视好友!智星菠萝透视,pokemmo修改器手机版(确实真的有挂)pokemmo修改器手机版辅助器中...
aapoker透视辅助!aap... aapoker透视辅助!aapoker辅助工具下载,(AAPOKER)总是有挂,详细教程(有挂详情)...
透视透视!xpoker辅助助手... 透视透视!xpoker辅助助手,pokemmo辅助器手机版下载(原来是有挂);在进入pokemmo辅...