pdf流文件的展示、下载、打印;html转为pdf
背景:合同(后台返回pdf流文件)展示、下载、打印,基于angular4
场景区分:
1、checkout页面 —— post接口,入参为offering、shippingInfo、invoice等(body),返回生成合同的pdf流文件;
2、orderList、orderDetail页面 —— get接口,入参contentId(生成合同在内容管理保存对应的id,url),返回对应合同的pdf流文件。
需求补充说明:
在一个弹出dialog,合同展示在中间部分,footer部分为 下载、打印等按钮。
实现核心:
1、展示:
1> package.json中
"dependencies": {
"ng2-pdf-viewer": "^3.0.8",
"pdfjs-dist": "^1.9.426"
}
2> html
展示:
<pdf-viewer *ngIf ="contract.stc" [src]="contract.src" [render-text]="true" (after-load-complete)="afterLoadComplete($event)" (error)="loadContractError($event)" ></pdf-viewer>
下载:
<a id="contract_download" class="fl mar1d5r mat6" [href]='sanitize(contract.src)' [download]="contract.name" *ngIf="contract.isLoadSuccess" >
<svg class="hlds-icon fillWhite icon2r">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="./assets/theme/default/icons/omniIcon-sprite/svg/symbols.svg#downLoad"></use>
</svg>
</a>
打印:
<a class="fl mat6" (click)="print()" *ngIf="contract.isLoadSuccess">
<svg class="hlds-icon fillWhite icon2r" >
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="./assets/theme/default/icons/omniIcon-sprite/svg/symbols.svg#print"></use>
</svg>
</a>
<iframe #iframe [src]="contract.url" style='display:none;' *ngIf="contract.isLoadSuccess" ></iframe>
3>ts文件
import { Component, Input, Output, EventEmitter, OnInit, OnChanges, SimpleChanges, ViewChild, ElementRef } from '@angular/core';
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';
import { ConstantService } from '../../../../service/constant.service';
import { MessageService } from "../../../../service/message.service";
import { NormalHttpService } from "../../../../service/normal-http.service";
import { SmartrecCommonService } from '../../../bizcommon/service/smartrec.service';
import { OrderService } from '../../service/order.service';
declare var $: any;
@Component({
selector: 'app-order-contract',
templateUrl: './contract.component.html',
styleUrls: ['./contract.component.css']
})
export class ContractComponent implements OnInit, OnChanges {
/*contractInfo : any = {
isGenerated : true,
orderId: '',
orderInfo : {} //the param that generate contract
}*/
@ViewChild('iframe') iframe: ElementRef;
@Input() contractInfo : any ;
@Output() acceptContractState = new EventEmitter<any>();
contract : any = {
src : '',
url : '',
totalPage : 1,
page : 1,
isLoading : true,
isLoadSuccess: false
}
constructor(
private sanitizer: DomSanitizer,
private messageService: MessageService,
private normalHttpService: NormalHttpService,
private smartrecCommonService: SmartrecCommonService,
private orderService: OrderService
) { }
ngOnInit() {
}
ngOnChanges(change: SimpleChanges) {
if(this.contractInfo && this.contractInfo.isGenerated && change.contractInfo.currentValue.orderInfo){
this.contract.isLoadSuccess = false;
this.generateContract();
}
if(this.contractInfo && !this.contractInfo.isGenerated && this.contractInfo.orderId) {
this.queryContract();
}
}
generateContract() {
if(!this.contract.isLoadSuccess){
this.contract.src = '';
this.contract.isLoading = true;
let header = this.normalHttpService.expandHeader({}, true);
let url = this.normalHttpService.makeRequestUrl(`/order/order/${ConstantService.API_VERSION}/contract`);
let xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.responseType = "blob";
for(let key in header) {
xhr.setRequestHeader(key, header[key]);
}
xhr.send(JSON.stringify(this.contractInfo.orderInfo));
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
let blob = new Blob([xhr.response], {type: 'application/pdf'});
this.contract.src = URL.createObjectURL(blob);
this.contract.name = 'contract.pdf';
this.contract.url = this.sanitizer.bypassSecurityTrustResourceUrl(this.contract.src);
}
}.bind(this);
}
}
queryContract() {
this.contract.src = '';
this.orderService.queryContractId(this.contractInfo.orderId).then((res) => {
if (res.code !== 0) {
this.messageService.errorAlert('Query contractId is failed !');
return;
}
if (res.data.contentId) {
this.contract.src = this.smartrecCommonService.getContentUrlById(res.data.contentId);
}
})
}
afterLoadComplete(pdf: any) {
this.contract.totalPage = pdf.pdfInfo.numPages;
this.contract.isLoading = false;
this.contract.isLoadSuccess = true;
}
loadContractError(event: any) {
this.contract.isLoading = false;
this.contract.isLoadSuccess = false;
this.messageService.errorAlert('Contract failed to load !');
}
agree() {
this.acceptContractState.emit({
isAgree : true,
isLoadSuccess : this.contract.isLoadSuccess
})
}
disAgree() {
this.acceptContractState.emit({
isAgree : false,
isLoadSuccess : this.contract.isLoadSuccess
})
}
print() {
let getMyFrame = this.iframe.nativeElement.contentWindow || this.iframe.nativeElement.contentDocument;
getMyFrame.print();
}
changePage(event) {
const pageHeight = $('.ng2-pdf-viewer-container').height() / this.contract.totalPage;
this.contract.page = Math.ceil($(event.target).scrollTop()/pageHeight);
}
sanitize(url:string){
return this.sanitizer.bypassSecurityTrustUrl(url);
}
}
备注:
1.打印内容在弹框内如何实现:首先,打印调用的是window.print()方法。利用iframe标签,将弹框内容的路径,转移到iframe的src属性上,相当于为打印内容新开辟了一个window,最后回归到调用window.print()方法。
参考:
1.https://github.com/VadimDez/ng2-pdf-viewer //展示打印思路
2.https://stackoverflow.com/questions/38457662/iframe-inside-angular2-component-property-contentwindow-does-not-exist-on-typ/50508477 //pdf局部打印思路
3.http://kriscan.oschina.io/blog/2017/05/26/20170526/ //链接安全问题解决
拓展:
1.如何将html内容转换为pdf:a.html2cavas将页面内容screenshot下来;b.jsPdf将screenshot转换为pdf。
pdf流文件的展示、下载、打印;html转为pdf的更多相关文章
- pdf流文件转图片
需求:将后台返回的pdf流文件转换成图片与页面其他内容一起打印pdf流文件不能直接在前台显示,需要借助pdf.js+viewer.js. 一般情况下,如果要打印pdf流文件,可以直接在新打开的view ...
- 在.net Core 使用PDF模板文件生成PDF文件,代替WEB打印控件!
这几天找WEB打印控件,要么收费的,要么免费的只能在IE里用! 我只想简单的打个标签纸!百度2天,看到一老兄说可以用PDF,然后又开始百度..找到了一篇文章 http://www.jianshu.co ...
- xilinx Vivado的使用详细介绍(2):创建工程、添加文件、综合、实现、管脚约束、产生比特流文件、烧写程序、硬件验证
xilinx Vivado的使用详细介绍(2):创建工程.添加文件.综合.实现.管脚约束.产生比特流文件.烧写程序.硬件验证 Author:zhangxianhe 新建工程 打开Vivado软件,直接 ...
- C#/VB.NET 创建PDF/UA文件
1.什么是PDF/UA文件 PDF/UA,即Universally Accessible PDF,该格式的PDF文件是于2012年8月以ISO标准14289-1发布的.具有普遍可访问的PDF文档标准. ...
- java转pdf(html转为pdf),解决中文乱码,标签不规范等问题
第一步,下载jar包以及建对应的文件夹.注意pd4ml的jar要选择pro版本.然后建一个pd4fonts.properties 里面对应的字体. SimSun = simsun.ttf 前面为变量名 ...
- SAP 打印自动导出PDF本地文件(自己做的例子)
smartforms导出PDF: 常用函数有: convert_otfconvert_otfspooljob_2_pdfconvert_abapspooljob_2_pdfCONVERT_OTF_2_ ...
- 打印信息,通过.jasper工具将集合输出到PDF文件 然后利用打印机打印文件
我们上一次成功的利用iReport工具制作了一张报表,并且预览了报表最后的效果,也生成了格式为“jrpxml”.“jrxml”与“jasper”的文件.这次,我们使用jasper提供的java的api ...
- 如何打印加密的PDF文件?
如何打印加密的PDF文件? Pdf加密了不让打印怎么办?? 下载Foxit PDF Editor以下是下载地址:http://www.orsoon.com/Soft/4865.html 用它打开加密的 ...
- go http 下载视频(TS码流文件)(推荐一个网站学习 go example)
视频 http下载代码 dn.go(注意:代码很ugly,没怎么花时间) 总体感觉特别简单,网上看了下 net/http ,io这2个库的使用, 几分钟就写完了,感觉cpp 在做工具这块 开发效率的 ...
随机推荐
- 【原创】qlogic网卡软中断不均衡问题分析
引子 使用qlogic QL45000网卡测试业务性能,发现cpu软中断分布不均衡,而且很有规律,导致cpu空闲也不是很均衡, 会影响业务稳定性. 设备使用3张网卡Qlogic网卡,配置为4*25G模 ...
- [转]使用screw plus来保护php代码安全
原文链接 https://www.jianshu.com/p/f6425e2f8643 https://github.com/del-xiong/screw-plus http://git.oschi ...
- tamptermonkey 脚本初步尝试
刚开始接触tamptermonkey 主要用于基于浏览器的数据采集 基本都是js基础 这里是采集表格里的内容, 构造json, 然后post发送到后台 如果加入定时刷新功能就可以随时或是按照固定或是随 ...
- Java中的equals和hashCode方法详解
Java中的equals和hashCode方法详解 转自 https://www.cnblogs.com/crazylqy/category/655181.html 参考:http://blog.c ...
- css3+html5特效-向上滑动
css+html5特效-向上滑动 效果描述:切换的下拉和上拉状态 鼠标悬浮:下拉鼠标离开:上拉 /*外容器设置*/ .box1{position:relative;top:100px;left:100 ...
- gridview 选中某行后 某行的按钮显示,无选中则隐藏
如后面的加号,选中行时出现,未被选中的隐藏 解决: 在按钮中的visibility属性中绑定该item是否选中,再用转换器转换 <Button Name="btnA" Cli ...
- 转载:如何优雅的实现INotifyPropertyChanged接口
转载来源:http://www.cnblogs.com/TianFang/p/6240933.html 如何优雅的实现INotifyPropertyChanged接口 INotifyPropertyC ...
- H5真机调试
为什么要做H5真机调试? 第一,样式调试.浏览器的效果和真机上的效果不一定相同,没有真机调试,我们都是先上传到服务器,然后再用手机访问,显然对于样式调试来说,这样效率很低. 第二,调用到底层.真机才拥 ...
- 命令行连WiFi
命令行连WiFi sudo iw dev wlan0 scan |grep SSID wpa_cli -iwlan0 add_network wpa_cli -iwlan0 set_network 4 ...
- BP算法的矩阵推导
目录 1. 需要的微积分知识 1.1 导数 1.2 求导的链式法则 2. 梯度下降法 2.1 梯度 2.2 梯度算法的解释 3.误差反向传播算法 3.1 理论推导 3.1.1 符号说明 3.1.2 推 ...