Fix - Size PDF
Signed-off-by: Rodrigo Martins <rodrigo.sousa.577@gmail.com>
This commit is contained in:
1 parent
ecf692502f
commit
c99852898c
2 files changed
+20
-4
No files matched your search
+2
-1
@@ -47,7 +47,8 @@ app.use((req, res, next) => {
|
|||||||
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 clienteRepository = new ClienteRepository();
|
||||||
const processoRepository = new ProcessoRepository();
|
const processoRepository = new ProcessoRepository();
|
||||||
|
|||||||
@@ -200,10 +200,25 @@ export class PdfService {
|
|||||||
// Após mesclar o timbrado
|
// Após mesclar o timbrado
|
||||||
const pageCountBefore = pdfDoc.getPageCount();
|
const pageCountBefore = pdfDoc.getPageCount();
|
||||||
if (pageCountBefore > 1) {
|
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 lastPage = pdfDoc.getPage(pageCountBefore - 1);
|
||||||
const text = await lastPage.getTextContent?.();
|
let isBlank = true;
|
||||||
if (!text || (Array.isArray(text.items) && text.items.length === 0)) {
|
// 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);
|
pdfDoc.removePage(pageCountBefore - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user