tenta fix pdfservice.js

This commit is contained in:
rodrigo committed 2025-04-22 09:56:28 -03:00
1 parent 0121f67c65
commit cb47df6e88
1 file changed
+44 -25
+44 -25
View File
@@ -412,6 +412,11 @@ export class PdfService {
lines.push({ tokens: currentTokens }); lines.push({ tokens: currentTokens });
} }
// Ajuste: Força alinhamento consistente para todas as linhas do parágrafo
// Se align === 'center', todas as linhas centralizadas
// Se align === 'justify', todas as linhas justificadas (inclusive a última)
// Se align === 'left' ou outro, todas à esquerda
for (let i = 0; i < lines.length; i++) { for (let i = 0; i < lines.length; i++) {
if (y < bottomMargin + fontSize * 1.5) { if (y < bottomMargin + fontSize * 1.5) {
return null; return null;
@@ -420,13 +425,29 @@ export class PdfService {
let drawX = x; let drawX = x;
let totalWidth = line.tokens.reduce((acc, t) => acc + t.font.widthOfTextAtSize(t.text, fontSize), 0); let totalWidth = line.tokens.reduce((acc, t) => acc + t.font.widthOfTextAtSize(t.text, fontSize), 0);
// Alinhamento
if (align === 'center') { if (align === 'center') {
drawX = x + (maxWidth - totalWidth) / 2; drawX = x + (maxWidth - totalWidth) / 2;
} else if (align === 'right') { for (const t of line.tokens) {
drawX = x + (maxWidth - totalWidth); page.drawText(t.text, {
} else if (align === 'justify' && i !== lines.length - 1 && line.tokens.length > 1) { x: drawX,
// Justify: distribui espaço entre palavras (menos na última linha) 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);
}
} else if (align === 'justify') {
// Justify: distribui espaço entre palavras em TODAS as linhas
const spaces = line.tokens.filter(t => /^\s+$/.test(t.text)); const spaces = line.tokens.filter(t => /^\s+$/.test(t.text));
const numSpaces = spaces.length; const numSpaces = spaces.length;
const lineTextWidth = line.tokens.reduce((acc, t) => acc + t.font.widthOfTextAtSize(t.text, fontSize), 0); const lineTextWidth = line.tokens.reduce((acc, t) => acc + t.font.widthOfTextAtSize(t.text, fontSize), 0);
@@ -459,29 +480,27 @@ export class PdfService {
localDrawX += t.font.widthOfTextAtSize(t.text, fontSize); localDrawX += t.font.widthOfTextAtSize(t.text, fontSize);
} }
} }
y -= fontSize * 1.5; } else {
continue; // Alinhamento à esquerda (padrão)
} for (const t of line.tokens) {
page.drawText(t.text, {
// Alinhamento padrão (esquerda, centro, direita) x: drawX,
for (const t of line.tokens) { y,
page.drawText(t.text, { size: fontSize,
x: drawX, font: t.font,
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 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);
} }
drawX += t.font.widthOfTextAtSize(t.text, fontSize);
} }
y -= fontSize * 1.5; y -= fontSize * 1.5;
} }