npm pdfmake文件通过表单数据上传

时间:2018-11-26 16:05:11

标签: node.js form-data pdfmake

很抱歉,这可能是一个愚蠢的问题。但是我不明白,如何使用pdfmakeform-data npm包通过表单数据发送pdf文件? 我的代码如下:

const PdfPrinter = require('pdfmake')
const FormData = require('form-data')
const http = require('http')
const printer = new PdfPrinter()
...
async postFile () {
   return new Promise((resolve, reject) => {
      let docDefinition = { // preparing pdf}
      let pdfDoc = printer.createPdfKitDocument(docDefinition)
      let form = new FormData()
      form.append('file', //WHAT I SHOULD DO HERE?)          
      form.append('fileName', 'MyFileName')
      let request = http.request({
         method: 'post',
         host: 'my.host.com',
         path: '/upload',
         headers: form.getHeaders()
      })
      form.pipe(request)
      let result = ''
      request.on('response', res => {
         res.on('data', chunk => { result += chunk })
         res.on('end', () => {
           resolve(result)
         })
      })
      request.on('error', err => {
         reject(err)
      })
   })
}

我如何附加以形成我的pdfDoc或如何pdfDoc.pipe来形成数据?

0 个答案:

没有答案