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 在做工具这块 开发效率的 ...
随机推荐
- ASP.NET结合Redis实现分布式缓存
最近一个项目ASP.NET+MySQL 有的网页打开初始化的查询需要10秒甚至更久,用户体验极差,而且并发量变大的时候网站容易崩溃 后来想了两种解决方案都不是太满意 1.数据库里建一张缓存表,后台作业 ...
- spring-framework源码编译及导入
环境说明: 时间:2017-09-26 OS name: "mac os x", version: "10.12.4", arch: "x86_64& ...
- python-django(框架结构)
项目架构: 最外层myshop目录:项目的容器,没有实质性的作用 .idea:项目的配置信息.这个文件夹是自动生成,版本控制信息等,包括历史记录.无用 manage.py:(管理项目)一个实用的命令行 ...
- 基于服务器的AAA配置实验(Cisco PT)
一.实验拓扑 二.网络地址分配 Device Interface IP Address Subnet Mask R1 Fa0/0 192.168.1.1 255.255.255.0 S0/0/0 10 ...
- 监控服务器配置(二)-----Grafana安装配置
1.下载grafana安装包(linux版)到 /opt/minitor/grafana . 下载地址:https://download.csdn.net/download/a15565772151/ ...
- Pyjwt ,python jwt ,jwt
pip install Pyjwt 不错的jwt 文章: https://www.cnblogs.com/wayneiscoming/p/7513487.html Sampleimport jwt i ...
- Linux驱动之触摸屏程序编写
本篇博客分以下几部分讲解 1.介绍电阻式触摸屏的原理 2.介绍触摸屏驱动的框架(输入子系统) 3.介绍程序用到的结构体 4.介绍程序用到的函数 5.编写程序 6.测试程序 1.介绍电阻式触摸屏的原理 ...
- python_appium_模拟器启动app进行登录
#coding=utf-8from appium import webdriverimport timedesired_caps = {} #列表desired_caps['platformName' ...
- VNF网络性能提升解决方案及实践
VNF网络性能提升解决方案及实践 2016年7月 作者: 王智民 贡献者: 创建时间: 2016-7-20 稳定程度: 初稿 修改历史 版本 日期 修订人 说明 1.0 20 ...
- Opecv + Anaconda 读取视频(windows)
前言:之前一直用的是python(x,y),但是发现在使用opencv时容易出现一些pythonw.exe停止工作的问题.后来发现自己的操作系统是64位的,却安装了32位的python(x,y),虽然 ...