qrcode-zip-filesaver,下载zip包

WEB端A4纸排版、打印、二维码生成、zip包下载

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<template>
<div class="print-box">
<div class="print-bd" v-for="x in pages" :key="x" v-show="currentPage ? (currentPage == x) : true">
<div v-for="y in x < pages ? pageSize : lastPageSize" :key="y">
<qrcode :ref="'cvs'+((x-1)*15+y)" value="https://fengyuanchen.github.io/vue-qrcode" :data-name="'张某'+((x-1)*15+y)"></qrcode>
<!-- <qrcode value="https://fengyuanchen.github.io/vue-qrcode" :options="{ color: { dark: '#0275d8' } }"></qrcode> -->
<!-- <qrcode :ref="'cvs'+((x-1)*15+y)" value="https://yitong.com" tag="img" :name="'张某'+((x-1)*15+y)"></qrcode> -->
<div>张某 {{(x-1)*15+y}}</div>
</div>
</div>
<div class="print-btn">
<div>
<div class="handle-all">
<div class="btn" @click="printAll">全部打印</div>
<div class="btn" @click="zipAll">全部下载</div>
</div>
<div class="handle-page">
第<input type="number" maxlength="1000" minlength="1" v-model="currentPage" />页 <div class="btn" @click="print">打印</div><div class="btn" @click="zip">下载</div>
</div>
</div>
</div>
</div>
</template>

<script>
/* global*/
import { $http } from '../../assets/js/api';
import JSZip from 'jszip'
import { saveAs } from 'file-saver';

export default {
name: 'retrieve',
data() {
return {
total: 46,
pageSize: 15,
pages: 0,
lastPageSize: 0,
currentPage: 0
};
},
created() {

},
mounted () {
console.log($http)
console.log(JSZip)
this.lastPageSize = (this.total % this.pageSize) || this.pageSize
this.pages = parseInt(this.total / this.pageSize) + (this.lastPageSize ? 1 : 0)
},
methods: {
// 单页打包
zip() {
if(!this.currentPage) {
return
}
let pageSize = this.pageSize;
let first = (this.currentPage - 1)*this.pageSize;
if(this.pages === this.currentPage) {
pageSize = this.lastPageSize
}
var zip = new JSZip(); // 不支持win10 ie11 ,详见github
zip.file("Hello.txt", "Hello World\n");
var img = zip.folder("images");
for(let x in this.$refs) {
if(x.indexOf('cvs') > -1) {
const refIndex = parseInt(x.replace('cvs', ''))
if(refIndex > first && refIndex <= (first+pageSize)) {
const $el = this.$refs[x][0].$el
const { name } = $el.dataset
img.file(name + '.jpg', $el.toDataURL().split(',')[1], {base64: true});
}
}
}
// img.file("smile.gif", imgData, {base64: true});
zip.generateAsync({type:"blob"})
.then(function(content) {
// see FileSaver.js
saveAs(content, "学生二维码.zip");
});
},
// 全部打包
zipAll() {
console.log(this.$refs)
console.log(this.$refs['cvs1'][0])
console.log(this.$refs['cvs1'][0].$el)
var zip = new JSZip(); // 不支持win10 ie11 ,详见github
zip.file("Hello.txt", "Hello World\n");
var img = zip.folder("images");
for(let x in this.$refs) {
if(x.indexOf('cvs') > -1) {
const $el = this.$refs[x][0].$el
const { name } = $el.dataset
img.file(name + '.jpg', $el.toDataURL().split(',')[1], {base64: true});
}
}
// img.file("smile.gif", imgData, {base64: true});
zip.generateAsync({type:"blob"})
.then(function(content) {
// see FileSaver.js
saveAs(content, "学生二维码.zip");
});
},
// 全部打印
printAll() {
this.currentPage = 0
setTimeout(() => {
window.print()
}, 201)
},
// 单页打印
print() {
window.print()
}
}
};
</script>

<style scoped lang="scss">
.print-box {
height: auto;
position: relative;
padding-top: 12px;
padding-bottom: 72px;
background: #000;
line-height: 0;
}
.print-bd {
box-sizing: border-box;
padding: 10mm 15mm 20mm 15mm;
width: 210mm;
height: 297mm;
background-color: #fff;
margin: 0 auto;
> div {
width: 60mm;
text-align: center;
line-height: 1;
display: block;
float: left;
> img {
width: 80%;
}
> div {
line-height: 1;
font-size: 14px;
color: #666;
margin: 1mm 0 4mm;
}
}
}
.print-btn {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
height: 60px;
background-color: #f8f8f8;
> div {
height: 100%;
width: 210mm;
margin: 0 auto;
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
.handle-all, .handle-page {
display: flex;
flex-direction: row;
align-items: center;
}
.btn {
cursor: pointer;
line-height: 1;
margin: 10px 5px;
border: 1px solid #ccc;
padding: 4px 8px;
transition: all .5s;
&:hover {
border-color: #c4261d;
color: #fff;
background-color: #c4261d;
}
}
}
@media print {
.print-btn {
display: none;
}
.print-box {
padding: 0;
}
}
</style>

github库

https://github.com/eligrey/FileSaver.js

https://github.com/Stuk/jszip

https://github.com/fengyuanchen/vue-qrcode.git

https://github.com/davidshimjs/qrcodejs

参考资料

https://www.jianshu.com/p/85088be3c860

https://www.cnblogs.com/sxww-zyt/p/12674568.html


qrcode-zip-filesaver,下载zip包
http://example.com/20200820-zip-filesaver/
作者
csorz
发布于
2020年8月20日
许可协议