Fix - Size PDF

Signed-off-by: Rodrigo Martins <rodrigo.sousa.577@gmail.com>
This commit is contained in:
rodrigo committed 2025-04-23 16:01:24 -03:00
1 parent ecf692502f
commit fe54b55af0
2 files changed
+24 -4

No files matched your search

+2 -1
View File
@@ -47,7 +47,8 @@ app.use((req, res, next) => {
next();
});
app.use(express.json());
app.use(express.json({ limit: '20mb' }));
app.use(express.urlencoded({ extended: true, limit: '20mb' }));
const clienteRepository = new ClienteRepository();
const processoRepository = new ProcessoRepository();
+22 -3
View File
@@ -200,10 +200,29 @@ export class PdfService {
// Após mesclar o timbrado
const pageCountBefore = pdfDoc.getPageCount();
if (pageCountBefore > 1) {
// Heurística: remove a última página se ela estiver completamente em branco (sem texto)
// Heurística aprimorada: remove a última página apenas se estiver realmente vazia (sem texto, imagens ou gráficos)
const lastPage = pdfDoc.getPage(pageCountBefore - 1);
const text = await lastPage.getTextContent?.();
if (!text || (Array.isArray(text.items) && text.items.length === 0)) {
let isBlank = true;
// Verifica se há texto
const annotations = lastPage.node.Annots;
if (annotations && annotations.length > 0) {
isBlank = false;
}
// Verifica se há objetos gráficos (imagens, paths, etc.)
if (isBlank && typeof lastPage.node?.Resources === 'function') {
const resourcesDict = lastPage.node.Resources();
if (resourcesDict && typeof resourcesDict === 'object') {
// Corrige: usa PDFName para acessar o XObject
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { PDFName } = require('pdf-lib');
const xObject = resourcesDict.get(PDFName.of('XObject'));
if (xObject && typeof xObject === 'object' && Object.keys(xObject).length > 0) {
isBlank = false;
}
}
}
// Só remove se realmente não houver nada
if (isBlank) {
pdfDoc.removePage(pageCountBefore - 1);
}
}