【发布时间】:2018-04-24 16:36:57
【问题描述】:
最新代码::: 转换() {
const doc = new jsPDF();
// tslint:disable-next-line:max-line-length
const col = ['DischargeDate', 'Case Number', 'Patient Name', 'Hospital Name', 'Payor', 'Total Doctor Fee', 'To be Collected'];
const rows = [];
/* The following array of object as response from the API req */
const itemNew = this.finalArList;
itemNew.forEach(element => {
// tslint:disable-next-line:max-line-length
const temp = [element.dischargeDate, element.caseNo, element.patientName, element.instName, element.payor, element.totalDrFee, element.drFeeToCollect];
rows.push(temp);
});
doc.autoTable(col, rows);
doc.save('Test.pdf');
}
以上是返回对象数组的 api url,在视图中我正在打印这些数据。现在如何将此列表下载为 pdf 格式。
从 api 调用返回的 json 数据看起来像这样。如何实现此 json 数据的 PDF 下载。
好的,这是我尝试过的代码。
public downloadPdf() {
return this.http
.get('http://161.202.31.51:8185/sgdocsrv/ar/getARList', {
responseType: ResponseContentType.Blob
})
.map(res => {
return {
filename: 'filename.pdf',
data: res.blob()
};
})
.subscribe(res => {
console.log('start download:', res);
const url = window.URL.createObjectURL(res.data);
const a = document.createElement('a');
document.body.appendChild(a);
a.setAttribute('style', 'display: none');
a.href = url;
a.download = res.filename;
a.click();
window.URL.revokeObjectURL(url);
a.remove(); // remove the element
}, error => {
console.log('download error:', JSON.stringify(error));
}, () => {
console.log('Completed file download.')
});
}
但它不起作用。这里的 return this.http 有一个 get 调用,但我的 api 有一个 post 方法。我不确定要尝试的确切逻辑是什么。
【问题讨论】:
-
由于后端只是为您提供 JSON 数据,我认为您能做的最好的事情就是让用户将页面打印为 pdf。想想你看到“查看可打印版本”链接的所有地方,比如机票。
-
它被否决了,因为它的质量不足以提供一个好的答案。改进问题的方法可能包括您已经尝试过的事情、示例代码、您正在使用的文档等。编辑:像这样删除您的评论是不礼貌的,现在我的评论脱离了上下文。 OP 最初问“为什么我的问题被否决了?”
-
好的,我已经添加了我尝试过的代码你现在可以删除否决票
-
我没有投反对票。
-
jsPDF 之类的内容可能适合您的需求。