tenta fix pdfservice.js

This commit is contained in:
rodrigo committed 2025-04-21 22:38:21 -03:00
1 parent eeeae75919
commit ad3130a160
1 file changed
+12 -16
+12 -16
View File
@@ -378,7 +378,7 @@ export class PdfService {
}
// Junta todos os tokens em uma lista de palavras com formatação
let words: { text: string, font: any, underline: boolean }[] = [];
let words: { text: string, font: any, underline: boolean, bold: boolean, italic: boolean }[] = [];
for (const token of tokens) {
let usedFont = font;
if (token.bold && token.italic) usedFont = fontBoldItalic;
@@ -388,36 +388,33 @@ export class PdfService {
const split = token.text.split(/(\s+)/);
for (const part of split) {
if (part.length > 0) {
words.push({ text: part, font: usedFont, underline: token.underline });
words.push({ text: part, font: usedFont, underline: token.underline, bold: token.bold, italic: token.italic });
}
}
}
// Monta linhas respeitando largura máxima
let lines: { text: string, tokens: { text: string, font: any, underline: boolean }[] }[] = [];
let currentLine = '';
let currentTokens: { text: string, font: any, underline: boolean }[] = [];
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;
for (const [i, word] of words.entries()) {
// Aplica recuo de parágrafo na primeira linha
let recuo = 0;
if (indent && lines.length === 0 && currentLine === '') {
if (indent && lines.length === 0 && currentTokens.length === 0) {
recuo = fontSize * 2;
}
const wordWidth = word.font.widthOfTextAtSize(word.text, fontSize);
if (currentWidth + wordWidth + recuo > maxWidth && currentLine.trim() !== '') {
lines.push({ text: currentLine, tokens: currentTokens });
currentLine = word.text;
if (currentWidth + wordWidth + recuo > maxWidth && currentTokens.length > 0) {
lines.push({ tokens: currentTokens });
currentTokens = [word];
currentWidth = wordWidth;
} else {
currentLine += word.text;
currentTokens.push(word);
currentWidth += wordWidth;
}
}
if (currentLine.trim() !== '' || currentTokens.length > 0) {
lines.push({ text: currentLine, tokens: currentTokens });
if (currentTokens.length > 0) {
lines.push({ tokens: currentTokens });
}
// Renderiza cada linha
@@ -438,7 +435,7 @@ export class PdfService {
drawX = x + (maxWidth - totalWidth) / 2;
}
// Justificado (exceto última linha)
else if (align === 'justify' && i < lines.length - 1 && line.text.trim().length > 0) {
else if (align === 'justify' && i < lines.length - 1) {
// Conta espaços
const spaceCount = line.tokens.filter(t => /^\s+$/.test(t.text)).length;
const extraSpace = spaceCount > 0 ? (maxWidth - totalWidth) / spaceCount : 0;
@@ -460,10 +457,9 @@ export class PdfService {
color
});
}
if (/^\s+$/.test(t.text)) {
drawX += t.font.widthOfTextAtSize(t.text, fontSize) + extraSpace;
} else {
drawX += t.font.widthOfTextAtSize(t.text, fontSize);
if (/^\s+$/.test(t.text)) {
drawX += extraSpace;
}
}
y -= fontSize * 1.5;