tenta fix pdfservice.js

This commit is contained in:
rodrigo committed 2025-04-22 10:09:39 -03:00
1 parent 85977d7229
commit bd70c10047
1 file changed
+52 -17
+52 -17
View File
@@ -314,21 +314,37 @@ export class PdfService {
// Função para remover todas as tags HTML, exceto as de formatação suportadas // Função para remover todas as tags HTML, exceto as de formatação suportadas
function stripUnsupportedTags(html: string): string { function stripUnsupportedTags(html: string): string {
// Remove todas as tags, exceto <b>, <strong>, <i>, <em>, <u>, <br>, <p>, <span style="text-indent:2em;">, <span style="text-align:center;"> // Remove todas as tags, exceto <b>, <strong>, <i>, <em>, <u>, <br>, <p>, <span style="text-indent:2em;">, <span style="text-align:center;">, <blockquote>, <ul>, <ol>, <li>
// Remove atributos de <span> exceto text-indent e text-align // Remove atributos de <span> exceto text-indent e text-align
return html return html
// Normaliza blockquote para recuo visual
.replace(/<blockquote[^>]*>/gi, '§§BLOCKQUOTE_START§§')
.replace(/<\/blockquote>/gi, '§§BLOCKQUOTE_END§§')
// Mantém apenas <span> com text-indent ou text-align:center // Mantém apenas <span> com text-indent ou text-align:center
.replace(/<span\b(?:(?!text-indent|text-align)[^>])*?>/gi, '') .replace(/<span\b(?:(?!text-indent|text-align)[^>])*?>/gi, '')
// Remove atributos de <b>, <strong>, <i>, <em>, <u>, <p> // Remove atributos de <b>, <strong>, <i>, <em>, <u>, <p>
.replace(/<(b|strong|i|em|u|p)[^>]*>/gi, '<$1>') .replace(/<(b|strong|i|em|u|p)[^>]*>/gi, '<$1>')
// Remove todas as outras tags e atributos // Remove todas as outras tags e atributos, exceto listas
.replace(/<(?!\/?(b|strong|i|em|u|br|p|span)[ >])[^>]+>/gi, '') .replace(/<(?!\/?(b|strong|i|em|u|br|p|span|ul|ol|li)[ >])[^>]+>/gi, '')
// Remove spans sem text-indent ou text-align // Remove spans sem text-indent ou text-align
.replace(/<span>(.*?)<\/span>/gi, '$1') .replace(/<span>(.*?)<\/span>/gi, '$1')
// Remove comentários HTML // Remove comentários HTML
.replace(/<!--[\s\S]*?-->/g, ''); .replace(/<!--[\s\S]*?-->/g, '');
} }
// Função para converter listas HTML em texto com marcadores/numeração
function htmlListToText(html: string): string {
// <ul> e <ol> para marcadores/numeração simples
let idx = 1;
return html
.replace(/<ul[^>]*>([\s\S]*?)<\/ul>/gi, (_, items) =>
items.replace(/<li[^>]*>([\s\S]*?)<\/li>/gi, (__, li) => `\n• ${li.trim()}`)
)
.replace(/<ol[^>]*>([\s\S]*?)<\/ol>/gi, (_, items) =>
items.replace(/<li[^>]*>([\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 // Função para renderizar texto com formatação inline (negrito, itálico, sublinhado) e controle de quebra de linha e recuo
function renderInlineFormattedParagraph( function renderInlineFormattedParagraph(
page, page,
@@ -515,18 +531,23 @@ export class PdfService {
} }
const texto = decodeHtmlEntities( const texto = decodeHtmlEntities(
stripUnsupportedTags( htmlListToText(
this.processarTemplate(conteudo, escritorio) stripUnsupportedTags(
.replace(/<br\s*\/?>/gi, '\n') this.processarTemplate(conteudo, escritorio)
.replace(/<\/p>/gi, '\n\n') .replace(/<br\s*\/?>/gi, '\n')
.replace(/<p[^>]*>/gi, '') .replace(/<\/p>/gi, '\n\n')
.replace(/<span[^>]*text-indent[^>]*>/gi, '§§INDENT§§') .replace(/<p[^>]*>/gi, '')
.replace(/<span[^>]*text-align\s*:\s*center[^>]*>/gi, '§§CENTER§§') // Blockquote para recuo visual
.replace(/<\/span>/gi, '') .replace(/§§BLOCKQUOTE_START§§/g, '§§INDENT2§§')
.replace(/\r\n/g, '\n') .replace(/§§BLOCKQUOTE_END§§/g, '')
.replace(/\r/g, '\n') .replace(/<span[^>]*text-indent[^>]*>/gi, '§§INDENT§§')
.replace(/\n{3,}/g, '\n\n') .replace(/<span[^>]*text-align\s*:\s*center[^>]*>/gi, '§§CENTER§§')
.trim() .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; let y = PAGE_HEIGHT - margens.top;
while (currentPar < paragrafos.length) { while (currentPar < paragrafos.length) {
let indent = paragrafos[currentPar].startsWith('§§INDENT§§'); let indent = paragrafos[currentPar].startsWith('§§INDENT§§');
let indent2 = paragrafos[currentPar].startsWith('§§INDENT2§§');
let center = paragrafos[currentPar].startsWith('§§CENTER§§'); let center = paragrafos[currentPar].startsWith('§§CENTER§§');
let align: 'center' | 'justify' | 'left' = center ? 'center' : 'justify'; 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 // Se o parágrafo for centralizado, força alinhamento center e ignora recuo
if (center) { if (center) {
indent = false; indent = false;
indent2 = false;
align = 'center'; align = 'center';
} }
@@ -576,6 +603,14 @@ export class PdfService {
let x = margens.left; let x = margens.left;
let parWidth = MAX_WIDTH; 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( let yAfter = renderInlineFormattedParagraph(
page, page,
@@ -590,7 +625,7 @@ export class PdfService {
rgb(0, 0, 0), rgb(0, 0, 0),
align, align,
parWidth, parWidth,
indent, false,
margens.top, margens.top,
margens.bottom, margens.bottom,
PAGE_HEIGHT PAGE_HEIGHT