angular页面打印局部功能实现方法思考
发布时间:2020-12-17 08:18:17 所属栏目:安全 来源:网络整理
导读:场景 在页面显示的时候是分页现实的,当前页面只有10条数据,但是打印需要打印完整的100条数据。 并且在当前页面包含了表格之外的标题,菜单等其他元素。 后天api请求都需要带上token信息 前台页面跳转到打印页面后再跳回 1、通过缓存传递数据, 2、路由跳转
场景在页面显示的时候是分页现实的,当前页面只有10条数据,但是打印需要打印完整的100条数据。 前台页面跳转到打印页面后再跳回1、通过缓存传递数据, 调用打印页面 this.cach.setBylocal({key: key,value: data}); // 写入缓存 this.router.navigate([url,key]); // 路由跳转,缓存key值传递过去 打印页面 ngAfterViewInit() { const $qus = this.route.params.subscribe(q => { const key = q.key; if (key) { this._data = this.cach.getBylocal(key) ; this.cach.removeBylocal(key); setTimeout(() => { window.print(); history.go(-1); },'20'); } }); }
在打印页面里自动处理了分页显示等问题。测试一页A4纸适合页面840px-1225px 在后台生成pdf页面显示自带打印下载功能1、后台生成PDF文件,返回二进制流。 private down(url: string,body?: any) { return this.http.post(url,body || null,{ responseType: 'blob' }) } private downLoad(url: string,type: string,body?: any,fileName?: string) { return this.down(url,body).map(r => { const blob = new Blob([r],{type: type}); const objectUrl = URL.createObjectURL(blob); const a = document.createElement('a'); document.body.appendChild(a); a.setAttribute('style','display:none'); a.setAttribute('href',objectUrl); if (fileName) { a.setAttribute('download',fileName); } else { a.setAttribute('target','_blank'); } a.click(); URL.revokeObjectURL(objectUrl); return true; }); } /** * 下载pdf, 如果不传入文件名会再浏览器中打开 实现打印功能 * 传入文件名会直接下载 * @param {string} url * @param body * @param {string} fileName * @returns {Observable<boolean>} */ downLoadPdf(url: string,fileName?: string) { return this.downLoad(url,'application/pdf',body,fileName); }
在iframe页面里调用打印这个方法可以结合前两个方法使用,把前两个页面放在在iframe页面里。略。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |