我正在节点后端生成一个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↵% ↵50obj↵<<↵/ Type /Page↵/ Parent 10R↵/ MediaBox [0 0 612 792]↵/ Contents 3 0 R↵/资源40R↵>>↵endobj↵40 obj↵<<↵/ ProcSet [/ PDF / Text / ImageB / ImageC / ImageI]↵/字体<<↵/ F1 6 0 R↵>>>>↵endobj↵70obj↵<<↵/ Producer(PDFKit)↵/ Creator (PDFKit)↵/ CreationDate(D:20160903110846Z)↵>>↵endobj↵60obj↵<<↵/ Type /Font↵/ BaseFont /Helvetica↵/ Subtype /Type1↵/ Encoding /WinAnsiEncoding↵>>↵endobj↵20obj↵<<↵/ Type /Catalog↵/ Pages 1 0 R↵>>↵endobj↵10obj↵<<↵/ Type /Pages↵/Count1↵/ Kids [5 0 R]↵>>↵endobj↵3 0obj↵<<↵/长度106↵/过滤器 /FlateDecode↵>>↵stream↵xe1↵@yEsfvw1LL.:13+IAS]4RdzIjcy@a9 〜8〜的Zm%WKʖ=({ݶ{4 0z < LS .J ↵endstream↵endobj↵xref↵08↵000000000065535f ↵000000044600000n↵000000039700000n↵000000050300000 n ↵000000011900000 n00000000000015 00000n↵000000030000000n ↵000000020800000n↵trailer↵<<↵/Size8↵/ Root 20R↵/ Info 7 0 R↵>>↵startxref↵681↵%%EOF↵“
标题:标题 _headersMap:地图大小:(...) proto :地图[1] 0:{“content-type”=>数组[1]}键:“content-type”值:数组[1] 0:“application / pdf”长度:1 proto :数组[0] proto :对象ok:true状态:200 statusText:“OK”键入:2 url:“http://localhost:3000/api/submissions/generatecontract”
我看到我的PDF是在回复的正文中。但是我现在如何在浏览器中查看或下载呢?如果重要的话,我在前端使用Angular2。
我发现的信息是以前的角度2版本。
答案 0 :(得分:0)
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);
}