Fix - Size PDF

Signed-off-by: Rodrigo Martins <rodrigo.sousa.577@gmail.com>
This commit is contained in:
rodrigo committed 2025-04-23 15:58:50 -03:00
1 parent ecf692502f
commit c99852898c
2 files changed
+20 -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();
+18 -3
View File
@@ -200,10 +200,25 @@ 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
if (typeof lastPage.getTextContent === 'function') {
const text = await lastPage.getTextContent();
if (text && Array.isArray(text.items) && text.items.length > 0) {
isBlank = false;
}
}
// Verifica se há objetos gráficos (imagens, paths, etc.)
if (isBlank && typeof lastPage.node?.Resources === 'object') {
const resources = lastPage.node.Resources;
if (resources.XObject && Object.keys(resources.XObject).length > 0) {
isBlank = false;
}
}
// Só remove se realmente não houver nada
if (isBlank) {
pdfDoc.removePage(pageCountBefore - 1);
}
}