From bd70c100473b399c07e27f99396a4c94bc0025db Mon Sep 17 00:00:00 2001 From: Rodrigo Martins Date: Tue, 22 Apr 2025 10:09:39 -0300 Subject: [PATCH] tenta fix pdfservice.js --- src/server/services/PdfService.ts | 69 +++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 17 deletions(-) diff --git a/src/server/services/PdfService.ts b/src/server/services/PdfService.ts index cf4e73e..966afe1 100644 --- a/src/server/services/PdfService.ts +++ b/src/server/services/PdfService.ts @@ -314,21 +314,37 @@ export class PdfService { // Função para remover todas as tags HTML, exceto as de formatação suportadas function stripUnsupportedTags(html: string): string { - // Remove todas as tags, exceto , , , , ,
,

, , + // Remove todas as tags, exceto , , , , ,
,

, , ,

,
    ,
      ,
    1. // Remove atributos de exceto text-indent e text-align return html + // Normaliza blockquote para recuo visual + .replace(/]*>/gi, '§§BLOCKQUOTE_START§§') + .replace(/<\/blockquote>/gi, '§§BLOCKQUOTE_END§§') // Mantém apenas com text-indent ou text-align:center .replace(/])*?>/gi, '') // Remove atributos de , , , , ,

      .replace(/<(b|strong|i|em|u|p)[^>]*>/gi, '<$1>') - // Remove todas as outras tags e atributos - .replace(/<(?!\/?(b|strong|i|em|u|br|p|span)[ >])[^>]+>/gi, '') + // Remove todas as outras tags e atributos, exceto listas + .replace(/<(?!\/?(b|strong|i|em|u|br|p|span|ul|ol|li)[ >])[^>]+>/gi, '') // Remove spans sem text-indent ou text-align .replace(/(.*?)<\/span>/gi, '$1') // Remove comentários HTML .replace(//g, ''); } + // Função para converter listas HTML em texto com marcadores/numeração + function htmlListToText(html: string): string { + //

        e
          para marcadores/numeração simples + let idx = 1; + return html + .replace(/]*>([\s\S]*?)<\/ul>/gi, (_, items) => + items.replace(/]*>([\s\S]*?)<\/li>/gi, (__, li) => `\n• ${li.trim()}`) + ) + .replace(/]*>([\s\S]*?)<\/ol>/gi, (_, items) => + items.replace(/]*>([\s\S]*?)<\/li>/gi, () => `\n${idx++}. `) + ); + } + // Função para renderizar texto com formatação inline (negrito, itálico, sublinhado) e controle de quebra de linha e recuo function renderInlineFormattedParagraph( page, @@ -515,18 +531,23 @@ export class PdfService { } const texto = decodeHtmlEntities( - stripUnsupportedTags( - this.processarTemplate(conteudo, escritorio) - .replace(//gi, '\n') - .replace(/<\/p>/gi, '\n\n') - .replace(/]*>/gi, '') - .replace(/]*text-indent[^>]*>/gi, '§§INDENT§§') - .replace(/]*text-align\s*:\s*center[^>]*>/gi, '§§CENTER§§') - .replace(/<\/span>/gi, '') - .replace(/\r\n/g, '\n') - .replace(/\r/g, '\n') - .replace(/\n{3,}/g, '\n\n') - .trim() + htmlListToText( + stripUnsupportedTags( + this.processarTemplate(conteudo, escritorio) + .replace(//gi, '\n') + .replace(/<\/p>/gi, '\n\n') + .replace(/]*>/gi, '') + // Blockquote para recuo visual + .replace(/§§BLOCKQUOTE_START§§/g, '§§INDENT2§§') + .replace(/§§BLOCKQUOTE_END§§/g, '') + .replace(/]*text-indent[^>]*>/gi, '§§INDENT§§') + .replace(/]*text-align\s*:\s*center[^>]*>/gi, '§§CENTER§§') + .replace(/<\/span>/gi, '') + .replace(/\r\n/g, '\n') + .replace(/\r/g, '\n') + .replace(/\n{3,}/g, '\n\n') + .trim() + ) ) ); @@ -562,13 +583,19 @@ export class PdfService { let y = PAGE_HEIGHT - margens.top; while (currentPar < paragrafos.length) { let indent = paragrafos[currentPar].startsWith('§§INDENT§§'); + let indent2 = paragrafos[currentPar].startsWith('§§INDENT2§§'); let center = paragrafos[currentPar].startsWith('§§CENTER§§'); let align: 'center' | 'justify' | 'left' = center ? 'center' : 'justify'; - let text = paragrafos[currentPar].replace(/^§§INDENT§§/, '').replace(/^§§CENTER§§/, '').trim(); + let text = paragrafos[currentPar] + .replace(/^§§INDENT2§§/, '') + .replace(/^§§INDENT§§/, '') + .replace(/^§§CENTER§§/, '') + .trim(); // Se o parágrafo for centralizado, força alinhamento center e ignora recuo if (center) { indent = false; + indent2 = false; align = 'center'; } @@ -576,6 +603,14 @@ export class PdfService { let x = margens.left; let parWidth = MAX_WIDTH; + if (indent2) { + // Recuo maior para blockquote/citação + x += 40; + parWidth -= 40; + } else if (indent) { + x += 20; + parWidth -= 20; + } let yAfter = renderInlineFormattedParagraph( page, @@ -590,7 +625,7 @@ export class PdfService { rgb(0, 0, 0), align, parWidth, - indent, + false, margens.top, margens.bottom, PAGE_HEIGHT