Angular 有棱角的印刷品

Angular 有棱角的印刷品,angular,printing,Angular,Printing,谢谢你停在这里 downloadHandler(id) { // this.isResultsLoading = true; this.invoiceService.downloadInvoice(id).subscribe( (data) => { console.log(data); saveAs(data, this.invoice.item);

谢谢你停在这里

downloadHandler(id) {
        // this.isResultsLoading = true;
        this.invoiceService.downloadInvoice(id).subscribe(
            (data) => {
                console.log(data);
                saveAs(data, this.invoice.item);
                // saveAs(data, this.invoice.item);
            },
            (err) => {
                console.error(err);
                //this.errorHandler(err, 'Error while downloading invoice');
            }
        );
    }
我用这个功能下载,效果很好, 现在我可以下载我的文件,然后打印出来。 我的问题是:我需要在不下载的情况下查看要打印的文件


非常感谢

步骤1:获取内容

this._http.get('http://localhost:4200/')
将响应设置为arrayBuffer或blob作为josn

this._http.get('http://localhost:4200/', { responseType: 'arraybuffer' }).
          or
this._http.get('some api end point', { responseType: 'blob' as 'json' }).
步骤2:创建blob对象

const myBlobPart: BlobPart = <BlobPart>res;
const file = new Blob([myBlobPart], { type: 'your media type' });
// media type can be : text/csv, application/pdf, text/plain etc.
步骤4:打开url

    window.open(fileURL);
因此,完整的解决方案如下:

this._http.get('some api endpoint', { responseType: 'blob' as 'json'}).subscribe(res => {
    const myBlobPart: BlobPart = <BlobPart>res;
    const file = new Blob([myBlobPart], { type: 'your media type' });
    const fileURL = URL.createObjectURL(file);
    console.log(fileURL);
    window.open(fileURL);
    this.url = fileURL;
});
this.\u http.get('someapi端点',{responseType:'blob'为'json'})。订阅(res=>{
常量myBlobPart:BlobPart=res;
const file=new Blob([myBlobPart],{type:'your media type'});
const fileURL=URL.createObjectURL(文件);
log(fileURL);
window.open(fileURL);
this.url=fileURL;
});

我让它工作了,所以我分享了它可能会帮助其他人的方法

服务台

printInvoice(id: string) {
    this.httpClient.get('${BASE_URL}/invoices/${id}', { responseType: 'blob' as 'json' }).subscribe((res) => {
        const invoice: Invoice = <Invoice>res;
        const file = new Blob([ invoice ], { type: 'html' });
        const fileURL = URL.createObjectURL(file);
        console.log(fileURL);
        window.open(fileURL);
    });
}
最后是HTML

<button mat-raised-button color="accent" (click)="printHandler(invoice._id)">Print</button>
打印

Shadab Faiz,谢谢你的回答,你能解释一下吗??我需要一个函数来加载文件,然后打印:),很抱歉,我是angular的新手。我已经用更好的逻辑更新了我的答案,并添加了解释。Shadab Faiz,非常感谢,我正在处理:)会让你知道的。Shadab Faiz,目前无法完成。我还在尝试:),这是我的第一个应用程序MEANSTACK,一点也不容易:你被卡住了吗?
printHandler(id) {
    this.invoiceService.downloadInvoice(id).subscribe((res) => {
        console.log(this.printHandler);
        var fileURL = URL.createObjectURL(res);
        window.open(fileURL);
    });
}
<button mat-raised-button color="accent" (click)="printHandler(invoice._id)">Print</button>