0% found this document useful (0 votes)
36 views2 pages

Const Downloadreport Async

The document outlines an asynchronous function for generating a PDF report from HTML elements. It captures page elements in batches, optimizes their dimensions, and uses the jsPDF library to create and save the PDF. Error handling is included to manage issues during the PDF generation process.

Uploaded by

amit.s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views2 pages

Const Downloadreport Async

The document outlines an asynchronous function for generating a PDF report from HTML elements. It captures page elements in batches, optimizes their dimensions, and uses the jsPDF library to create and save the PDF. Error handling is included to manage issues during the PDF generation process.

Uploaded by

amit.s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

// const downloadReport = async () => {

// if (!reportRef.current) return;

// setIsGeneratingPdf(true); // Start loading

// try {
// // Get all page elements
// const pageElements =
// reportRef.current.querySelectorAll(".page-container");
// if (pageElements.length === 0) {
// console.error("No pages found");
// return;
// }

// // Optimize dimensions and quality for faster processing


// const scaleFactor = 2; // Reduced from 2 for faster rendering
// const pdfWidth = 840;
// const pdfHeight = 1000;

// // Create PDF with correct dimensions


// const pdf = new jsPDF({
// unit: "px",
// format: [pdfWidth, pdfHeight],
// orientation: "portrait",
// compress: true, // Enable compression for smaller file size
// });

// // Convert NodeList to array of HTMLElements


// const pageElementsArray = Array.from(pageElements) as HTMLElement[];

// // Process pages in smaller batches for better performance


// const batchSize = 2; // Process 2 pages at a time

// // Process pages in batches


// for (let i = 0; i < pageElementsArray.length; i += batchSize) {
// const batch = pageElementsArray.slice(i, i + batchSize);

// // Process pages in each batch concurrently


// const batchPromises = batch.map(async (pageElement, idx) => {
// // Force page to be exactly the right size before capture
// pageElement.style.width = `${pdfWidth}px`;
// pageElement.style.height = `${pdfHeight}px`;
// pageElement.style.overflow = "hidden";
// pageElement.style.margin = "0";
// pageElement.style.position = "relative";

// // Capture with optimized settings


// const canvas = await html2canvas(pageElement, {
// scale: scaleFactor,
// useCORS: true,
// allowTaint: true,
// logging: false,
// width: pdfWidth,
// height: pdfHeight,
// imageTimeout: 0,
// backgroundColor: "#ffffff",
// });

// // Use JPEG with moderate quality for smaller size


// const imgData = canvas.toDataURL("image/jpeg", 1.0);

// return { imgData, index: i + idx };


// });

// // Wait for all pages in the batch to be processed


// const results = await Promise.all(batchPromises);

// // Add pages to PDF in correct order


// for (const { imgData, index } of results) {
// if (index > 0) {
// pdf.addPage([pdfWidth, pdfHeight], "portrait");
// }
// pdf.addImage(imgData, "JPEG", 0, 0, pdfWidth, pdfHeight);
// console.log(`Page ${index + 1} captured successfully`);
// }
// }

// // Save the PDF with a more descriptive name including date


// const date = new Date().toISOString().split("T")[0];
// pdf.save(`InSAR_Insights_${date}.pdf`);
// } catch (error) {
// console.error("Error generating PDF:", error);
// } finally {
// setIsGeneratingPdf(false); // Stop loading
// }
// };

You might also like