diff --git a/src/server/services/PdfService.ts b/src/server/services/PdfService.ts index 885e65d..121fb12 100644 --- a/src/server/services/PdfService.ts +++ b/src/server/services/PdfService.ts @@ -379,33 +379,46 @@ export class PdfService { let textParts = []; // Processa texto com formatação - remover tags HTML e aplicar estilos - let processedText = ''; let currentFont = normalFont; let boldActive = false; let italicActive = false; + // Simplificar texto antes de processar para evitar loops infinitos + // Remove tags não suportadas como , , etc. + text = text + .replace(/|<\/u>/gi, '') // Remover tags de sublinhado + .replace(/]*>|<\/span>/gi, '') // Remover tags span + .replace(/<[^>]*style=[^>]*>/gi, '') // Remover atributos style + .replace(/<(?!\/?(?:b|i|strong|em)(?!\w)[^>]*>)[^>]*>/gi, ''); // Manter apenas b, i, strong, em + // Primeiro, extrair partes do texto com suas formatações let i = 0; while (i < text.length) { - if (text.substring(i, i+3) === '') { + // Limite de segurança para evitar loops infinitos + if (textParts.length > 1000) { + console.error('Limite de processamento de texto excedido'); + break; + } + + if (text.substring(i, i+3) === '' || text.substring(i, i+7) === '') { boldActive = true; currentFont = italicActive ? boldItalicFont : boldFont; - i += 3; + i += text.substring(i, i+3) === '' ? 3 : 7; } - else if (text.substring(i, i+4) === '') { + else if (text.substring(i, i+4) === '' || text.substring(i, i+8) === '') { boldActive = false; currentFont = italicActive ? italicFont : normalFont; - i += 4; + i += text.substring(i, i+4) === '' ? 4 : 8; } - else if (text.substring(i, i+3) === '') { + else if (text.substring(i, i+3) === '' || text.substring(i, i+4) === '') { italicActive = true; currentFont = boldActive ? boldItalicFont : italicFont; - i += 3; + i += text.substring(i, i+3) === '' ? 3 : 4; } - else if (text.substring(i, i+4) === '') { + else if (text.substring(i, i+4) === '' || text.substring(i, i+5) === '') { italicActive = false; currentFont = boldActive ? boldFont : normalFont; - i += 4; + i += text.substring(i, i+4) === '' ? 4 : 5; } else { // Encontrar o próximo início de tag