From defd9b33f58de7fd1079396914a46e87ce9bd651 Mon Sep 17 00:00:00 2001 From: Rodrigo Martins Date: Mon, 21 Apr 2025 23:11:33 -0300 Subject: [PATCH] tenta fix pdfservice.js --- src/server/services/PdfService.ts | 38 ++++++++++++++----------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/src/server/services/PdfService.ts b/src/server/services/PdfService.ts index 414c2f6..679c58c 100644 --- a/src/server/services/PdfService.ts +++ b/src/server/services/PdfService.ts @@ -392,22 +392,22 @@ export class PdfService { } } + // NOVA ABORDAGEM: calcula o recuo apenas na PRIMEIRA linha do parágrafo, nunca nas demais let lines: { tokens: { text: string, font: any, underline: boolean, bold: boolean, italic: boolean }[] }[] = []; let currentTokens: { text: string, font: any, underline: boolean, bold: boolean, italic: boolean }[] = []; let currentWidth = 0; - // Defina recuo antes do loop - let recuo = indent ? fontSize * 2 : 0; + const recuo = indent ? fontSize * 2 : 0; for (const [i, word] of words.entries()) { const wordWidth = word.font.widthOfTextAtSize(word.text, fontSize); - // Corrige o cálculo de recuo apenas na primeira palavra da primeira linha + // Só aplica recuo na primeira linha do parágrafo if (lines.length === 0 && currentTokens.length === 0 && recuo > 0) { - if (wordWidth + recuo > maxWidth) { + if (wordWidth + recuo + currentWidth > maxWidth) { lines.push({ tokens: currentTokens }); currentTokens = [word]; currentWidth = wordWidth; } else { currentTokens.push(word); - currentWidth += wordWidth + recuo; + currentWidth += wordWidth; } } else { if (currentWidth + wordWidth > maxWidth && currentTokens.length > 0) { @@ -432,34 +432,30 @@ export class PdfService { let drawX = x; let totalWidth = line.tokens.reduce((acc, t) => acc + t.font.widthOfTextAtSize(t.text, fontSize), 0); - // Aplica recuo de parágrafo na primeira linha (apenas se alinhamento for justify ou left) - let recuo = 0; - // 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; - } + // Aplica recuo de parágrafo SOMENTE na primeira linha + let lineRecuo = (indent && i === 0) ? recuo : 0; + drawX += lineRecuo; // Alinhamento if (align === 'center') { - drawX = x + (maxWidth - totalWidth) / 2; + drawX = x + (maxWidth - totalWidth) / 2 + lineRecuo; } else if (align === 'right') { - drawX = x + (maxWidth - totalWidth); + drawX = x + (maxWidth - totalWidth) + lineRecuo; } else if (align === 'justify' && i !== lines.length - 1 && line.tokens.length > 1) { // Justify: distribui espaço entre palavras (menos na última linha) const wordsOnly = line.tokens.filter(t => t.text.trim() !== ''); const spaces = line.tokens.filter(t => /^\s+$/.test(t.text)); const numSpaces = spaces.length; const lineTextWidth = line.tokens.reduce((acc, t) => acc + t.font.widthOfTextAtSize(t.text, fontSize), 0); - const extraSpace = maxWidth - lineTextWidth; + const extraSpace = maxWidth - lineTextWidth - lineRecuo; let spaceWidth = 0; if (numSpaces > 0) { spaceWidth = extraSpace / numSpaces; } + let localDrawX = drawX; for (const t of line.tokens) { page.drawText(t.text, { - x: drawX, + x: localDrawX, y, size: fontSize, font: t.font, @@ -468,16 +464,16 @@ export class PdfService { 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 }, + start: { x: localDrawX, y: underlineY }, + end: { x: localDrawX + t.font.widthOfTextAtSize(t.text, fontSize), y: underlineY }, thickness: 0.75, color }); } if (/^\s+$/.test(t.text) && spaceWidth > 0) { - drawX += t.font.widthOfTextAtSize(t.text, fontSize) + spaceWidth; + localDrawX += t.font.widthOfTextAtSize(t.text, fontSize) + spaceWidth; } else { - drawX += t.font.widthOfTextAtSize(t.text, fontSize); + localDrawX += t.font.widthOfTextAtSize(t.text, fontSize); } } y -= fontSize * 1.5;