diff --git a/src/server/services/PdfService.ts b/src/server/services/PdfService.ts index 018c1e8..60b9235 100644 --- a/src/server/services/PdfService.ts +++ b/src/server/services/PdfService.ts @@ -385,11 +385,11 @@ export class PdfService { if (token.bold && token.italic) usedFont = fontBoldItalic; else if (token.bold) usedFont = fontBold; else if (token.italic) usedFont = fontItalic; + // Corrige: split por espaço, mas preserva espaços como tokens separados const split = token.text.split(/(\s+)/); for (const part of split) { - if (part.length > 0) { - words.push({ text: part, font: usedFont, underline: token.underline, bold: token.bold, italic: token.italic }); - } + // Adiciona todos os tokens, inclusive espaços, para manter alinhamento correto + words.push({ text: part, font: usedFont, underline: token.underline, bold: token.bold, italic: token.italic }); } } @@ -403,13 +403,25 @@ export class PdfService { recuo = fontSize * 2; } const wordWidth = word.font.widthOfTextAtSize(word.text, fontSize); - if (currentWidth + wordWidth + recuo > maxWidth && currentTokens.length > 0) { - lines.push({ tokens: currentTokens }); - currentTokens = [word]; - currentWidth = wordWidth; + // Se for a primeira palavra da linha, soma o recuo + if (currentTokens.length === 0) { + if (currentWidth + wordWidth + recuo > maxWidth && currentTokens.length > 0) { + lines.push({ tokens: currentTokens }); + currentTokens = [word]; + currentWidth = wordWidth; + } else { + currentTokens.push(word); + currentWidth += wordWidth + recuo; + } } else { - currentTokens.push(word); - currentWidth += wordWidth; + if (currentWidth + wordWidth > maxWidth && currentTokens.length > 0) { + lines.push({ tokens: currentTokens }); + currentTokens = [word]; + currentWidth = wordWidth; + } else { + currentTokens.push(word); + currentWidth += wordWidth; + } } } if (currentTokens.length > 0) {