Spaces:
Running
Running
Update static/infographic_gen.html
Browse files- static/infographic_gen.html +30 -10
static/infographic_gen.html
CHANGED
@@ -151,19 +151,39 @@
|
|
151 |
|
152 |
// Download infographic as an image
|
153 |
$('#download-btn').click(function() {
|
154 |
-
|
155 |
-
|
156 |
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
});
|
166 |
});
|
|
|
167 |
</script>
|
168 |
</body>
|
169 |
</html>
|
|
|
151 |
|
152 |
// Download infographic as an image
|
153 |
$('#download-btn').click(function() {
|
154 |
+
const iframe = document.getElementById('output-frame');
|
155 |
+
const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
|
156 |
|
157 |
+
// Clone the iframe content into a div for rendering
|
158 |
+
const clonedContent = iframeDocument.body.cloneNode(true);
|
159 |
+
|
160 |
+
// Add external styles to the cloned content
|
161 |
+
const link = document.createElement('link');
|
162 |
+
link.rel = 'stylesheet';
|
163 |
+
link.href = 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css';
|
164 |
+
clonedContent.appendChild(link);
|
165 |
+
|
166 |
+
// Wrap in a temporary container for rendering
|
167 |
+
const tempDiv = document.createElement('div');
|
168 |
+
tempDiv.appendChild(clonedContent);
|
169 |
+
document.body.appendChild(tempDiv);
|
170 |
+
|
171 |
+
// Use html2canvas to render the temporary container
|
172 |
+
html2canvas(tempDiv, {
|
173 |
+
useCORS: true, // Allow cross-origin styles
|
174 |
+
scale: 2, // Improve image quality
|
175 |
+
}).then(function(canvas) {
|
176 |
+
document.body.removeChild(tempDiv); // Clean up
|
177 |
+
const link = document.createElement('a');
|
178 |
+
link.download = 'infographic.png';
|
179 |
+
link.href = canvas.toDataURL('image/png');
|
180 |
+
link.click();
|
181 |
+
}).catch(function(error) {
|
182 |
+
document.body.removeChild(tempDiv); // Clean up
|
183 |
+
alert('An error occurred while capturing the infographic: ' + error);
|
184 |
});
|
185 |
});
|
186 |
+
});
|
187 |
</script>
|
188 |
</body>
|
189 |
</html>
|