tenta fix pdfservice.js
This commit is contained in:
1 parent
8abf8791be
commit
defd9b33f5
1 file changed
+17
-21
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user