Spaces:
Sleeping
Sleeping
drakosfire
commited on
Commit
•
0a3d2d2
1
Parent(s):
a702223
Trying displying brewRendererContent in a modal, then printing the modal
Browse files- app.py +3 -1
- scripts.js +70 -124
app.py
CHANGED
@@ -88,7 +88,9 @@ def generate_image():
|
|
88 |
# New route to convert HTML to PDF
|
89 |
@app.route('/proxy.html', methods=['POST'])
|
90 |
def proxy():
|
91 |
-
|
|
|
|
|
92 |
|
93 |
# Render the proxy HTML with the provided content
|
94 |
return render_template('proxy.html', html_content=html_content)
|
|
|
88 |
# New route to convert HTML to PDF
|
89 |
@app.route('/proxy.html', methods=['POST'])
|
90 |
def proxy():
|
91 |
+
data = request.json
|
92 |
+
html_content = data.get('htmlContent', '')
|
93 |
+
print(f"Received HTML content: {html_content}")
|
94 |
|
95 |
# Render the proxy HTML with the provided content
|
96 |
return render_template('proxy.html', html_content=html_content)
|
scripts.js
CHANGED
@@ -149,140 +149,86 @@ document.addEventListener("DOMContentLoaded", function() {
|
|
149 |
}
|
150 |
|
151 |
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
// Get the current value of the textarea
|
157 |
-
var storeTitle = titleElement.value;
|
158 |
-
|
159 |
-
// Replace spaces with dashes
|
160 |
-
var processedTitle = storeTitle.replace(/\s+/g, '-');
|
161 |
-
|
162 |
-
htmlContent = `
|
163 |
-
<!DOCTYPE html>
|
164 |
-
<html lang="en">
|
165 |
-
<head>
|
166 |
-
<meta charset="UTF-8">
|
167 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
168 |
-
<link href="{{all_css}}" rel="stylesheet">
|
169 |
-
<link href="{{font_css}}" rel="stylesheet">
|
170 |
-
<link href="{{bundle_css}}" rel="stylesheet">
|
171 |
-
<link href="{{style_css}}" rel="stylesheet">
|
172 |
-
<link href="{{phb_style_css}}" rel="stylesheet">
|
173 |
-
<link href="{{store_ui_css}}" rel="stylesheet">
|
174 |
-
<title>Print Preview - DnD Stat Block</title>
|
175 |
-
|
176 |
-
<style>
|
177 |
-
@media print {
|
178 |
-
|
179 |
-
.page {
|
180 |
-
page-break-before: auto;
|
181 |
-
}
|
182 |
-
.columnWrapper {
|
183 |
-
overflow: visible;
|
184 |
-
}
|
185 |
-
|
186 |
-
}
|
187 |
-
</style>
|
188 |
-
</head>
|
189 |
-
<body>
|
190 |
-
<div id="pageContainer" class="page-container">
|
191 |
-
<div id= "brewRenderer" class="brewRenderer">
|
192 |
-
${pageContainer.innerHTML}
|
193 |
-
</div>
|
194 |
-
</div>
|
195 |
-
</body>
|
196 |
-
</html>
|
197 |
-
`;
|
198 |
-
|
199 |
-
console.log(htmlContent); // Add this line to check the content
|
200 |
-
// var encodedHtmlContent = btoa(encodeURIComponent(htmlContent));
|
201 |
-
|
202 |
-
// Send the HTML content to the server to generate a PDF
|
203 |
-
fetch('/generate-pdf', {
|
204 |
-
method: 'POST',
|
205 |
-
headers: {
|
206 |
-
'Content-Type': 'application/json'
|
207 |
-
},
|
208 |
-
body: JSON.stringify({ htmlContent: htmlContent,
|
209 |
-
title: processedTitle
|
210 |
-
})
|
211 |
-
})
|
212 |
-
|
213 |
-
.then(response => {
|
214 |
-
if (response.ok) {
|
215 |
-
return response.blob(); // Get the response as a Blob (binary large object)
|
216 |
-
} else {
|
217 |
-
return response.json().then(data => {
|
218 |
-
throw new Error(data.error || 'Failed to generate PDF');
|
219 |
-
});
|
220 |
-
}
|
221 |
-
})
|
222 |
-
.then(blob => {
|
223 |
-
// Create a URL for the Blob
|
224 |
-
var downloadUrl = window.URL.createObjectURL(blob);
|
225 |
-
|
226 |
-
// Create an anchor element and click it to trigger the download
|
227 |
-
var a = document.createElement('a');
|
228 |
-
a.href = downloadUrl;
|
229 |
-
a.download = `${processedTitle}.pdf`; // Set the desired file name
|
230 |
-
document.body.appendChild(a);
|
231 |
-
a.click();
|
232 |
-
|
233 |
-
// Clean up by revoking the object URL and removing the anchor element
|
234 |
-
window.URL.revokeObjectURL(downloadUrl);
|
235 |
-
a.remove();
|
236 |
-
})
|
237 |
-
.catch(error => {
|
238 |
-
console.error('Error generating PDF:', error);
|
239 |
-
});
|
240 |
-
};
|
241 |
-
// document.getElementById('printButton').addEventListener('click', function() {
|
242 |
-
// window.printPageContainer();
|
243 |
-
// }
|
244 |
-
// );
|
245 |
-
|
246 |
-
function printPDF() {
|
247 |
-
// Capture the innerHTML of brewRenderer
|
248 |
-
var brewRendererContent = document.getElementById('brewRenderer').innerHTML;
|
249 |
|
250 |
-
|
251 |
-
|
252 |
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
|
271 |
-
|
272 |
-
|
273 |
|
274 |
-
|
275 |
-
|
276 |
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
282 |
|
283 |
-
|
|
|
|
|
284 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
285 |
|
|
|
286 |
|
287 |
|
288 |
// Store initial positions of the blocks
|
|
|
149 |
}
|
150 |
|
151 |
|
152 |
+
// Works great locally, not deployed to huggingface
|
153 |
+
// function printPDF() {
|
154 |
+
// // Capture the innerHTML of brewRenderer
|
155 |
+
// var brewRendererContent = document.getElementById('brewRenderer').innerHTML;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
|
157 |
+
// // Open a new window immediately on user interaction
|
158 |
+
// var previewWindow = window.open('', 'pdf-preview', 'width=800,height=600');
|
159 |
|
160 |
+
// // Check if the window was blocked
|
161 |
+
// if (!previewWindow) {
|
162 |
+
// alert("Popup blocked! Please allow popups for this website to preview the PDF.");
|
163 |
+
// return;
|
164 |
+
// }
|
165 |
|
166 |
+
// // Create a form to send the content to the proxy
|
167 |
+
// var form = document.createElement("form");
|
168 |
+
// form.setAttribute("method", "post");
|
169 |
+
// form.setAttribute("action", "/proxy.html");
|
170 |
+
// form.setAttribute("target", "pdf-preview");
|
171 |
|
172 |
+
// // Create a hidden input to store the HTML content
|
173 |
+
// var hiddenField = document.createElement("input");
|
174 |
+
// hiddenField.setAttribute("type", "hidden");
|
175 |
+
// hiddenField.setAttribute("name", "htmlContent");
|
176 |
+
// hiddenField.setAttribute("value", brewRendererContent);
|
177 |
|
178 |
+
// form.appendChild(hiddenField);
|
179 |
+
// document.body.appendChild(form);
|
180 |
|
181 |
+
// // Submit the form, which will load the proxy.html in the new window
|
182 |
+
// form.submit();
|
183 |
|
184 |
+
// // Clean up the form after a short delay
|
185 |
+
// setTimeout(function() {
|
186 |
+
// form.remove();
|
187 |
+
// }, 1000);
|
188 |
+
// }
|
189 |
+
|
190 |
+
// document.getElementById('printButton').addEventListener('click', printPDF);
|
191 |
+
|
192 |
+
function openPrintModal() {
|
193 |
+
var brewRendererContent = document.getElementById('brewRenderer').innerHTML;
|
194 |
+
console.log('brewRendererContent:', brewRendererContent);
|
195 |
+
|
196 |
+
// Fetch proxy.html via AJAX
|
197 |
+
fetch('/proxy.html', {
|
198 |
+
method: 'POST',
|
199 |
+
headers: {
|
200 |
+
'Content-Type': 'application/json',
|
201 |
+
},
|
202 |
+
body: JSON.stringify({ htmlContent: brewRendererContent }),
|
203 |
+
})
|
204 |
+
.then(response => response.text())
|
205 |
+
.then(html => {
|
206 |
+
// Insert the fetched HTML into the modal
|
207 |
+
document.getElementById('modalPreviewContent').innerHTML = html;
|
208 |
+
|
209 |
+
// Display the modal
|
210 |
+
var modal = document.getElementById('printModal');
|
211 |
+
modal.style.display = "block";
|
212 |
+
|
213 |
+
// Attach event listeners to the print and cancel buttons
|
214 |
+
document.getElementById('print-button').onclick = function() {
|
215 |
+
window.print(); // Trigger print dialog
|
216 |
+
};
|
217 |
|
218 |
+
document.getElementById('cancel-button').onclick = function() {
|
219 |
+
modal.style.display = "none"; // Close the modal
|
220 |
+
};
|
221 |
|
222 |
+
document.getElementsByClassName('close')[0].onclick = function() {
|
223 |
+
modal.style.display = "none"; // Close the modal
|
224 |
+
};
|
225 |
+
})
|
226 |
+
.catch(error => {
|
227 |
+
console.error('Error loading the print preview:', error);
|
228 |
+
});
|
229 |
+
}
|
230 |
|
231 |
+
document.getElementById('printButton').addEventListener('click', openPrintModal);
|
232 |
|
233 |
|
234 |
// Store initial positions of the blocks
|