-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
166 lines (147 loc) Β· 5.74 KB
/
index.html
File metadata and controls
166 lines (147 loc) Β· 5.74 KB
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
<html>
<head>
<title>Print via iframe</title>
<style type="text/css">
/*iframe {
width: 100%;
height: 80vh;
border: 2px solid grey;
}*/
.visually-hidden {
position: absolute !important;
width: 1px !important;
height: 1px !important;
margin: 0 !important;
padding: 0 !important;
overflow: hidden !important;
clip: rect(0 0 0 0) !important;
-webkit-clip-path: inset(50%) !important;
clip-path: inset(50%) !important;
border: 0 !important;
white-space: nowrap !important;
}
button {
padding: 0.6rem 1rem;
border-radius: 6px;
text-transform: uppercase;
font-weight: bold;
outline: none;
border: 3px solid #999999;
cursor: pointer;
margin-bottom: 1rem;
background: #000000;
color: #ffffff;
}
.centred-container {
left: 50%;
top: 50%;
position: absolute;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<main>
<div class="centred-container">
<button type="button" id="print-button">π PRINT DOCUMENT π</button>
</div>
</main>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.slim.min.js"></script>
<script type="text/javascript" src="scripts/image-loader.js"></script>
<script type="text/javascript">
(function() {
var pdfPrinter = {};
var _this = pdfPrinter;
// MOCK image sources
_this.imageSources = [
'http://placekitten.com/g/2000/3000',
'http://placekitten.com/g/4000/500',
'http://placekitten.com/g/3000/800',
'http://placekitten.com/g/600/200',
'http://placekitten.com/g/2000/2000'
];
// Self invoking kick off method
(function init() {
// Create iframe
_this.iframe = document.createElement('iframe');
_this.iframe.setAttribute('src', 'about:blank');
_this.iframe.setAttribute('id', 'print-iframe');
_this.iframe.setAttribute('class', 'visually-hidden');
document.body.appendChild(_this.iframe);
// Initialise the button
_this.button = document.querySelector('#print-button');
_this.button.addEventListener('click', (e) => {
populateDocument();
});
// Prepare the image preloader
prepareImagePreloading();
})();
// Build the MOCK contents from various documents
// Make sure that the images from the contents are listed for preloading
function populateDocument() {
var htmlContent = `<button class="button-warning pure-button">Warning Button</button>`;
htmlContent += `<article>`;
htmlContent += `<h1>Document with kittens π π π</h1>`;
htmlContent += `<p>This document was generated at ${generateDate()}</p>`;
htmlContent += `</article>`;
htmlContent += `<article>`;
htmlContent += `<h1>Kitten #1</h1>`;
htmlContent += `<img src="${_this.imageSources[0]}" alt="kitten" />`;
htmlContent += `</article>`;
htmlContent += `<article>`;
htmlContent += `<h1>Kitten #2</h1>`;
htmlContent += `<img src="${_this.imageSources[1]}" alt="kitten" />`;
htmlContent += `</article>`;
htmlContent += `<article>`;
htmlContent += `<h1>Kitten #3</h1>`;
htmlContent += `<img src="${_this.imageSources[2]}" alt="kitten" />`;
htmlContent += `</article>`;
htmlContent += `<article>`;
htmlContent += `<h1>Kitten #4</h1>`;
htmlContent += `<img src="${_this.imageSources[3]}" alt="kitten" />`;
htmlContent += `</article>`;
htmlContent += `<article>`;
htmlContent += `<h1>Kitten #5</h1>`;
htmlContent += `<img src="${_this.imageSources[4]}" alt="kitten" />`;
htmlContent += `</article>`;
_this.imageLoader.reset(); // Manually reset the image loader
_this.imageLoader.addToQueue(_this.imageSources);
renderContent(htmlContent);
}
// Render the html content into the iframe using print styling
function renderContent(htmlContent) {
// Prepare the HTML document
var documentSource = '<html><head>';
documentSource += '<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pure/1.0.1/pure-min.css" media="screen" />';
documentSource += '<link rel="stylesheet" href="styles/print.css" />';
documentSource += '<body></body></html>';
// Write the HTML document into the iframe
var iframeDoc = _this.iframe.contentWindow.document;
iframeDoc.open('text/html', 'replace');
iframeDoc.write(documentSource);
iframeDoc.close();
// Use the MOCK content
iframeDoc.body.innerHTML += htmlContent;
// Preload the images to make sure everything prints fine
_this.imageLoader.loadQueue();
}
function prepareImagePreloading() {
if (!_this.imageLoader) {
_this.imageLoader = new ImageLoader();
$(_this.imageLoader).on('progress', (e, progress) => {
console.log('load progress: ', `${progress*100}%`);
});
$(_this.imageLoader).on('completed', () => {
setTimeout(() => {
_this.iframe.contentWindow.print();
}, 1000);
});
}
}
function generateDate() {
return Date.now();
}
})();
</script>
</body>
</html>