Node js, piping pdfkit to a memory stream

后端 未结 5 1628
谎友^
谎友^ 2021-02-07 21:07

I am using pdfkit (https://github.com/devongovett/pdfkit) on my node server, typically creating pdf files, and then uploading them to s3. The problem is that pdfkit examples pip

5条回答
  •  面向向阳花
    2021-02-07 21:46

    A tweak of @bolav's answer worked for me trying to work with pdfmake and not pdfkit. First you need to have memorystream added to your project using npm or yarn.

    const MemoryStream = require('memorystream');
    const PdfPrinter = require('pdfmake');
    const pdfPrinter = new PdfPrinter();
    const docDef = {};
    const pdfDoc = pdfPrinter.createPdfKitDocument(docDef);
    const memStream = new MemoryStream(null, {readable: false});
    const pdfDocStream = pdfDoc.pipe(memStream);
    pdfDoc.end();
    pdfDocStream.on('finish', () => {
      console.log(Buffer.concat(memStream.queue);
    });
    

提交回复
热议问题