Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 错误:使用pdfkit结束后写入_Node.js_Angular_Http_Jspdf - Fatal编程技术网

Node.js 错误:使用pdfkit结束后写入

Node.js 错误:使用pdfkit结束后写入,node.js,angular,http,jspdf,Node.js,Angular,Http,Jspdf,Im使用pdfkit,可以成功地将pdf上传到S3一次。但如果第二次尝试,它会抛出此错误 Error: write after end at writeAfterEnd (_stream_writable.js:220:12) at PDFReference.Writable.write (_stream_writable.js:271:5) at PDFPage.write (/Users/jonathancorrin/Desktop/workspace/rent-ap

Im使用pdfkit,可以成功地将pdf上传到S3一次。但如果第二次尝试,它会抛出此错误

Error: write after end
    at writeAfterEnd (_stream_writable.js:220:12)
    at PDFReference.Writable.write (_stream_writable.js:271:5)
    at PDFPage.write (/Users/jonathancorrin/Desktop/workspace/rent-app/node_modules/pdfkit/js/page.js:95:27)
    at PDFDocument.addContent (/Users/jonathancorrin/Desktop/workspace/rent-app/node_modules/pdfkit/js/document.js:161:17)
    at PDFDocument.save (/Users/jonathancorrin/Desktop/workspace/rent-app/node_modules/pdfkit/js/mixins/vector.js:19:19)
    at PDFDocument._fragment (/Users/jonathancorrin/Desktop/workspace/rent-app/node_modules/pdfkit/js/mixins/text.js:257:12)
    at PDFDocument._line (/Users/jonathancorrin/Desktop/workspace/rent-app/node_modules/pdfkit/js/mixins/text.js:203:12)
    at emitThree (events.js:140:20)
    at LineWrapper.emit (events.js:216:7)
    at /Users/jonathancorrin/Desktop/workspace/rent-app/node_modules/pdfkit/js/line_wrapper.js:141:17
    at LineWrapper.wrap (/Users/jonathancorrin/Desktop/workspace/rent-app/node_modules/pdfkit/js/line_wrapper.js:207:9)
    at PDFDocument._text (/Users/jonathancorrin/Desktop/workspace/rent-app/node_modules/pdfkit/js/mixins/text.js:48:17)
    at PDFDocument.text (/Users/jonathancorrin/Desktop/workspace/rent-app/node_modules/pdfkit/js/mixins/text.js:59:19)
    at /Users/jonathancorrin/Desktop/workspace/rent-app/server/routes/image-upload.js:36:6
    at Layer.handle [as handle_request] (/Users/jonathancorrin/Desktop/workspace/rent-app/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/jonathancorrin/Desktop/workspace/rent-app/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/jonathancorrin/Desktop/workspace/rent-app/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/jonathancorrin/Desktop/workspace/rent-app/node_modules/express/lib/router/layer.js:95:5)
    at /Users/jonathancorrin/Desktop/workspace/rent-app/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/Users/jonathancorrin/Desktop/workspace/rent-app/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/jonathancorrin/Desktop/workspace/rent-app/node_modules/express/lib/router/index.js:275:10)
    at Function.handle (/Users/jonathancorrin/Desktop/workspace/rent-app/node_modules/express/lib/router/index.js:174:3)
    at router (/Users/jonathancorrin/Desktop/workspace/rent-app/node_modules/express/lib/router/index.js:47:12)
    at Layer.handle [as handle_request] (/Users/jonathancorrin/Desktop/workspace/rent-app/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/jonathancorrin/Desktop/workspace/rent-app/node_modules/express/lib/router/index.js:317:13)
    at /Users/jonathancorrin/Desktop/workspace/rent-app/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/jonathancorrin/Desktop/workspace/rent-app/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/jonathancorrin/Desktop/workspace/rent-app/node_modules/express/lib/router/index.js:275:10)
我不确定是什么导致了这个错误。这是我的服务器端代码

var express = require('express');
var router = express.Router();
var aws = require('aws-sdk');
var fs = require('fs');
var path = require('path');
var path3 = path.join(__dirname, "../config/config-aws.json");
var pdf = require('pdfkit');
var multer = require('multer');
var multerS3 = require('multer-s3');
var doc = new pdf;

aws.config.loadFromPath(path3);
var s3 = new aws.S3();
var fileName = '';
var uploadM = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'XXXX',
    acl: 'public-read',
    metadata: function (req, file, cb) {
      cb(null, {fieldName: file.fieldname});
    },
    key: function (req, file, cb) {
      fileName = Date.now().toString() + "-" + (Math.round(Math.random() * 10000000000000000)).toString() + '-' + file.originalname;
      cb(null, fileName)
    }
  })
});





router.post('/upload/contract', function (req,res) {
  doc.fontSize(21)
    .text(req.body.contractType, 240, 50);
  doc.fontSize(15)
    .text(req.body.text, 100, 100)
    .text(req.body.text2, 100, 280);
  doc.end();

  var params = {
    Key: req.body.fileName,
    Body: doc,
    Bucket: 'XXXX',
    ACL: 'public-read',
    contentType: 'application/pdf'
  };
  s3.upload(params, function (err, response) {
    if (err) {
      return res.status(400).json({
        message: "Error uploading pdf",
        error: err.stack
      });
    }
    return res.status(200).json({
      title: "PDF uploaded successfully",
      obj: response
    });
  });
});
它与doc.end()有关,但我不确定具体是什么。 这是我发出请求的客户端代码

 onReturnPDF(html: string, fileName: string, contractType: string, html2: string) {
    const body = {
      fileName: fileName,
      text: html,
      contractType: contractType,
      text2: html2
    };
    return this.http.post(this.devUrl + '/upload/contract', body)
      .shareReplay()
  }

有人知道我如何解决这个问题吗?

可能是这样,因为您正在将
doc
创建为全局文件。移动
var doc=新pdf
就在
doc.fontSize(21)
之前,它应该可以工作


问题是-在第一次请求时-pdf被实例化并关闭(使用
doc.end()
)。在第二次请求时,您无法写入pdf,因为它已经关闭(因为它在当前请求的上下文中未安装)

它可能是,因为您正在将
doc
创建为全局文件。移动
var doc=新pdf
就在
doc.fontSize(21)
之前,它应该可以工作


问题是-在第一次请求时-pdf被实例化并关闭(使用
doc.end()
)。在第二次请求时,您无法将其写入pdf,因为它已经关闭(因为它在当前请求的上下文中未安装)

您的文档是全局的,我认为您在调用end后无法对其进行润色。尝试在你的处理程序中添加新的pdf。你的文档是全局的,我不认为你可以在调用end后修改它。尝试在处理程序中使用新的pdf。就是这样。非常感谢!就这样。非常感谢!