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 // 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) { for (const token of tokens) {
let usedFont = font; let usedFont = font;
if (token.bold && token.italic) usedFont = fontBoldItalic; if (token.bold && token.italic) usedFont = fontBoldItalic;
@@ -388,36 +388,33 @@ export class PdfService {
const split = token.text.split(/(\s+)/); const split = token.text.split(/(\s+)/);
for (const part of split) { for (const part of split) {
if (part.length > 0) { 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 // Monta linhas respeitando largura máxima
let lines: { text: string, tokens: { text: string, font: any, underline: boolean }[] }[] = []; let lines: { tokens: { text: string, font: any, underline: boolean, bold: boolean, italic: boolean }[] }[] = [];
let currentLine = ''; let currentTokens: { text: string, font: any, underline: boolean, bold: boolean, italic: boolean }[] = [];
let currentTokens: { text: string, font: any, underline: boolean }[] = [];
let currentWidth = 0; let currentWidth = 0;
for (const [i, word] of words.entries()) { for (const [i, word] of words.entries()) {
// Aplica recuo de parágrafo na primeira linha // Aplica recuo de parágrafo na primeira linha
let recuo = 0; let recuo = 0;
if (indent && lines.length === 0 && currentLine === '') { if (indent && lines.length === 0 && currentTokens.length === 0) {
recuo = fontSize * 2; recuo = fontSize * 2;
} }
const wordWidth = word.font.widthOfTextAtSize(word.text, fontSize); const wordWidth = word.font.widthOfTextAtSize(word.text, fontSize);
if (currentWidth + wordWidth + recuo > maxWidth && currentLine.trim() !== '') { if (currentWidth + wordWidth + recuo > maxWidth && currentTokens.length > 0) {
lines.push({ text: currentLine, tokens: currentTokens }); lines.push({ tokens: currentTokens });
currentLine = word.text;
currentTokens = [word]; currentTokens = [word];
currentWidth = wordWidth; currentWidth = wordWidth;
} else { } else {
currentLine += word.text;
currentTokens.push(word); currentTokens.push(word);
currentWidth += wordWidth; currentWidth += wordWidth;
} }
} }
if (currentLine.trim() !== '' || currentTokens.length > 0) { if (currentTokens.length > 0) {
lines.push({ text: currentLine, tokens: currentTokens }); lines.push({ tokens: currentTokens });
} }
// Renderiza cada linha // Renderiza cada linha
@@ -438,7 +435,7 @@ export class PdfService {
drawX = x + (maxWidth - totalWidth) / 2; drawX = x + (maxWidth - totalWidth) / 2;
} }
// Justificado (exceto última linha) // 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 // Conta espaços
const spaceCount = line.tokens.filter(t => /^\s+$/.test(t.text)).length; const spaceCount = line.tokens.filter(t => /^\s+$/.test(t.text)).length;
const extraSpace = spaceCount > 0 ? (maxWidth - totalWidth) / spaceCount : 0; const extraSpace = spaceCount > 0 ? (maxWidth - totalWidth) / spaceCount : 0;
@@ -460,10 +457,9 @@ export class PdfService {
color color
}); });
} }
drawX += t.font.widthOfTextAtSize(t.text, fontSize);
if (/^\s+$/.test(t.text)) { if (/^\s+$/.test(t.text)) {
drawX += t.font.widthOfTextAtSize(t.text, fontSize) + extraSpace; drawX += extraSpace;
} else {
drawX += t.font.widthOfTextAtSize(t.text, fontSize);
} }
} }
y -= fontSize * 1.5; y -= fontSize * 1.5;