From 4726ce1dfa6997a81e821f51ea7de0dfe9566ed3 Mon Sep 17 00:00:00 2001 From: Rodrigo Martins Date: Mon, 21 Apr 2025 23:01:05 -0300 Subject: [PATCH] tenta fix pdfservice.js --- src/server/services/PdfService.ts | 73 ++++++------------------------- 1 file changed, 14 insertions(+), 59 deletions(-) diff --git a/src/server/services/PdfService.ts b/src/server/services/PdfService.ts index 4f3eb31..44189b9 100644 --- a/src/server/services/PdfService.ts +++ b/src/server/services/PdfService.ts @@ -398,7 +398,17 @@ export class PdfService { for (const [i, word] of words.entries()) { // Aplica recuo de parágrafo na primeira linha (apenas se alinhamento for justify ou left) let recuo = 0; - if (indent && lines.length === 0 && currentTokens.length === 0 && (align === 'justify' || align === 'left')) { + // Detecta se é uma linha de lista (ex: "1. ", "a) ", "1) ") e NÃO aplica recuo + const isLista = /^\s*((\d+\.|[a-zA-Z]\)|\d+\)))\s+/.test( + lines.length === 0 && currentTokens.length === 0 ? word.text : '' + ); + if ( + indent && + lines.length === 0 && + currentTokens.length === 0 && + (align === 'justify' || align === 'left') && + !isLista + ) { recuo = fontSize * 2; } const wordWidth = word.font.widthOfTextAtSize(word.text, fontSize); @@ -436,68 +446,13 @@ export class PdfService { // Aplica recuo de parágrafo na primeira linha (apenas se alinhamento for justify ou left) let recuo = 0; - if (indent && i === 0 && (align === 'justify' || align === 'left')) { + // Detecta se é uma linha de lista (ex: "1. ", "a) ", "1) ") e NÃO aplica recuo + const isListaLinha = i === 0 && line.tokens.length > 0 && /^\s*((\d+\.|[a-zA-Z]\)|\d+\)))\s+/.test(line.tokens[0].text); + if (indent && i === 0 && (align === 'justify' || align === 'left') && !isListaLinha) { recuo = fontSize * 2; drawX += recuo; } - // CORREÇÃO: Se align não for definido, usar 'left' por padrão, mas se center, centralizar corretamente - let effectiveAlign = align || 'left'; - - if (effectiveAlign === 'center') { - drawX = x + (maxWidth - totalWidth) / 2; - for (const t of line.tokens) { - page.drawText(t.text, { - x: drawX, - y, - size: fontSize, - font: t.font, - color - }); - if (t.underline && t.text.trim() !== '') { - const underlineY = y - 1.5; - page.drawLine({ - start: { x: drawX, y: underlineY }, - end: { x: drawX + t.font.widthOfTextAtSize(t.text, fontSize), y: underlineY }, - thickness: 0.75, - color - }); - } - drawX += t.font.widthOfTextAtSize(t.text, fontSize); - } - y -= fontSize * 1.5; - continue; - } - // Justificado (exceto última linha) - else if (effectiveAlign === 'justify' && i < lines.length - 1) { - const spaceCount = line.tokens.filter(t => /^\s+$/.test(t.text)).length; - const extraSpace = spaceCount > 0 ? (maxWidth - totalWidth) / spaceCount : 0; - for (const t of line.tokens) { - page.drawText(t.text, { - x: drawX, - y, - size: fontSize, - font: t.font, - color - }); - if (t.underline && t.text.trim() !== '') { - const underlineY = y - 1.5; - page.drawLine({ - start: { x: drawX, y: underlineY }, - end: { x: drawX + t.font.widthOfTextAtSize(t.text, fontSize), y: underlineY }, - thickness: 0.75, - color - }); - } - drawX += t.font.widthOfTextAtSize(t.text, fontSize); - if (/^\s+$/.test(t.text)) { - drawX += extraSpace; - } - } - y -= fontSize * 1.5; - continue; - } - // Alinhamento à esquerda (ou última linha justificada) for (const t of line.tokens) { page.drawText(t.text, {