From d530356101acf34f9634f3510930ae9304c6f176 Mon Sep 17 00:00:00 2001
From: Lucas Rayan
Date: Sun, 23 Aug 2026 12:52:16 -0300
Subject: [PATCH] refactor: implement server-side CMYK PDF caching and improve
frontend export UI with dynamic DOM injection
---
app/api/export-cmyk/route.js | 33 ++++--
public/formacoes/1/.next/trace | 1 -
public/formacoes/1/index.html | 204 +++++++++++++++------------------
3 files changed, 120 insertions(+), 118 deletions(-)
delete mode 100644 public/formacoes/1/.next/trace
diff --git a/app/api/export-cmyk/route.js b/app/api/export-cmyk/route.js
index 6666926..a889bef 100644
--- a/app/api/export-cmyk/route.js
+++ b/app/api/export-cmyk/route.js
@@ -23,10 +23,28 @@ export async function GET(request) {
}
const tmpDir = os.tmpdir();
- const tmpRgbPdf = path.join(tmpDir, `espdocs-mod-${modulo}-${Date.now()}-rgb.pdf`);
- const tmpCmykPdf = path.join(tmpDir, `espdocs-mod-${modulo}-${Date.now()}-cmyk.pdf`);
+ const cachedCmykPdf = path.join(tmpDir, `espdocs-cached-cmyk-mod-${modulo}.pdf`);
+ const htmlMtime = fs.statSync(htmlFile).mtimeMs;
+ // Se já foi gerado no /tmp e o HTML não mudou, entrega instantaneamente
+ if (fs.existsSync(cachedCmykPdf)) {
+ const cachedMtime = fs.statSync(cachedCmykPdf).mtimeMs;
+ if (cachedMtime >= htmlMtime) {
+ const cachedBuffer = fs.readFileSync(cachedCmykPdf);
+ return new NextResponse(cachedBuffer, {
+ status: 200,
+ headers: {
+ "Content-Type": "application/pdf",
+ "Content-Disposition": `attachment; filename="LASER-ESP32-Modulo-${modulo}-CMYK.pdf"`,
+ "Content-Length": cachedBuffer.length.toString()
+ }
+ });
+ }
+ }
+
+ const tmpRgbPdf = path.join(tmpDir, `espdocs-tmp-${modulo}-${Date.now()}-rgb.pdf`);
let browser = null;
+
try {
browser = await puppeteer.launch({
headless: "new",
@@ -40,15 +58,15 @@ export async function GET(request) {
const page = await browser.newPage();
await page.setViewport({ width: 1200, height: 1600, deviceScaleFactor: 2 });
- await page.goto(`file://${htmlFile}`, { waitUntil: "networkidle0", timeout: 60000 });
+ await page.goto(`file://${htmlFile}`, { waitUntil: "load", timeout: 30000 });
// Aguarda o Paged.js concluir a montagem
await page.waitForFunction(() => {
const pages = document.querySelectorAll(".pagedjs_page");
return pages.length > 0 && !document.querySelector(".aviso-carregando");
- }, { timeout: 30000 });
+ }, { timeout: 25000 });
- await new Promise((r) => setTimeout(r, 1000));
+ await new Promise((r) => setTimeout(r, 600));
await page.pdf({
path: tmpRgbPdf,
@@ -62,10 +80,10 @@ export async function GET(request) {
browser = null;
// Converte para CMYK (DeviceCMYK / ISO Coated)
- const gsCmd = `gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sColorConversionStrategy=CMYK -dProcessColorModel=/DeviceCMYK -dCompatibilityLevel=1.4 -sOutputFile="${tmpCmykPdf}" "${tmpRgbPdf}"`;
+ const gsCmd = `gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sColorConversionStrategy=CMYK -dProcessColorModel=/DeviceCMYK -dCompatibilityLevel=1.4 -sOutputFile="${cachedCmykPdf}" "${tmpRgbPdf}"`;
execSync(gsCmd, { stdio: "pipe" });
- const pdfBuffer = fs.readFileSync(tmpCmykPdf);
+ const pdfBuffer = fs.readFileSync(cachedCmykPdf);
return new NextResponse(pdfBuffer, {
status: 200,
@@ -84,7 +102,6 @@ export async function GET(request) {
} finally {
if (browser) await browser.close();
if (fs.existsSync(tmpRgbPdf)) fs.unlinkSync(tmpRgbPdf);
- if (fs.existsSync(tmpCmykPdf)) fs.unlinkSync(tmpCmykPdf);
}
}
diff --git a/public/formacoes/1/.next/trace b/public/formacoes/1/.next/trace
deleted file mode 100644
index fbb92cd..0000000
--- a/public/formacoes/1/.next/trace
+++ /dev/null
@@ -1 +0,0 @@
-[{"name":"generate-buildid","duration":224,"timestamp":13103889101,"id":4,"parentId":1,"tags":{},"startTime":1787229898989,"traceId":"aad4d6d36b5e5e6b"},{"name":"load-custom-routes","duration":309,"timestamp":13103889458,"id":5,"parentId":1,"tags":{},"startTime":1787229898989,"traceId":"aad4d6d36b5e5e6b"},{"name":"next-build","duration":129948,"timestamp":13103762107,"id":1,"tags":{"buildMode":"default","isTurboBuild":"false","version":"15.5.9"},"startTime":1787229898862,"traceId":"aad4d6d36b5e5e6b"}]
diff --git a/public/formacoes/1/index.html b/public/formacoes/1/index.html
index f24b14d..64f1a9c 100644
--- a/public/formacoes/1/index.html
+++ b/public/formacoes/1/index.html
@@ -1961,126 +1961,112 @@ Temperatura: 26.1 C | Umidade: 62.0 %
-
-
-
-
- Apostila:
-
-
-
-
-
-
-
+