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
+40 -5
+40 -5
View File
@@ -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 <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
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
.replace(/<span\b(?:(?!text-indent|text-align)[^>])*?>/gi, '')
// Remove atributos de <b>, <strong>, <i>, <em>, <u>, <p>
.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>(.*?)<\/span>/gi, '$1')
// Remove comentários HTML
.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
function renderInlineFormattedParagraph(
page,
@@ -515,11 +531,15 @@ export class PdfService {
}
const texto = decodeHtmlEntities(
htmlListToText(
stripUnsupportedTags(
this.processarTemplate(conteudo, escritorio)
.replace(/<br\s*\/?>/gi, '\n')
.replace(/<\/p>/gi, '\n\n')
.replace(/<p[^>]*>/gi, '')
// Blockquote para recuo visual
.replace(/§§BLOCKQUOTE_START§§/g, '§§INDENT2§§')
.replace(/§§BLOCKQUOTE_END§§/g, '')
.replace(/<span[^>]*text-indent[^>]*>/gi, '§§INDENT§§')
.replace(/<span[^>]*text-align\s*:\s*center[^>]*>/gi, '§§CENTER§§')
.replace(/<\/span>/gi, '')
@@ -528,6 +548,7 @@ export class PdfService {
.replace(/\n{3,}/g, '\n\n')
.trim()
)
)
);
const paragrafos = texto.split(/\n{2,}/).map(p => p.trim()).filter(Boolean);
@@ -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