tenta fix pdfservice.js

This commit is contained in:
rodrigo committed 2025-04-22 14:49:30 -03:00
1 parent 6fdfc99e7d
commit 597236c525
1 file changed
+294 -272
+294 -272
View File
@@ -290,30 +290,30 @@ export class PdfService {
// Preserva quebras de linha HTML como marcadores especiais // Preserva quebras de linha HTML como marcadores especiais
.replace(/<br\s*\/?>|<\/p><p>|<\/div><div>/gi, '\n') .replace(/<br\s*\/?>|<\/p><p>|<\/div><div>/gi, '\n')
// Substitui parágrafos, divs e blockquote por marcadores de bloco + quebra de linha // Substitui parágrafos, divs e blockquote por marcadores de bloco + quebra de linha
.replace(/<p[^>]*style="[^"]*text-align\s*:\s*(center|right|justify)[^"]*"[^>]*>/gi, .replace(/<p[^>]*style="[^"]*text-align\s*:\s*(center|right|justify)[^"]*"[^>]*>/gi, >]*align="(center|right|justify)"[^>]*>/gi,
(match, align) => `<p style="text-align:${align}">`) (match, align) => `<p style="text-align:${align}">`)yle || alignAttr}§§`)
.replace(/<div[^>]*style="[^"]*text-align\s*:\s*(center|right|justify)[^"]*"[^>]*>/gi, .replace(/<div[^>]*style="[^"]*text-align\s*:\s*(center|right|justify)[^"]*"[^>]*>/gi, [^>]*align="(center|right|justify)"[^>]*>/gi,
(match, align) => `<div style="text-align:${align}">`) (match, align) => `<div style="text-align:${align}">`)e || alignAttr}§§`)
.replace(/<p[^>]*>|<div[^>]*>|<blockquote[^>]*>/gi, '') .replace(/<p[^>]*>|<div[^>]*>|<blockquote[^>]*>/gi, '')res de bloco + quebra de linha
.replace(/<\/p>|<\/div>|<\/blockquote>/gi, '\n\n') .replace(/<\/p>|<\/div>|<\/blockquote>/gi, '\n\n'), '')
// Marca formatação de texto // Marca formatação de textolockquote>/gi, '\n\n')
.replace(/<b>|<strong>/gi, '<b>') .replace(/<b>|<strong>/gi, '<b>')
.replace(/<\/b>|<\/strong>/gi, '</b>') .replace(/<\/b>|<\/strong>/gi, '</b>')
.replace(/<i>|<em>/gi, '<i>') .replace(/<i>|<em>/gi, '<i>'), '</b>')
.replace(/<\/i>|<\/em>/gi, '</i>') .replace(/<\/i>|<\/em>/gi, '</i>')
// Normaliza espaços extras e quebras de linha // Normaliza espaços extras e quebras de linha
.replace(/\n{3,}/g, '\n\n') .replace(/\n{3,}/g, '\n\n')bém
.replace(/[ \t]+/g, ' '); // Usamos itálico para representar sublinhado
.replace(/<\/u>/gi, '</i>')
return cleanHtml;paços extras e quebras de linha
} .replace(/\n{3,}/g, '\n\n')
.replace(/[ \t]+/g, ' '); .replace(/[ \t]+/g, ' ');
return cleanHtml;
}
// Função para decodificar entidades HTML comuns // Função para decodificar entidades HTML comuns
function decodeHtmlEntities(str: string): string { function decodeHtmlEntities(str: string): string {
if (!str) return ''; if (!str) return '';
return str return strra decodificar entidades HTML comuns
.replace(/\r\n/g, '\n') .replace(/\r\n/g, '\n')(str: string): string {
// Acentos e caracteres especiais // Acentos e caracteres especiais
.replace(/&Aacute;/g, 'Á').replace(/&aacute;/g, 'á') .replace(/&Aacute;/g, 'Á').replace(/&aacute;/g, 'á')
.replace(/&Eacute;/g, 'É').replace(/&eacute;/g, 'é') .replace(/&Eacute;/g, 'É').replace(/&eacute;/g, 'é')
@@ -325,168 +325,168 @@ export class PdfService {
.replace(/&Igrave;/g, 'Ì').replace(/&igrave;/g, 'ì') .replace(/&Igrave;/g, 'Ì').replace(/&igrave;/g, 'ì')
.replace(/&Ograve;/g, 'Ò').replace(/&ograve;/g, 'ò') .replace(/&Ograve;/g, 'Ò').replace(/&ograve;/g, 'ò')
.replace(/&Ugrave;/g, 'Ù').replace(/&ugrave;/g, 'ù') .replace(/&Ugrave;/g, 'Ù').replace(/&ugrave;/g, 'ù')
.replace(/&Acirc;/g, 'Â').replace(/&acirc;/g, 'â') .replace(/&Acirc;/g, 'Â').replace(/&acirc;/g, 'â')')
.replace(/&Ecirc;/g, 'Ê').replace(/&ecirc;/g, 'ê') .replace(/&Ecirc;/g, 'Ê').replace(/&ecirc;/g, 'ê')')
.replace(/&Icirc;/g, 'Î').replace(/&icirc;/g, 'î') .replace(/&Icirc;/g, 'Î').replace(/&icirc;/g, 'î')')
.replace(/&Ocirc;/g, 'Ô').replace(/&ocirc;/g, 'ô') .replace(/&Ocirc;/g, 'Ô').replace(/&ocirc;/g, 'ô')')
.replace(/&Ucirc;/g, 'Û').replace(/&ucirc;/g, 'û') .replace(/&Ucirc;/g, 'Û').replace(/&ucirc;/g, 'û')
.replace(/&Atilde;/g, 'Ã').replace(/&atilde;/g, 'ã') .replace(/&Atilde;/g, 'Ã').replace(/&atilde;/g, 'ã')
.replace(/&Otilde;/g, 'Õ').replace(/&otilde;/g, 'õ') .replace(/&Otilde;/g, 'Õ').replace(/&otilde;/g, 'õ')
.replace(/&Ccedil;/g, 'Ç').replace(/&ccedil;/g, 'ç') .replace(/&Ccedil;/g, 'Ç').replace(/&ccedil;/g, 'ç')
.replace(/&Ntilde;/g, 'Ñ').replace(/&ntilde;/g, 'ñ') .replace(/&Ntilde;/g, 'Ñ').replace(/&ntilde;/g, 'ñ')
.replace(/&Uuml;/g, 'Ü').replace(/&uuml;/g, 'ü') .replace(/&Uuml;/g, 'Ü').replace(/&uuml;/g, 'ü')'ã')
// Símbolos e caracteres especiais // Símbolos e caracteres especiais(/&otilde;/g, 'õ')
.replace(/&ordm;/g, 'º') .replace(/&ordm;/g, 'º')').replace(/&ccedil;/g, 'ç')
.replace(/&ordf;/g, 'ª') .replace(/&ordf;/g, 'ª')').replace(/&ntilde;/g, 'ñ')
.replace(/&sect;/g, '§') .replace(/&sect;/g, '§').replace(/&uuml;/g, 'ü')
.replace(/&deg;/g, '°') .replace(/&deg;/g, '°')s especiais
.replace(/&nbsp;/g, ' ') .replace(/&nbsp;/g, ' ')
.replace(/&amp;/g, '&') .replace(/&amp;/g, '&'))
.replace(/&quot;/g, '"') .replace(/&quot;/g, '"')
.replace(/&#39;/g, "'") .replace(/&#39;/g, "'")
.replace(/&lt;/g, '<') .replace(/&lt;/g, '<')')
.replace(/&gt;/g, '>'); .replace(/&gt;/g, '>');
} } .replace(/&quot;/g, '"')
.replace(/&#39;/g, "'")
/** /** .replace(/&lt;/g, '<')
* Renderiza um parágrafo com formatação inline (negrito, itálico) * Renderiza um parágrafo com formatação inline (negrito, itálico)
* @returns A posição Y após renderizar ou null se não coube na página * @returns A posição Y após renderizar ou null se não coube na página
*/ */
function renderInlineFormattedParagraph( function renderInlineFormattedParagraph(
page: any, page: any, um parágrafo com formatação inline (negrito, itálico)
text: string, text: string,osição Y após renderizar ou null se não coube na página
x: number, x: number,
startY: number, startY: number,lineFormattedParagraph(
normalFont: any, normalFont: any,
boldFont: any, boldFont: any,
italicFont: any, italicFont: any,
boldItalicFont: any, boldItalicFont: any,
fontSize: number, fontSize: number,
color: any, color: any,ny,
align: 'left' | 'center' | 'right' | 'justify', align: 'left' | 'center' | 'right' | 'justify',
maxWidth: number, maxWidth: number,ny,
debug: boolean = false, debug: boolean = false,
marginTop: number, marginTop: number,
marginBottom: number, marginBottom: number,er' | 'right' | 'justify',
pageHeight: number pageHeight: number
): number | null { ): number | null { false,
const lineHeight = fontSize * 1.5; const lineHeight = fontSize * 1.5;
let y = startY; let y = startY;umber,
let currentX = x; let currentX = x;r
let words: Array<{text: string, font: any, width: number}> = []; let words: Array<{text: string, font: any, width: number}> = [];
let currentLine: Array<{text: string, font: any, width: number}> = []; let currentLine: Array<{text: string, font: any, width: number}> = [];
let currentLineWidth = 0; let currentLineWidth = 0;
let currentX = x;
// Verifica se o texto parece ser um título (todo em maiúsculas ou curto) // Verifica se o texto parece ser um título (todo em maiúsculas ou curto)
const isTitle = text.toUpperCase() === text || text.length < 50; const isTitle = text.toUpperCase() === text || text.length < 50; = [];
let currentLineWidth = 0;
// Se for título, centralizar independente do alinhamento fornecido // Se for título, centralizar independente do alinhamento fornecido
const effectiveAlign = isTitle ? 'center' : align; const effectiveAlign = isTitle ? 'center' : align;em maiúsculas ou curto)
const isTitle = text.toUpperCase() === text || text.length < 50;
// Processa texto com formatação - remover tags HTML e aplicar estilos // Processa texto com formatação - remover tags HTML e aplicar estilos
let currentFont = normalFont; let currentFont = normalFont; independente do alinhamento fornecido
let boldActive = false; let boldActive = false;isTitle ? 'center' : align;
let italicActive = false; let italicActive = false;
// Processa texto com formatação - remover tags HTML e aplicar estilos
// Simplificar texto antes de processar para evitar loops infinitos // Simplificar texto antes de processar para evitar loops infinitos
// Remove tags não suportadas como <u>, <span>, etc. // Remove tags não suportadas como <u>, <span>, etc.
text = text text = textctive = false;
.replace(/<u>|<\/u>/gi, '') // Remover tags de sublinhados .replace(/<u>|<\/u>/gi, '') // Remover tags de sublinhados
.replace(/<span[^>]*>|<\/span>/gi, '') // Remover tags span .replace(/<span[^>]*>|<\/span>/gi, '') // Remover tags spaninitos
.replace(/<[^>]*style=[^>]*>/gi, '') // Remover atributos style .replace(/<[^>]*style=[^>]*>/gi, '') // Remover atributos style
.replace(/<(?!\/?(?:b|i|strong|em)(?!\w)[^>]*>)[^>]*>/gi, ''); // Manter apenas b, i, strong, em .replace(/<(?!\/?(?:b|i|strong|em)(?!\w)[^>]*>)[^>]*>/gi, ''); // Manter apenas b, i, strong, em
.replace(/<u>|<\/u>/gi, '') // Remover tags de sublinhados
// Primeiro, extrair partes do texto com suas formataçõesspan
let remainingText = text;^>]*>/gi, '') // Remover atributos style
let textParts: Array<{text: string, bold: boolean, italic: boolean}> = []; apenas b, i, strong, em
// Primeiro, extrair partes do texto com suas formatações while (remainingText.length > 0) {to com suas formatações
let remainingText = text;
let textParts: Array<{text: string, bold: boolean, italic: boolean}> = [];
while (remainingText.length > 0) {
const boldStartMatch = remainingText.match(/<b>|<strong>/i); const boldStartMatch = remainingText.match(/<b>|<strong>/i);
const italicStartMatch = remainingText.match(/<i>|<em>/i); const italicStartMatch = remainingText.match(/<i>|<em>/i);oolean}> = [];
const boldEndMatch = remainingText.match(/<\/b>|<\/strong>/i); const boldEndMatch = remainingText.match(/<\/b>|<\/strong>/i);
const italicEndMatch = remainingText.match(/<\/i>|<\/em>/i); const italicEndMatch = remainingText.match(/<\/i>|<\/em>/i);
const boldStartMatch = remainingText.match(/<b>|<strong>/i);
// Encontrar a próxima tag // Encontrar a próxima tagemainingText.match(/<i>|<em>/i);
const matches = [boldStartMatch, italicStartMatch, boldEndMatch, italicEndMatch] const matches = [boldStartMatch, italicStartMatch, boldEndMatch, italicEndMatch]
.filter(m => m !== null); .filter(m => m !== null);iningText.match(/<\/i>|<\/em>/i);
if (matches.length === 0) { if (matches.length === 0) {
// Não há mais tags, adicionar o texto restante // Não há mais tags, adicionar o texto restante, boldEndMatch, italicEndMatch]
if (remainingText.length > 0) { if (remainingText.length > 0) {
textParts.push({ textParts.push({
text: remainingText, text: remainingText,{
bold: boldActive, bold: boldActive,adicionar o texto restante
italic: italicActive italic: italicActive > 0) {
});tParts.push({
} text: remainingText,
break;ld: boldActive,
} italic: italicActive
}); });
}
break;
}
// Ordenar matches por índice // Ordenar matches por índice
matches.sort((a, b) => a!.index! - b!.index!); matches.sort((a, b) => a!.index! - b!.index!);
const nextMatch = matches[0]!; const nextMatch = matches[0]!;
const matchIndex = nextMatch.index!; const matchIndex = nextMatch.index!;
// Ordenar matches por índice
// Texto antes da próxima tag // Texto antes da próxima tagex! - b!.index!);
if (matchIndex > 0) { if (matchIndex > 0) {ches[0]!;
textParts.push({ textParts.push({ nextMatch.index!;
text: remainingText.substring(0, matchIndex), text: remainingText.substring(0, matchIndex),
bold: boldActive, bold: boldActive,xima tag
italic: italicActive italic: italicActive
}); });tParts.push({
} } text: remainingText.substring(0, matchIndex),
bold: boldActive,
// Atualizar estado de formatação // Atualizar estado de formatação
if (nextMatch[0].match(/<b>|<strong>/i)) boldActive = true; if (nextMatch[0].match(/<b>|<strong>/i)) boldActive = true;
else if (nextMatch[0].match(/<\/b>|<\/strong>/i)) boldActive = false; else if (nextMatch[0].match(/<\/b>|<\/strong>/i)) boldActive = false;
else if (nextMatch[0].match(/<i>|<em>/i)) italicActive = true; else if (nextMatch[0].match(/<i>|<em>/i)) italicActive = true;
else if (nextMatch[0].match(/<\/i>|<\/em>/i)) italicActive = false; else if (nextMatch[0].match(/<\/i>|<\/em>/i)) italicActive = false;
if (nextMatch[0].match(/<b>|<strong>/i)) boldActive = true;
// Remover o texto processado e a tag // Remover o texto processado e a tag/strong>/i)) boldActive = false;
remainingText = remainingText.substring(matchIndex + nextMatch[0].length); remainingText = remainingText.substring(matchIndex + nextMatch[0].length);
} } else if (nextMatch[0].match(/<\/i>|<\/em>/i)) italicActive = false;
// Processar as partes extraídas // Processar as partes extraídase a tag
for (const part of textParts) { for (const part of textParts) {.substring(matchIndex + nextMatch[0].length);
// Determinar a fonte baseado na formatação // Determinar a fonte baseado na formatação
let font = normalFont; let font = normalFont;
if (part.bold && part.italic) font = boldItalicFont; if (part.bold && part.italic) font = boldItalicFont;
else if (part.bold) font = boldFont; else if (part.bold) font = boldFont;
else if (part.italic) font = italicFont; else if (part.italic) font = italicFont;ção
let font = normalFont;
// Dividir em palavras // Dividir em palavrasitalic) font = boldItalicFont;
const partWords = part.text.split(/\s+/); const partWords = part.text.split(/\s+/);
else if (part.italic) font = italicFont;
for (const wordText of partWords) { for (const wordText of partWords) {
if (!wordText) continue; if (!wordText) continue;
nst partWords = part.text.split(/\s+/);
const width = font.widthOfTextAtSize(wordText, fontSize); const width = font.widthOfTextAtSize(wordText, fontSize);
words.push({ text: wordText, font, width }); words.push({ text: wordText, font, width });
words.push({ text: ' ', font, width: font.widthOfTextAtSize(' ', fontSize) }); words.push({ text: ' ', font, width: font.widthOfTextAtSize(' ', fontSize) });
} }
const width = font.widthOfTextAtSize(wordText, fontSize);
// Remove o último espaço adicionado // Remove o último espaço adicionado width });
if (words.length > 0) { if (words.length > 0) {', font, width: font.widthOfTextAtSize(' ', fontSize) });
words.pop(); words.pop();
} }
} } // Remove o último espaço adicionado
if (words.length > 0) {
// Verificar se há espaço suficiente na página para pelo menos uma linha // Verificar se há espaço suficiente na página para pelo menos uma linha
if (y - lineHeight < marginBottom) { if (y - lineHeight < marginBottom) {
return null; // Não há espaço suficiente para renderizar return null; // Não há espaço suficiente para renderizar
} }
// Verificar se há espaço suficiente na página para pelo menos uma linha
// Aumentar ligeiramente o espaçamento de linhas para títulos // Aumentar ligeiramente o espaçamento de linhas para títulos
const effectiveLineHeight = isTitle ? lineHeight * 1.3 : lineHeight; const effectiveLineHeight = isTitle ? lineHeight * 1.3 : lineHeight;
}
// Aumentar o tamanho da fonte para títulos // Aumentar o tamanho da fonte para títulos
const effectiveFontSize = isTitle ? fontSize * 1.2 : fontSize; const effectiveFontSize = isTitle ? fontSize * 1.2 : fontSize;
const effectiveLineHeight = isTitle ? lineHeight * 1.3 : lineHeight;
// Se for um título, adicionar um espaço extra antes // Se for um título, adicionar um espaço extra antes
if (isTitle && y < startY) { if (isTitle && y < startY) {te para títulos
y -= lineHeight * 0.5; y -= lineHeight * 0.5;= isTitle ? fontSize * 1.2 : fontSize;
} }
// Se for um título, adicionar um espaço extra antes
// Agora, renderizar as palavras em linhas // Agora, renderizar as palavras em linhas
for (let i = 0; i < words.length; i++) { for (let i = 0; i < words.length; i++) {
const word = words[i]; const word = words[i];
@@ -495,280 +495,302 @@ export class PdfService {
if (currentLineWidth + word.width <= maxWidth || currentLine.length === 0) { if (currentLineWidth + word.width <= maxWidth || currentLine.length === 0) {
// Adicionar palavra à linha atual // Adicionar palavra à linha atual
currentLine.push(word); currentLine.push(word);
currentLineWidth += word.width; currentLineWidth += word.width; linha atual
} else { } else {entLineWidth + word.width <= maxWidth || currentLine.length === 0) {
// Renderizar a linha atual // Renderizar a linha atuala atual
if (y - effectiveLineHeight < marginBottom) { if (y - effectiveLineHeight < marginBottom) {
return null; // Não há mais espaço na página return null; // Não há mais espaço na página
}lse {
// Renderizar a linha atual
// Ajustar X baseado no alinhamentonBottom) {
let lineX = x; // Não há mais espaço na página
} }
// Ajustar X baseado no alinhamento
let lineX = x;
// Para justificado, calcular espaçamentos extras entre palavras // Para justificado, calcular espaçamentos extras entre palavras
// Mas não justificar a última linha de um parágrafo ou títulos // Mas não justificar a última linha de um parágrafo ou títulos
const isLastLine = i === words.length - 1; const isLastLine = i === words.length - 1;
const shouldJustify = effectiveAlign === 'justify' && !isLastLine && !isTitle && currentLine.length > 1; const shouldJustify = effectiveAlign === 'justify' && !isLastLine && !isTitle && currentLine.length > 1;
const extraSpacing = shouldJustify ? const extraSpacing = shouldJustify ? entos extras entre palavras
(maxWidth - currentLineWidth) / (currentLine.length - 1) : 0; (maxWidth - currentLineWidth) / (currentLine.length - 1) : 0;
const isLastLine = i === words.length - 1;
if (effectiveAlign === 'center') { if (effectiveAlign === 'center') {gn === 'justify' && !isLastLine && !isTitle && currentLine.length > 1;lavras, mesmo para não justificado
lineX = x + (maxWidth - currentLineWidth) / 2; lineX = x + (maxWidth - currentLineWidth) / 2;ntre palavras
} else if (effectiveAlign === 'right') { } else if (effectiveAlign === 'right') {rentLine.length - 1) : 0;
lineX = x + maxWidth - currentLineWidth; lineX = x + maxWidth - currentLineWidth;
} }f (effectiveAlign === 'center') { lineX = x + (maxWidth - currentLineWidth) / 2;
lineX = x + (maxWidth - currentLineWidth) / 2;} else if (effectiveAlign === 'right') {
// Desenhar a linha // Desenhar a linhaeAlign === 'right') {dth - currentLineWidth;
let posX = lineX; let posX = lineX;Width - currentLineWidth;
for (let j = 0; j < currentLine.length; j++) { for (let j = 0; j < currentLine.length; j++) {
const item = currentLine[j]; const item = currentLine[j];
const itemFontSize = isTitle ? effectiveFontSize : fontSize; const itemFontSize = isTitle ? effectiveFontSize : fontSize;
t posX = lineX;r (let j = 0; j < currentLine.length; j++) {
page.drawText(item.text, { page.drawText(item.text, {ine.length; j++) {];
x: posX, x: posX, = currentLine[j];FontSize = isTitle ? effectiveFontSize : fontSize;
y: y, y: y,temFontSize = isTitle ? effectiveFontSize : fontSize;
font: item.font, font: item.font,
size: itemFontSize, size: itemFontSize,xt, {
color colorsX,
}); });: y,ont: item.font,
font: item.font, size: itemFontSize,
// Avançar posição X // Avançar posição X,
posX += item.width; posX += item.width;
});
// Adicionar espaço extra se for justificado (exceto no final da linha) // Adicionar espaço extra se for justificado (exceto no final da linha)
if (shouldJustify && j < currentLine.length - 1) { if (shouldJustify && j < currentLine.length - 1) {
posX += extraSpacing; posX += extraSpacing;
} } Adicionar espaço extra entre palavras
} } // Adicionar espaço extra se for justificado (exceto no final da linha) if (j < currentLine.length - 1) {
if (shouldJustify && j < currentLine.length - 1) { // Usar espaçamento mínimo ou espaçamento justificado, o que for maior
// Avançar para a próxima linha // Avançar para a próxima linhay ? Math.max(extraSpacing, minSpacing) : minSpacing;
y -= effectiveLineHeight; y -= effectiveLineHeight;
currentLine = [word]; currentLine = [word];
currentLineWidth = word.width; currentLineWidth = word.width;
// Avançar para a próxima linha
// Verificar se ainda temos espaço // Verificar se ainda temos espaço
if (y - effectiveLineHeight < marginBottom) { if (y - effectiveLineHeight < marginBottom) {
return null; return null;th = word.width;[word];
} }rrentLineWidth = word.width;
} } // Verificar se ainda temos espaço
} } if (y - effectiveLineHeight < marginBottom) { // Verificar se ainda temos espaço
return null; if (y - effectiveLineHeight < marginBottom) {
// Renderizar a última linha, se houver // Renderizar a última linha, se houver
if (currentLine.length > 0) { if (currentLine.length > 0) {
// Ajustar X baseado no alinhamento (última linha nunca é justificada, exceto se for curta) // Ajustar X baseado no alinhamento (última linha nunca é justificada, exceto se for curta)
let lineX = x; let lineX = x;
Renderizar a última linha, se houver
// Se for título ou alinhado ao centro/direita, ajustar posição // Se for título ou alinhado ao centro/direita, ajustar posição
if (effectiveAlign === 'center') { if (effectiveAlign === 'center') {o (última linha nunca é justificada, exceto se for curta)
lineX = x + (maxWidth - currentLineWidth) / 2; lineX = x + (maxWidth - currentLineWidth) / 2;or curta)
} else if (effectiveAlign === 'right') { } else if (effectiveAlign === 'right') {
lineX = x + maxWidth - currentLineWidth; lineX = x + maxWidth - currentLineWidth;eita, ajustar posição
} }f (effectiveAlign === 'center') {/ Se for título ou alinhado ao centro/direita, ajustar posição
lineX = x + (maxWidth - currentLineWidth) / 2;if (effectiveAlign === 'center') {
// Desenhar a linha // Desenhar a linhaeAlign === 'right') {idth - currentLineWidth) / 2;
let posX = lineX; let posX = lineX;Width - currentLineWidth;iveAlign === 'right') {
for (const item of currentLine) { for (const item of currentLine) {
const itemFontSize = isTitle ? effectiveFontSize : fontSize; const itemFontSize = isTitle ? effectiveFontSize : fontSize;
Desenhar a linha
page.drawText(item.text, { page.drawText(item.text, {
x: posX, x: posX,tem of currentLine) {ineX;
y: y, y: y,temFontSize = isTitle ? effectiveFontSize : fontSize;t item of currentLine) {
font: item.font, font: item.font,tiveFontSize : fontSize;
size: itemFontSize, size: itemFontSize,xt, {
color colorsX,awText(item.text, {
}); });: y,: posX,
posX += item.width; posX += item.width;
} } size: itemFontSize, font: item.font,
color size: itemFontSize,
y -= effectiveLineHeight; y -= effectiveLineHeight;
} } posX += item.width; });
} posX += item.width;
// Se for título, adicionar um espaço extra depois // Se for título, adicionar um espaço extra depois
if (isTitle) { if (isTitle) {veLineHeight;
y -= lineHeight * 0.5; y -= lineHeight * 0.5;
} }
// Se for título, adicionar um espaço extra depois
return y; return y;le) { título, adicionar um espaço extra depois
} } y -= lineHeight * 0.5; if (isTitle) {
} y -= lineHeight * 0.5;
// Processe o texto com as novas funções melhoradas // Processe o texto com as novas funções melhoradas
const textoLimpo = stripUnsupportedTags( const textoLimpo = stripUnsupportedTags(
this.processarTemplate(conteudo, escritorio) this.processarTemplate(conteudo, escritorio)
); );
const texto = decodeHtmlEntities(textoLimpo); const texto = decodeHtmlEntities(textoLimpo);oradas
console.log('Texto processado para renderização:', texto.substring(0, 100) + '...'); console.log('Texto processado para renderização:', texto.substring(0, 100) + '...');
// Corrigir o processamento de parágrafos para lidar melhor com formatações // Corrigir o processamento de parágrafos para lidar melhor com formatações
const paragrafos = texto.split(/\n{2,}/).map(p => { const paragrafos = texto.split(/\n{2,}/).map(p => {
let paragraph = p.trim(); let paragraph = p.trim();ities(textoLimpo);
if (!paragraph) return ''; if (!paragraph) return '';o para renderização:', texto.substring(0, 100) + '...');ties(textoLimpo);
Corrigir o processamento de parágrafos para lidar melhor com formataçõesnsole.log('Texto processado para renderização:', texto.substring(0, 100) + '...');
// Extrair informações de formatação // Extrair informações de formatação/).map(p => {fos para lidar melhor com formatações
let align = 'justify'; // Padrão agora é justificado para texto normal let align = 'left'; // Default align
if (!paragraph) return '';let paragraph = p.trim();
// Verifica se o texto parece ser um título por diversos critérios: // Verificar se há marcadores de alinhamento definidos pelo HTML
// 1. Começa com '#' (estilo markdown)
// 2. Todo em maiúsculas e relativamente curto
// 3. Começa com números seguidos de ponto (como "1. Título")
// 4. Termina com dois pontos e é curto (como "Considerando:")
const isTitle =
paragraph.startsWith('#') ||
(paragraph.toUpperCase() === paragraph && paragraph.length < 100) ||
/^\d+\.\s+\S+/.test(paragraph) ||
(paragraph.endsWith(':') && paragraph.length < 70);
// Se for um título, centraliza
if (isTitle) {
align = 'center';
paragraph = paragraph.replace(/^#+\s*/, ''); // Remove marcadores de título estilo markdown
}
// Processa alinhamentos explícitos (têm precedência)
const alignMatch = paragraph.match(/§§ALIGN=(center|right|justify|left)§§/); const alignMatch = paragraph.match(/§§ALIGN=(center|right|justify|left)§§/);
if (alignMatch) { if (alignMatch) {ficado para texto normal
align = alignMatch[1]; align = alignMatch[1];
paragraph = paragraph.replace(/§§ALIGN=(center|right|justify|left)§§/, ''); paragraph = paragraph.replace(/§§ALIGN=(center|right|justify|left)§§/, '');rios:
} '#' (estilo markdown) texto parece ser um título por diversos critérios:
ativamente curtoarkdown)
// Verifica se o texto tem formatação que indica um título
const hasStrongFormatting = paragraph.includes('<b>') && paragraph.includes('</b>');urto (como "Considerando:")e ponto (como "1. Título")
const hasUnderline = paragraph.includes('<u>') && paragraph.includes('</u>');
paragraph.startsWith('#') ||const isTitle =
// Verifica se o texto parece ser um título por diversos critérios:paragraph && paragraph.length < 100) ||
// 1. Começa com '#' (estilo markdown)+/.test(paragraph) ||oUpperCase() === paragraph && paragraph.length < 100) ||
// 2. Todo em maiúsculas e relativamente curtoth(':') && paragraph.length < 70);st(paragraph) ||
// 3. Começa com números seguidos de ponto (como "1. Título")
// 4. Termina com dois pontos e é curto (como "Considerando:")/ Se for um título, centraliza
// 5. Tem formatação especial como negrito ou sublinhado e é curtoif (isTitle) {// Se for um título, centraliza
const isTitle =
paragraph.startsWith('#') ||o estilo markdown
(paragraph.toUpperCase() === paragraph && paragraph.length < 100) ||^#+\s*/, ''); // Remove marcadores de título estilo markdown
/^\d+\.\s+\S+/.test(paragraph) ||
(paragraph.endsWith(':') && paragraph.length < 70) ||
((hasStrongFormatting || hasUnderline) && paragraph.length < 150);onst alignMatch = paragraph.match(/§§ALIGN=(center|right|justify|left)§§/);/ Processa alinhamentos explícitos (têm precedência)
if (alignMatch) {const alignMatch = paragraph.match(/§§ALIGN=(center|right|justify|left)§§/);
// Se for um título e não tiver alinhamento definido explicitamente, centralizaMatch[1]; {
if (isTitle && !alignMatch) {)§§/, '');
align = 'center';ALIGN=(center|right|justify|left)§§/, '');
} }
let indent = 0; // Se for um texto normal sem alinhamento definido, justifica);
const indentMatch = paragraph.match(/§§INDENT=(\d+)(pt|px|em)?§§/); if (!isTitle && !alignMatch) {f (indentMatch) {onst indentMatch = paragraph.match(/§§INDENT=(\d+)(pt|px|em)?§§/);
if (indentMatch) { align = 'justify'; indent = parseInt(indentMatch[1]);if (indentMatch) {
}entMatch[2] === 'em') indent *= 12; // Converter em para pontos aproximadamente= parseInt(indentMatch[1]);
place(/§§INDENT=\d+(pt|px|em)?§§/, '');em') indent *= 12; // Converter em para pontos aproximadamente
// Remove marcadores de título estilo markdown
paragraph = paragraph.replace(/^#+\s*/, '');
let indent = 0;text: paragraph.trim(), turn {
const indentMatch = paragraph.match(/§§INDENT=(\d+)(pt|px|em)?§§/);ft' | 'center' | 'right' | 'justify',m(),
if (indentMatch) {| 'center' | 'right' | 'justify',
indent = parseInt(indentMatch[1]); indent = parseInt(indentMatch[1]);
if (indentMatch[2] === 'em') indent *= 12; // Converter em para pontos aproximadamente if (indentMatch[2] === 'em') indent *= 12; // Converter em para pontos aproximadamente
paragraph = paragraph.replace(/§§INDENT=\d+(pt|px|em)?§§/, ''); paragraph = paragraph.replace(/§§INDENT=\d+(pt|px|em)?§§/, '');
} }
return { return {
text: paragraph.trim(), text: paragraph.trim(), srafos`);
align: align as 'left' | 'center' | 'right' | 'justify', align: align as 'left' | 'center' | 'right' | 'justify',oops infinitos
indent, indent,ngth && pageIndex < MAX_PAGES) {
isTitle isTitlefo ${currentPar + 1} de ${paragrafos.length}`);
}; };TH, PAGE_HEIGHT]);ndex + 1}, processando parágrafo ${currentPar + 1} de ${paragrafos.length}`);
}).filter(p => p.text); }).filter(p => p.text);nar timbrado à página= pdfDoc.addPage([PAGE_WIDTH, PAGE_HEIGHT]);
let currentPar = 0; let currentPar = 0;adoPages.length > 0) {nar timbrado à página
let pageIndex = 0; let pageIndex = 0;ando timbrado na página ${pageIndex + 1}`);th > 0) {
console.log(`Gerando PDF com ${paragrafos.length} parágrafos`); console.log(`Gerando PDF com ${paragrafos.length} parágrafos`);oPages[0], {ndo timbrado na página ${pageIndex + 1}`);
// Define um contador máximo para evitar loops infinitos // Define um contador máximo para evitar loops infinitos: 0,e.drawPage(timbradoPages[0], {
const MAX_PAGES = 100; const MAX_PAGES = 100; y: 0, x: 0,
while (currentPar < paragrafos.length && pageIndex < MAX_PAGES) { while (currentPar < paragrafos.length && pageIndex < MAX_PAGES) {
console.log(`Criando página ${pageIndex + 1}, processando parágrafo ${currentPar + 1} de ${paragrafos.length}`); console.log(`Criando página ${pageIndex + 1}, processando parágrafo ${currentPar + 1} de ${paragrafos.length}`);
let page = pdfDoc.addPage([PAGE_WIDTH, PAGE_HEIGHT]); let page = pdfDoc.addPage([PAGE_WIDTH, PAGE_HEIGHT]);
// Adicionar timbrado à página // Adicionar timbrado à página
if (timbradoPages.length > 0) { if (timbradoPages.length > 0) {
console.log(`Adicionando timbrado na página ${pageIndex + 1}`); console.log(`Adicionando timbrado na página ${pageIndex + 1}`);false; // Flag para verificar se processamos algum parágrafos.top;
page.drawPage(timbradoPages[0], { page.drawPage(timbradoPages[0], {na página atuallse; // Flag para verificar se processamos algum parágrafo
x: 0, x: 0,) {na atual
y: 0, y: 0,
width: PAGE_WIDTH, width: PAGE_WIDTH,const text = paraObj.text;const paraObj = paragrafos[currentPar];
height: PAGE_HEIGHT height: PAGE_HEIGHT
}); });;
} }const isTitle = paraObj.isTitle || false;const indent = paraObj.indent || 0;
let y = PAGE_HEIGHT - margens.top; let y = PAGE_HEIGHT - margens.top;
let processedAnyParagraph = false; // Flag para verificar se processamos algum parágrafo let processedAnyParagraph = false; // Flag para verificar se processamos algum parágrafo indent;
// Tenta processar parágrafos na página atual // Tenta processar parágrafos na página atualMAX_WIDTH - indent;.left + indent;
while (currentPar < paragrafos.length) { while (currentPar < paragrafos.length) {
const paraObj = paragrafos[currentPar]; const paraObj = paragrafos[currentPar];to for muito curto, avance para o próximo parágrafo
const text = paraObj.text; const text = paraObj.text;f (text.length < 2) {/ Se o texto for muito curto, avance para o próximo parágrafo
const align = paraObj.align; const align = paraObj.align; currentPar++;if (text.length < 2) {
const indent = paraObj.indent || 0; const indent = paraObj.indent || 0;
const isTitle = paraObj.isTitle || false; const isTitle = paraObj.isTitle || false;
} continue;
let x = margens.left + indent; let x = margens.left + indent;
let parWidth = MAX_WIDTH - indent; let parWidth = MAX_WIDTH - indent;os
sTitle ? FONT_SIZE * 1.2 : FONT_SIZE;fonte para títulos
// Se o texto for muito curto, avance para o próximo parágrafo // Se o texto for muito curto, avance para o próximo parágrafot fontSizeToUse = isTitle ? FONT_SIZE * 1.2 : FONT_SIZE;
if (text.length < 2) { if (text.length < 2) {ágrafo da página)
currentPar++; currentPar++; o primeiro parágrafo da página)
processedAnyParagraph = true; processedAnyParagraph = true;T_SIZE * 0.8;e && processedAnyParagraph) {
continue; continue;-= FONT_SIZE * 0.8;
} } // Se não caber na página depois do espaço, vá para a próxima página
if (y < margens.bottom + fontSizeToUse * 2) { // Se não caber na página depois do espaço, vá para a próxima página
// Ajuste o tamanho da fonte para títulos // Ajuste o tamanho da fonte para títulos
const fontSizeToUse = isTitle ? FONT_SIZE * 1.2 : FONT_SIZE; const fontSizeToUse = isTitle ? FONT_SIZE * 1.2 : FONT_SIZE; } break;
// Adicionar espaço extra antes de títulos (exceto para o primeiro parágrafo da página) // Adicionar espaço extra antes de títulos (exceto para o primeiro parágrafo da página)
if (isTitle && processedAnyParagraph) { if (isTitle && processedAnyParagraph) {.log(`Tentando renderizar parágrafo ${currentPar + 1} em y=${y} com alinhamento ${align}${isTitle ? ' (título)' : ''}`);
y -= FONT_SIZE * 0.8; y -= FONT_SIZE * 0.8;log(`Tentando renderizar parágrafo ${currentPar + 1} em y=${y} com alinhamento ${align}${isTitle ? ' (título)' : ''}`);
yAfter = renderInlineFormattedParagraph(
// Se não caber na página depois do espaço, vá para a próxima página // Se não caber na página depois do espaço, vá para a próxima páginater = renderInlineFormattedParagraph(
if (y < margens.bottom + fontSizeToUse * 2) { if (y < margens.bottom + fontSizeToUse * 2) {
break; break;
} }
} }
console.log(`Tentando renderizar parágrafo ${currentPar + 1} em y=${y} com alinhamento ${align}${isTitle ? ' (título)' : ''}`); console.log(`Tentando renderizar parágrafo ${currentPar + 1} em y=${y} com alinhamento ${align}${isTitle ? ' (título)' : ''}`);alic,ld,
talic,c,
let yAfter = renderInlineFormattedParagraph( let yAfter = renderInlineFormattedParagraph(zeToUse,ldItalic,
page, page,,e,
text, text,
x, x,
y, y,false,parWidth,
font, font,
fontBold, fontBold,
fontItalic, fontItalic,
fontBoldItalic, fontBoldItalic,
fontSizeToUse, fontSizeToUse,
rgb(0, 0, 0), rgb(0, 0, 0),o conseguimos renderizar este parágrafo, vamos para a próxima página === null) {
align, align,ó se já tivermos processado algum parágrafo nesta páginao conseguimos renderizar este parágrafo, vamos para a próxima página
parWidth, parWidth,
false, false,;
margens.top, margens.top,
margens.bottom, margens.bottom,
PAGE_HEIGHT PAGE_HEIGHTcessar um parágrafo, algo está errado
); ); // Vamos avançar para o próximo parágrafo para evitar um loop infinito // Se não conseguimos nem processar um parágrafo, algo está errado
if (yAfter === null) { if (yAfter === null) {ole.log(`Não foi possível processar o parágrafo ${currentPar + 1}, avançando`);amos avançar para o próximo parágrafo para evitar um loop infinito
// Se não conseguimos renderizar este parágrafo, vamos para a próxima página // Se não conseguimos renderizar este parágrafo, vamos para a próxima páginar + 1}, avançando`);
// Mas só se já tivermos processado algum parágrafo nesta página // Mas só se já tivermos processado algum parágrafo nesta páginae;
if (processedAnyParagraph) { if (processedAnyParagraph) { true;
console.log(`Página ${pageIndex + 1} está cheia, criando nova página`); console.log(`Página ${pageIndex + 1} está cheia, criando nova página`);
break; break;
} else { } else {
// Se não conseguimos nem processar um parágrafo, algo está errado // Se não conseguimos nem processar um parágrafo, algo está errado
// Vamos avançar para o próximo parágrafo para evitar um loop infinito // Vamos avançar para o próximo parágrafo para evitar um loop infinito
console.log(`Não foi possível processar o parágrafo ${currentPar + 1}, avançando`); console.log(`Não foi possível processar o parágrafo ${currentPar + 1}, avançando`);log(`Parágrafo ${currentPar} processado com sucesso, nova posição y=${y}`);dAnyParagraph = true;
currentPar++; currentPar++;/ Verifica se ainda temos espaço para pelo menos uma linhaonsole.log(`Parágrafo ${currentPar} processado com sucesso, nova posição y=${y}`);
processedAnyParagraph = true; processedAnyParagraph = true; if (y < margens.bottom + FONT_SIZE * 2) { // Verifica se ainda temos espaço para pelo menos uma linha
} } console.log(`Espaço insuficiente para próximo parágrafo, criando nova página`); if (y < margens.bottom + FONT_SIZE * 2) {
} else { } else { break; console.log(`Espaço insuficiente para próximo parágrafo, criando nova página`);
// Parágrafo foi renderizado com sucesso // Parágrafo foi renderizado com sucesso
y = yAfter - FONT_SIZE * 0.5; y = yAfter - FONT_SIZE * 0.5; } }
currentPar++; currentPar++;
processedAnyParagraph = true; processedAnyParagraph = true;
console.log(`Parágrafo ${currentPar} processado com sucesso, nova posição y=${y}`); console.log(`Parágrafo ${currentPar} processado com sucesso, nova posição y=${y}`); pageIndex++;
// Verifica se ainda temos espaço para pelo menos uma linha // Verifica se ainda temos espaço para pelo menos uma linha } pageIndex++;
if (y < margens.bottom + FONT_SIZE * 2) { if (y < margens.bottom + FONT_SIZE * 2) {
console.log(`Espaço insuficiente para próximo parágrafo, criando nova página`); console.log(`Espaço insuficiente para próximo parágrafo, criando nova página`);de páginas (${MAX_PAGES}) atingido. Geração de PDF interrompida.`);
break; break;GES}) atingido. Geração de PDF interrompida.`);
} }
} });
} }es = await pdfDoc.save();`PDF gerado com ${pageIndex} páginas`);
return Buffer.from(pdfBytes); const pdfBytes = await pdfDoc.save();
pageIndex++; pageIndex++; } catch (error) { return Buffer.from(pdfBytes);
} } console.error('Erro geral ao gerar PDF:', error); } catch (error) {
if (pageIndex >= MAX_PAGES) { if (pageIndex >= MAX_PAGES) {
console.warn(`AVISO: Número máximo de páginas (${MAX_PAGES}) atingido. Geração de PDF interrompida.`); console.warn(`AVISO: Número máximo de páginas (${MAX_PAGES}) atingido. Geração de PDF interrompida.`);
} }
console.log(`PDF gerado com ${pageIndex} páginas`); console.log(`PDF gerado com ${pageIndex} páginas`);: string, dados: any): string {
const pdfBytes = await pdfDoc.save(); const pdfBytes = await pdfDoc.save();
return Buffer.from(pdfBytes); return Buffer.from(pdfBytes);m().split('.');{\{([^}]+)\}\}/g, (match, key) => {
} catch (error) { } catch (error) {= dados;arts = key.trim().split('.');
console.error('Erro geral ao gerar PDF:', error); console.error('Erro geral ao gerar PDF:', error);Parts) {
throw error; throw error;e && typeof value === 'object' && part in value) { part of keyParts) {
} } value = value[part];f (value && typeof value === 'object' && part in value) {
} } } else { value = value[part];
private processarTemplate(template: string, dados: any): string { private processarTemplate(template: string, dados: any): string { break; value = undefined;
return template.replace(/\{\{([^}]+)\}\}/g, (match, key) => { return template.replace(/\{\{([^}]+)\}\}/g, (match, key) => { } break;
const keyParts = key.trim().split('.'); const keyParts = key.trim().split('.'); } }
let value = dados;
for (const part of keyParts) {
if (value && typeof value === 'object' && part in value) {
value = value[part];
} else {
value = undefined;
break;
}
}
} } }); return value !== undefined ? value : match; } } break; value = undefined; } else { value = value[part]; if (value && typeof value === 'object' && part in value) { for (const part of keyParts) { let value = dados;
} } }); return value !== undefined ? value : match; }
return value !== undefined ? value : match; return value !== undefined ? value : match;
}); });
} }