backend: fix PDFService.ts

This commit is contained in:
Rodrigo B. de Sousa Martins committed 2025-06-11 11:52:30 -03:00
1 parent 8f6b708d51
commit 91c213e6f0
1 file changed
+9 -5
+9 -5
View File
@@ -204,15 +204,19 @@ export class PdfService {
const lastPage = pdfDoc.getPage(pageCountBefore - 1); const lastPage = pdfDoc.getPage(pageCountBefore - 1);
const contentStreams = (lastPage as any).node.Contents(); const contentStreams = (lastPage as any).node.Contents();
// Se não houver conteúdo, é uma página em branco // Se não houver conteúdo, é uma página em branco
if (!contentStreams || contentStreams.length === 0) { if (!contentStreams) {
pdfDoc.removePage(pageCountBefore - 1); pdfDoc.removePage(pageCountBefore - 1);
pageCountBefore = pdfDoc.getPageCount(); pageCountBefore = pdfDoc.getPageCount();
continue; continue;
} }
// Soma o tamanho dos streams de conteúdo let totalLength = 0;
const totalLength = contentStreams if (Array.isArray(contentStreams)) {
.map((stream: any) => stream.contents.length) totalLength = contentStreams
.reduce((acc: number, len: number) => acc + len, 0); .map((stream: any) => stream.contents.length)
.reduce((acc: number, len: number) => acc + len, 0);
} else if (contentStreams.contents && typeof contentStreams.contents.length === 'number') {
totalLength = contentStreams.contents.length;
}
// Se o conteúdo for muito pequeno (< 30 bytes), considera em branco // Se o conteúdo for muito pequeno (< 30 bytes), considera em branco
if (totalLength < 30) { if (totalLength < 30) {
pdfDoc.removePage(pageCountBefore - 1); pdfDoc.removePage(pageCountBefore - 1);