Express:在使用pdfkit生成pdf时,所有其他请求都被阻止

时间:2018-03-21 18:31:15

标签: node.js express node-pdfkit

使用以下代码,当请求/pdf时,express将停止服务所有其他页面,直到生成pdf为止。如何使PDF生成同步?感谢。

app.get('/pdf', function (req, res) {
  var publication_id = req.query['id'] // e.g. http://localhost:3000/pdf?id=I1519673917344
  var booklet = req.query['booklet'] // e.g. http://localhost:3000/pdf?id=I15196739173440&booklet=true

  const canvasWidth = 450
  const canvasHeight = 636
  const pageWidth = canvasWidth/1.34
  const pageHeight = canvasHeight/1.34

  var canvases = []
  var _publication

  const tasks = [
    function findPublication(callback) {
      Publication.findOne({ 'id': publication_id }, function (err, publication) {
        if (err) return console.error(err)
          _publication = publication
        console.log('found pub')
        callback(null)
      })
    },
    function makeCanvases(callback) {
      for (var i = 1; i < 9; i++) {
        var canvas = new fabric.StaticCanvas('c') // random name
        canvas.setWidth(canvasWidth)
        canvas.setHeight(canvasHeight)
        if ( _publication && _publication.pages.hasOwnProperty('p' + i) ) { // if not empty
          var pages = _publication.pages
          canvas.loadFromJSON(pages['p' + i]);
        }
        canvases.push(canvas)
      }
      console.log('made canvases')
      callback(null)
    },
    function makePdf(callback) {


      res.writeHead(200, {
        'Content-Type': 'application/pdf',
        'Access-Control-Allow-Origin': '*',
        'Content-Disposition': 'filename=' + publication_id + '.pdf'
      });

      if (booklet != 'true') {

        doc = new PDFDocument({size:[pageWidth, pageHeight]})
        var i = 0

        canvases.forEach(function(canvas) {
          SVGtoPDF(doc, canvas.toSVG(), 0, 0, {fontCallback: fonts })
          if (i != canvases.length - 1) {
            doc.addPage()
          }
          i++
        })

        doc.pipe(res).on('finish', function() {
          console.log('single page pdf was successfully created')
        })

        doc.end()

      } else {

        doc = new PDFDocument({size:[pageWidth*2, pageHeight]})

        // all the -1 to have a normal page number
        SVGtoPDF(doc, canvases[8-1].toSVG(), 0, 0, {fontCallback: fonts });
        SVGtoPDF(doc, canvases[1-1].toSVG(), pageWidth, 0, {fontCallback: fonts });
        doc.addPage()
        SVGtoPDF(doc, canvases[2-1].toSVG(), 0, 0, {fontCallback: fonts });
        SVGtoPDF(doc, canvases[7-1].toSVG(), pageWidth, 0, {fontCallback: fonts });
        doc.addPage()
        SVGtoPDF(doc, canvases[6-1].toSVG(), 0, 0, {fontCallback: fonts });
        SVGtoPDF(doc, canvases[3-1].toSVG(), pageWidth, 0, {fontCallback: fonts });
        doc.addPage()
        SVGtoPDF(doc, canvases[4-1].toSVG(), 0, 0, {fontCallback: fonts });
        SVGtoPDF(doc, canvases[5-1].toSVG(), pageWidth, 0, {fontCallback: fonts });

        doc.pipe(res).on('finish', function() {
          console.log('booklet pdf was successfully created')
        })

        doc.end()

      }
    }
  ]

  async.series(tasks, (err) => {
      if (err) {
          return next(err);
      }
  })

  console.log('serving pdf')

})

0 个答案:

没有答案