angular页面打印局部功能实现方法思考

前端之家收集整理的这篇文章主要介绍了angular页面打印局部功能实现方法思考前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

场景

页面显示的时候是分页现实的,当前页面只有10条数据,但是打印需要打印完整的100条数据。
并且在当前页面包含了表格之外的标题菜单等其他元素。
后天api请求都需要带上token信息

前台页面跳转到打印页面后再跳回

1、通过缓存传递数据,
2、路由跳转到打印页面
3、页面获取缓存数据,
4、调用浏览器打印方法
5、操作完毕页面后退一步

调用打印页面

@H_301_16@this.cach.setBylocal({key: key,value: data}); // 写入缓存 this.router.navigate([url,key]); // 路由跳转,缓存key值传递过去

打印页面

@H_301_16@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文件,返回二进制流。
2、在不需要token信息的情况下,并且支持get请求的,可以在浏览器中直接打开该地址。或者通过a标签来实现下载和页面显示。并实现打印功能
3、需要token验证的情况下,使用blob类型来接收数据,并创建一个本地地址供浏览器访问,后面操作如第二步。

@H_301_16@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页面里。略。

原文链接:https://www.f2er.com/angularjs/144784.html

猜你在找的Angularjs相关文章