tenta fix pdfservice.js

This commit is contained in:
rodrigo committed 2025-04-21 22:50:08 -03:00
1 parent a946f86cae
commit bb5c9e25e2
1 file changed
+14 -2
+14 -2
View File
@@ -385,13 +385,13 @@ export class PdfService {
if (token.bold && token.italic) usedFont = fontBoldItalic; if (token.bold && token.italic) usedFont = fontBoldItalic;
else if (token.bold) usedFont = fontBold; else if (token.bold) usedFont = fontBold;
else if (token.italic) usedFont = fontItalic; else if (token.italic) usedFont = fontItalic;
// Corrige: split por espaço, mas preserva espaços como tokens separados
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) { // Adiciona todos os tokens, inclusive espaços, para manter alinhamento correto
words.push({ text: part, font: usedFont, underline: token.underline, bold: token.bold, italic: token.italic }); words.push({ text: part, font: usedFont, underline: token.underline, bold: token.bold, italic: token.italic });
} }
} }
}
let lines: { tokens: { text: string, font: any, underline: boolean, bold: boolean, italic: 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 currentTokens: { text: string, font: any, underline: boolean, bold: boolean, italic: boolean }[] = [];
@@ -403,15 +403,27 @@ export class PdfService {
recuo = fontSize * 2; recuo = fontSize * 2;
} }
const wordWidth = word.font.widthOfTextAtSize(word.text, fontSize); const wordWidth = word.font.widthOfTextAtSize(word.text, fontSize);
// Se for a primeira palavra da linha, soma o recuo
if (currentTokens.length === 0) {
if (currentWidth + wordWidth + recuo > maxWidth && currentTokens.length > 0) { if (currentWidth + wordWidth + recuo > maxWidth && currentTokens.length > 0) {
lines.push({ tokens: currentTokens }); lines.push({ tokens: currentTokens });
currentTokens = [word]; currentTokens = [word];
currentWidth = wordWidth; currentWidth = wordWidth;
} else {
currentTokens.push(word);
currentWidth += wordWidth + recuo;
}
} else {
if (currentWidth + wordWidth > maxWidth && currentTokens.length > 0) {
lines.push({ tokens: currentTokens });
currentTokens = [word];
currentWidth = wordWidth;
} else { } else {
currentTokens.push(word); currentTokens.push(word);
currentWidth += wordWidth; currentWidth += wordWidth;
} }
} }
}
if (currentTokens.length > 0) { if (currentTokens.length > 0) {
lines.push({ tokens: currentTokens }); lines.push({ tokens: currentTokens });
} }