refac: note pdf export

This commit is contained in:
Timothy Jaeryang Baek 2025-05-04 16:59:41 +04:00
parent 175bbe27e9
commit 18149483f2

View File

@ -97,8 +97,20 @@
const virtualWidth = 1024; // Fixed width (adjust as needed) const virtualWidth = 1024; // Fixed width (adjust as needed)
const virtualHeight = 1400; // Fixed height (adjust as needed) const virtualHeight = 1400; // Fixed height (adjust as needed)
// STEP 1. Get a DOM node to render
const html = note.data?.content?.html ?? '';
let node;
if (html instanceof HTMLElement) {
node = html;
} else {
// If it's HTML string, render to a temporary hidden element
node = document.createElement('div');
node.innerHTML = html;
document.body.appendChild(node);
}
// Render to canvas with predefined width // Render to canvas with predefined width
const canvas = await html2canvas(note.data.content.html, { const canvas = await html2canvas(node, {
useCORS: true, useCORS: true,
scale: 2, // Keep at 1x to avoid unexpected enlargements scale: 2, // Keep at 1x to avoid unexpected enlargements
width: virtualWidth, // Set fixed virtual screen width width: virtualWidth, // Set fixed virtual screen width
@ -106,6 +118,11 @@
windowHeight: virtualHeight windowHeight: virtualHeight
}); });
// Remove hidden node if needed
if (!(html instanceof HTMLElement)) {
document.body.removeChild(node);
}
const imgData = canvas.toDataURL('image/png'); const imgData = canvas.toDataURL('image/png');
// A4 page settings // A4 page settings
@ -133,6 +150,8 @@
pdf.save(`${note.title}.pdf`); pdf.save(`${note.title}.pdf`);
} catch (error) { } catch (error) {
console.error('Error generating PDF', error); console.error('Error generating PDF', error);
toast.error(`${error}`);
} }
}; };