How to write the content generated by pdfkit into a pdf file?

writes a function to pdf, but when the exported pdf is opened, it indicates that the file is corrupted, and there should be something wrong with the written content, but I really don"t know how to solve
first paste code
using two libraries, pdfkit, and blob-stream

.
let doc = new PDFDocument()
      let stream = doc.pipe(blobStream())
      // doc.pipe(fs.createWriteStream("Aim.pdf"))
      console.dir(stream)
      doc.fontSize(25).text("Here is some vector graphics...", 100, 80)

      // some vector graphics
      doc
        .save()
        .moveTo(100, 150)
        .lineTo(100, 250)
        .lineTo(200, 250)
        .fill("-sharpFF3300")

      doc.circle(280, 200, 50).fill("-sharp6600FF")

      // an SVG path
      doc
        .scale(0.6)
        .translate(470, 130)
        .path("M 250,75 L 323,301 131,161 369,161 177,301 z")
        .fill("red", "even-odd")
        .restore()

      // end and display the document in the iframe to the right
      doc.end()

      // html5Bolbarraybuffer
      // stream.toBlob("application/pdf")
      // console.log("blob", stream._blob.length)
      // let reader = new FileReader()
      // reader.readAsArrayBuffer(stream._blob)
      // reader.onload = function (e) {
      //   console.log("onloaded", reader.result)
      // }
      stream.on("finish", function() {
        stream.toBlob("application/pdf")
        // stream.toBlobURL("application/pdf")
        console.log("after", stream, doc)
        doc.pipe(fs.createWriteStream("Aim.pdf"))
      //   // console.log("over", stream.toBlobURL("application/pdf"))
        console.log("over")
      })

the printed content is as follows

I don"t know where to start

Mar.09,2021

generate pdf can try XDOC, simple and practical: http://www.xdocin.com/office.


has been solved. The picture data is converted to arraybuffer and written to the document through pdfkit, and then fs.writeFile () is written locally

.
Menu