tenta fix pdfservice.js

This commit is contained in:
rodrigo committed 2025-04-22 16:37:51 -03:00
1 parent 00a9fb9557
commit 8488bd7b84
1 file changed
+8 -11
+8 -11
View File
@@ -197,17 +197,14 @@ export class PdfService {
const page = pdfDoc.getPage(i); const page = pdfDoc.getPage(i);
page.drawPage(timbradoEmbed, { x: 0, y: 0, width: page.getWidth(), height: page.getHeight() }); page.drawPage(timbradoEmbed, { x: 0, y: 0, width: page.getWidth(), height: page.getHeight() });
} }
// Remove última página se ela for vazia (heurística: largura e altura padrão, nenhum objeto além do fundo) // Após mesclar o timbrado
if (pdfDoc.getPageCount() > 1) { const pageCountBefore = pdfDoc.getPageCount();
const lastPage = pdfDoc.getPage(pdfDoc.getPageCount() - 1); if (pageCountBefore > 1) {
// Heurística: se a última página não tem annotations nem xObjects além do fundo, é vazia // Heurística: remove a última página se ela estiver completamente em branco (sem texto)
const annots = lastPage.node.Annots(); const lastPage = pdfDoc.getPage(pageCountBefore - 1);
const xObjects = lastPage.node.Resources().XObject(); const text = await lastPage.getTextContent?.();
const hasAnnots = annots && annots.size && annots.size() > 0; if (!text || (Array.isArray(text.items) && text.items.length === 0)) {
const hasXObjects = xObjects && xObjects.size && xObjects.size() > 0; pdfDoc.removePage(pageCountBefore - 1);
// Se não tem anotações nem xObjects, remove
if (!hasAnnots && !hasXObjects) {
pdfDoc.removePage(pdfDoc.getPageCount() - 1);
} }
} }
const mergedPdf = await pdfDoc.save(); const mergedPdf = await pdfDoc.save();