Merge pull request #32459 from silentoplayz/fix/long-pdf-export-sliced

fix: long notes and chats download as PDF instead of failing
This commit is contained in:
G30 authored and GitHub committed 2026-10-15 13:11:40 +04:00
1 parent fb5431a09c
commit f78e46ef44
3 files changed
+269 -124

No files matched your search

+97 -34
View File
@@ -62,55 +62,118 @@ export const downloadPdf = async (note) => {
document.body.appendChild(node);
}
// Render to canvas with predefined width
const canvas = await html2canvas(node, {
useCORS: true,
backgroundColor: isDarkMode ? '#000' : '#fff',
scale: 2, // Keep at 1x to avoid unexpected enlargements
width: virtualWidth, // Set fixed virtual screen width
windowWidth: virtualWidth, // Ensure consistent rendering
windowHeight: virtualHeight
});
// Remove hidden node if needed
if (!(html instanceof HTMLElement)) {
document.body.removeChild(node);
}
const imgData = canvas.toDataURL('image/jpeg', 0.7);
await Promise.all(
Array.from(node.querySelectorAll('img')).map((img) => img.decode().catch(() => {}))
);
// A4 page settings
const pdf = new jsPDF('p', 'mm', 'a4');
const imgWidth = 210; // A4 width in mm
const pageWidthMM = 210; // A4 width in mm
const pageHeight = 297; // A4 height in mm
const pageHeightMM = 297; // A4 height in mm
if (isDarkMode) {
pdf.setFillColor(0, 0, 0);
pdf.rect(0, 0, pageWidthMM, pageHeightMM, 'F'); // black bg
}
const pagePixelHeight = Math.floor((virtualWidth * pageHeightMM) / pageWidthMM);
const rootTop = node.getBoundingClientRect().top;
const contentHeight = Math.ceil(node.getBoundingClientRect().height);
// Maintain aspect ratio
const imgHeight = (canvas.height * imgWidth) / canvas.width;
let heightLeft = imgHeight;
let position = 0;
const blocks = [];
const trackedElements = new Set();
const collectBlocks = (element) => {
const children = Array.from(element.children);
const splittable =
children.length > 0 &&
(getComputedStyle(element).display === 'contents' ||
(element.getBoundingClientRect().height > pagePixelHeight / 4 &&
!Array.from(element.childNodes).some(
(childNode) => childNode.nodeType === Node.TEXT_NODE && childNode.textContent?.trim()
) &&
children.every((child) => !getComputedStyle(child).display.startsWith('inline'))));
pdf.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight);
heightLeft -= pageHeight;
if (element !== node) trackedElements.add(element);
// Handle additional pages
while (heightLeft > 0) {
position -= pageHeight;
pdf.addPage();
if (splittable) {
children.forEach(collectBlocks);
} else {
const { top, bottom } = element.getBoundingClientRect();
blocks.push({ element, top: top - rootTop, bottom: bottom - rootTop });
}
};
collectBlocks(node);
node.setAttribute('data-pdf-root', '');
let offsetY = 0;
let page = 0;
while (offsetY < contentHeight) {
const sliceHeight = Math.min(pagePixelHeight, contentHeight - offsetY);
const sliceBlocks = blocks.filter(
(block) => block.bottom > offsetY && block.top < offsetY + sliceHeight
);
const keptElements = new Set();
for (const { element } of sliceBlocks) {
for (let current = element; current && current !== node; current = current.parentElement) {
keptElements.add(current);
}
}
const anchor = sliceBlocks.find((block) => block.bottom > block.top);
anchor?.element.setAttribute('data-pdf-anchor', '');
const canvas = await html2canvas(node, {
useCORS: true,
logging: false,
backgroundColor: isDarkMode ? '#000' : '#fff',
scale: 2, // Keep at 1x to avoid unexpected enlargements
width: virtualWidth, // Set fixed virtual screen width
windowWidth: virtualWidth, // Ensure consistent rendering
windowHeight: virtualHeight,
height: sliceHeight,
y: offsetY,
ignoreElements: (element) =>
(element.parentElement === document.body &&
element !== node &&
!['STYLE', 'LINK'].includes(element.tagName)) ||
(trackedElements.has(element) && !keptElements.has(element)),
onclone: (clonedDocument) => {
const clonedRoot = clonedDocument.querySelector('[data-pdf-root]');
const clonedAnchor = clonedDocument.querySelector('[data-pdf-anchor]');
if (!clonedRoot || !clonedAnchor) return;
const spacer = clonedDocument.createElement('div');
clonedRoot.prepend(spacer);
let spacerHeight = 0;
for (let pass = 0; pass < 2; pass++) {
spacerHeight +=
anchor.top -
(clonedAnchor.getBoundingClientRect().top - clonedRoot.getBoundingClientRect().top);
spacer.style.height = `${spacerHeight}px`;
}
}
});
anchor?.element.removeAttribute('data-pdf-anchor');
const imgData = canvas.toDataURL('image/jpeg', 0.7);
const imgHeightMM = (sliceHeight * pageWidthMM) / virtualWidth;
if (page > 0) pdf.addPage();
if (isDarkMode) {
pdf.setFillColor(0, 0, 0);
pdf.rect(0, 0, pageWidthMM, pageHeightMM, 'F'); // black bg
}
pdf.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight);
heightLeft -= pageHeight;
pdf.addImage(imgData, 'JPEG', 0, 0, pageWidthMM, imgHeightMM);
offsetY += sliceHeight;
page++;
}
// Remove hidden node if needed
if (!(html instanceof HTMLElement)) {
document.body.removeChild(node);
}
pdf.save(`${note.title}.pdf`);