【问题标题】:View / download pdf in browser that comes from node.js backend在来自 node.js 后端的浏览器中查看/下载 pdf
【发布时间】:2016-09-04 20:22:59
【问题描述】:

我正在我的节点后端生成一个 pdf,如下所示:

router.post('/api/submissions/generatecontract', auth, function (req, res, next) {
  if (!req.body.stallholderId || !req.body.edition) {
    return res.json({status: 400, message: 'Please enter all fields.' });
  }

  var doc = new PDFDocument();

  doc.fontSize(25).text('Here is some text', 100, 80);
  doc.end();

  return doc.pipe(res);
});

现在,当我调用后端并 console.log 响应时,我得到以下输出:

_body: "%PDF-1.3↵%����↵5 0 obj↵>↵endobj↵4 0 obj↵>↵>>↵endobj↵7 0 obj↵>↵endobj↵6 0 obj↵>↵endobj↵2 0 obj↵>↵endobj↵1 0 obj↵>↵endobj↵3 0 obj↵>↵stream↵x�e�1↵�@�yE�sfvw�1��L�L.:13���+�IAS]�4Rdz��I�jcy�@�a9~8 ��~Zm����%WKʖ=({�����{�4 0z�>↵startxref↵681↵%%EOF↵"

标头:标头 _headersMap:地图大小:(...) proto: Map [1] 0: {"content-type" => Array[1]} key: "content-type" value: Array[1] 0: "application/pdf" length: 1 原型:数组[0] proto: Object ok: true status:200 statusText:"OK" type:2 url:"http://localhost:3000/api/submissions/generatecontract"

我看到我的 PDF 在回复正文中。但是我现在如何在浏览器中查看或下载它呢?如果这很重要,我会在前端使用 Angular2。

我找到的信息是以前的 angular 2 版本。

【问题讨论】:

标签: javascript node.js pdf angular node-pdfkit


【解决方案1】:

我是这样解决的:

  generateContract(stallholder: Stallholder, submission: Submission) {
    let headers = new Headers();
    let options = new RequestOptions({ headers: headers });

    headers.append('authorization', 'Bearer ' + this.userService.getToken());

    this.http.get(this.apiUrl + `api/editions/${submission.edition}`, options)
      .map(res => res.json()).subscribe(data => {
        if (data.status === 200) {
          let total = submission.metersStreet * data.price;
          let params = `email=${stallholder.email}&firstName=${stallholder.firstName}&lastName=${stallholder.lastName}
          &name=${stallholder.name}&metersStreet=${submission.metersStreet}&metersDepth=${submission.metersDepth}
          &edition=${submission.edition}&total=${total}`;
          this.openPdfFile(params);
        }
      });
  }

  openPdfFile(params) {
    this.downloadFile(params, function (blob) {
      let win: any = window.open('_blank');
      let blobb = new Blob([blob], {type: 'application/pdf'});
      let url: any = URL.createObjectURL(blobb);
      win.location.href = url;
    });
  }

  downloadFile(params, success) {
    let xhr = new XMLHttpRequest();
    xhr.open('POST', this.apiUrl + 'api/submissions/generatecontract', true);
    xhr.setRequestHeader('authorization', 'Bearer ' + this.userService.getToken());
    xhr.responseType = 'arraybuffer';
    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    xhr.onreadystatechange = function () {
      if (xhr.readyState === 4) {
        if (success) {
          success(xhr.response);
        }
      }
    };
    xhr.send(params);
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 2019-04-03
    • 1970-01-01
    相关资源
    最近更新 更多