tenta fix pdfservice.js

This commit is contained in:
rodrigo committed 2025-04-22 16:35:46 -03:00
1 parent 0908378ca0
commit 00a9fb9557
2 files changed
+228 -8

No files matched your search

+9 -8
View File
@@ -197,16 +197,17 @@ export class PdfService {
const page = pdfDoc.getPage(i);
page.drawPage(timbradoEmbed, { x: 0, y: 0, width: page.getWidth(), height: page.getHeight() });
}
// Remove última página se estiver vazia (sem conteúdo visível)
// Remove última página se ela for vazia (heurística: largura e altura padrão, nenhum objeto além do fundo)
if (pdfDoc.getPageCount() > 1) {
const lastPage = pdfDoc.getPage(pdfDoc.getPageCount() - 1);
const textContent = lastPage.getTextContent ? await lastPage.getTextContent() : null;
// pdf-lib não tem getTextContent, então alternativa: verifica se altura do conteúdo é mínima
if (lastPage.getHeight && lastPage.getWidth && lastPage.getHeight() > 0 && lastPage.getWidth() > 0) {
// Heurística: verifica se a página tem apenas o timbrado (sem outros objetos)
if (lastPage.node.Contents().size() === 0) {
pdfDoc.removePage(pdfDoc.getPageCount() - 1);
}
// Heurística: se a última página não tem annotations nem xObjects além do fundo, é vazia
const annots = lastPage.node.Annots();
const xObjects = lastPage.node.Resources().XObject();
const hasAnnots = annots && annots.size && annots.size() > 0;
const hasXObjects = xObjects && xObjects.size && xObjects.size() > 0;
// Se não tem anotações nem xObjects, remove
if (!hasAnnots && !hasXObjects) {
pdfDoc.removePage(pdfDoc.getPageCount() - 1);
}
}
const mergedPdf = await pdfDoc.save();