`).join('');
pdfExportContent.innerHTML = `
Back Pain Relief Stretches
A printable guide to your stretching routine.
${stretchHtml}
`;
await new Promise(resolve => setTimeout(resolve, 100));
try {
const canvas = await html2canvas(pdfExportContent, { scale: 2 });
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({ orientation: "portrait", unit: "pt", format: "a4" });
const pageWidth = pdf.internal.pageSize.getWidth();
const pageHeight = pdf.internal.pageSize.getHeight();
const margin = 40;
const imgProps = pdf.getImageProperties(imgData);
const pdfImageWidth = pageWidth - (margin * 2);
const pdfImageHeight = (imgProps.height * pdfImageWidth) / imgProps.width;
let heightLeft = pdfImageHeight;
let position = margin;
pdf.addImage(imgData, 'PNG', margin, position, pdfImageWidth, pdfImageHeight);
heightLeft -= (pageHeight - 2 * margin);
while (heightLeft > 0) {
position = heightLeft - pdfImageHeight + margin; // Adjust position for subsequent pages
pdf.addPage();
pdf.addImage(imgData, 'PNG', margin, position, pdfImageWidth, pdfImageHeight);
heightLeft -= pageHeight;
}
pdf.save(`Back-Pain-Stretches-Guide.pdf`);
} catch (error) {
console.error("Error generating PDF:", error);
alert("Sorry, there was an error creating the PDF.");
} finally {
pdfLoader.classList.add('hidden');
pdfDownloadBtn.disabled = false;
pdfExportContent.innerHTML = '';
}
}
// --- START THE APP ---
initialize();
});