From fa330e7fb3d091ab8b6e61c3a205ad004bdf58b9 Mon Sep 17 00:00:00 2001 From: sauravniraula Date: Tue, 22 Jul 2025 03:00:38 +0545 Subject: [PATCH 1/2] fix(nextjs): adds wait untill all dom elements have loaded --- servers/nextjs/app/api/export-as-pdf/route.ts | 46 ++++++++++++++++--- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/servers/nextjs/app/api/export-as-pdf/route.ts b/servers/nextjs/app/api/export-as-pdf/route.ts index 21a622f2..49ff41df 100644 --- a/servers/nextjs/app/api/export-as-pdf/route.ts +++ b/servers/nextjs/app/api/export-as-pdf/route.ts @@ -16,23 +16,57 @@ export async function POST(req: NextRequest) { headless: true, args: [ '--no-sandbox', - '--disable-setuid-sandbox', - '--disable-dev-shm-usage', - '--disable-gpu', '--disable-web-security', ] }); const page = await browser.newPage(); + await page.setViewport({ width: 1280, height: 720 }); + await page.goto(`http://localhost/pdf-maker?id=${id}`, { waitUntil: 'networkidle0', timeout: 80000 }); + await page.waitForFunction('() => document.readyState === "complete"') + + try { + await page.waitForFunction( + ` + () => { + const allElements = document.querySelectorAll('*'); + let loadedElements = 0; + let totalElements = allElements.length; + + for (let el of allElements) { + const style = window.getComputedStyle(el); + const isVisible = style.display !== 'none' && + style.visibility !== 'hidden' && + style.opacity !== '0'; + + if (isVisible && el.offsetWidth > 0 && el.offsetHeight > 0) { + loadedElements++; + } + } + + return (loadedElements / totalElements) >= 0.95; + } + `, + { timeout: 10000 } + ); + + await new Promise(resolve => setTimeout(resolve, 1000)); + + } catch (error) { + console.log("Warning: Some content may not have loaded completely:", error); + } + + const pdfBuffer = await page.pdf({ - printBackground: true, width: "1280px", height: "720px", - margin: { top: 0, right: 0, bottom: 0, left: 0 } - + printBackground: true, + margin: { top: 0, right: 0, bottom: 0, left: 0 }, }); + browser.close(); + const sanitizedTitle = sanitizeFilename(title); const destinationPath = path.join(process.env.APP_DATA_DIRECTORY!, 'exports', `${sanitizedTitle}.pdf`); console.log('destinationPath', destinationPath); From fd71bdd12599ae41f5ad034d1f3eaccd74449313 Mon Sep 17 00:00:00 2001 From: sauravniraula Date: Tue, 22 Jul 2025 16:44:19 +0545 Subject: [PATCH 2/2] perf(fastapi): removes multiple database write in single endpoint --- servers/fastapi/api/v1/ppt/endpoints/presentation.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/servers/fastapi/api/v1/ppt/endpoints/presentation.py b/servers/fastapi/api/v1/ppt/endpoints/presentation.py index 758a8f40..36ba523c 100644 --- a/servers/fastapi/api/v1/ppt/endpoints/presentation.py +++ b/servers/fastapi/api/v1/ppt/endpoints/presentation.py @@ -134,11 +134,6 @@ async def prepare_presentation( with get_sql_session() as sql_session: presentation = sql_session.get(PresentationModel, presentation_id) - presentation.outlines = [each.model_dump() for each in outlines] - presentation.title = title or presentation.title - presentation.layout = layout.model_dump() - sql_session.commit() - sql_session.refresh(presentation) total_slide_layouts = len(layout.slides) total_outlines = len(outlines) @@ -164,6 +159,9 @@ async def prepare_presentation( with get_sql_session() as sql_session: sql_session.add(presentation) + presentation.outlines = [each.model_dump() for each in outlines] + presentation.title = title or presentation.title + presentation.set_layout(layout) presentation.set_structure(presentation_structure) sql_session.commit() sql_session.refresh(presentation)