code: function generatePDF(req, res, next) {
    req.appformsResultPayload = req.appformsResultPayload || {};
  
    //If there is already a submission result, render this. This is useful for cases where the submission is fetched from another database and rendered elsewhere.
    var existingSubmission = req.appformsResultPayload.data;
  
    var params = {
      _id: req.params.id,
      pdfExportDir: req.pdfExportDir,
      downloadUrl: '' + req.protocol + '://' + req.hostname,
      existingSubmission: existingSubmission,
      environment: req.environment,
      mbaasConf: req.mbaasConf,
      domain: req.user.domain,
      filesAreRemote: req.filesAreRemote,
      fileUriPath: req.fileUriPath,
      location: req.coreLocation,
      pdfTemplateLoc: req.pdfTemplateLoc,
      maxConcurrentPhantomPerWorker: req.maxConcurrentPhantomPerWorker
    };
  
    logger.debug("Middleware generatePDF ", {params: params});
  
    forms.generateSubmissionPdf(_.extend(params, req.connectionOptions), function(err, submissionPdfLocation) {
      if (err) {
        logger.error("Middleware generatePDF", {error: err});
        return next(err);
      }
  
      logger.debug("Middleware generatePDF ", {submissionPdfLocation: submissionPdfLocation});
  
      //Streaming the file as an attachment
      res.download(submissionPdfLocation, '' + req.params.id + ".pdf", function(fileDownloadError) {
  
        //Download Complete, remove the cached file
        fs.unlink(submissionPdfLocation, function() {
          if (fileDownloadError) {
            logger.error("Middleware generatePDF ", {error: fileDownloadError});
            //If the headers have not been sent to the client, can use the error handler
            if (!res.headersSent) {
              return next(fileDownloadError);
            }
          }
        });
      });
    });
  }
docs: Middleware For Generating A PDF Representation Of A Submission
  
  This is the last step in a request. It will be terminated here unless there is an error.
  @param req
  @param res
  @param next