Node js, piping pdfkit to a memory stream

后端 未结 5 1657
谎友^
谎友^ 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:48

    My code to return a base64 for pdfkit:

    import * as PDFDocument from 'pdfkit'
    import getStream from 'get-stream'
    
    const pdf = {
      createPdf: async (text: string) => {
        const doc = new PDFDocument()
        doc.fontSize(10).text(text, 50, 50)
        doc.end()
    
        const data = await getStream.buffer(doc)
        let b64 = Buffer.from(data).toString('base64')
        return b64
      }
    }
    
    export default pdf
    

提交回复
热议问题