-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint.js
161 lines (137 loc) · 3.13 KB
/
print.js
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
function printDoc() {
const virtualDoc = document.createElement('div');
const serializedTree = serialize();
virtualDoc.classList.add('doc');
const rootHTML = `
<div class='root nodes'>
<div class='title' id='main-title'>${currentData.root.title}</div>
<div class='data meta-data'>
${currentData.root.data}
</div>
</div>
`;
virtualDoc.innerHTML = rootHTML;
const root = virtualDoc.querySelector('.root');
recursiveRender(serializedTree.root, root, serializedTree);
return virtualDoc;
}
function serialize() {
const serial = {};
serial.root = traverse('root');
recursiveTraverse(serial.root, serial);
return serial;
}
function traverse(id) {
const element = document.querySelector(`#${id}`);
const ul = [...element.children]
.filter(x => x.localName === 'ul')[0];
let children = [];
if (ul) {
children = [...ul.children]
.filter(x => x.className === 'nodes')
.map(child => child.id);
}
return children;
}
function recursiveTraverse(root, serial) {
for (let i of root) {
let temp = traverse(i);
serial[i] = temp;
recursiveTraverse(temp, serial);
}
}
function recursiveRender(array, parent, serializedTree) {
for (let i of array) {
const title = currentData[i]['title'];
const data = currentData[i]['data'];
const node = document.createElement('div');
node.classList.add(i);
node.classList.add('nodes');
node.innerHTML = `
<div class='title'>${currentData[i].title}</div>
<div class='data'>
${currentData[i].data}
</div>
`;
parent.appendChild(node);
const children = serializedTree[i];
recursiveRender(children, node, serializedTree);
}
}
// PRINT THE DOC
const printBtn = document.querySelector('.print');
printBtn.addEventListener('click', print);
function print() {
const virtualDoc = printDoc();
const body = document.querySelector('body');
const frame = document.createElement('iframe');
frame.setAttribute('src', 'about:blank');
frame.setAttribute('name', 'doc');
body.appendChild(frame);
const doc = window.frames['doc'];
doc.document.body.innerHTML += `
${demoStyle}
`;
doc.document.body.appendChild(virtualDoc);
// console.log(doc.document.body.innerHTML);
doc.window.focus();
doc.window.print();
body.removeChild(frame);
}
// STYLES FOR THE PRINT
var demoStyle = `
<style>
html{
background-color:#fff;
margin:0px;
}
body{
max-width:21.00cm;
max-height:29.70cm;
margin:5.2cm 4.401cm 5.2cm 4.401cm;
border:0px solid black;
page-break-after:right;
}
@page{
size:auto;
margin:0mm;
}
/*
body::first-line {
font-size: 14px;
font-weight: bold;
text-decoration: none;
text-align: right;
} */
.nodes {
display: block;
text-align: justify;
}
.nodes > .nodes {
padding: 15px
margin-left:0;
padding-left:0;
}
.nodes .title {
font-size: 12px;
font-weight: bold;
}
.nodes .data {
font-size: 10px;
text-align:justify;
}
#main-title, .meta-data{
font-weight:bold;
font-size:14px;
text-align:center;
width:280px;
margin:0 auto;
}
.meta-data>p{
font-size:10px;
font-weight:normal;
text-align:center;
margin-bottom:20px;
}
</style>
`;