tenta fix pdfservice.js
This commit is contained in:
1 parent
73b571be71
commit
3a3c64f1b3
1 file changed
+75
-46
@@ -251,48 +251,48 @@ export class PdfService {
|
|||||||
function decodeHtmlEntities(str: string): string {
|
function decodeHtmlEntities(str: string): string {
|
||||||
if (!str) return '';
|
if (!str) return '';
|
||||||
return str
|
return str
|
||||||
.replace(/á/gi, 'á')
|
// Acentos minúsculos e maiúsculos
|
||||||
.replace(/é/gi, 'é')
|
.replace(/Á/g, 'Á').replace(/á/g, 'á')
|
||||||
.replace(/í/gi, 'í')
|
.replace(/É/g, 'É').replace(/é/g, 'é')
|
||||||
.replace(/ó/gi, 'ó')
|
.replace(/Í/g, 'Í').replace(/í/g, 'í')
|
||||||
.replace(/ú/gi, 'ú')
|
.replace(/Ó/g, 'Ó').replace(/ó/g, 'ó')
|
||||||
.replace(/Á/g, 'Á')
|
.replace(/Ú/g, 'Ú').replace(/ú/g, 'ú')
|
||||||
.replace(/É/g, 'É')
|
.replace(/À/g, 'À').replace(/à/g, 'à')
|
||||||
.replace(/Í/g, 'Í')
|
.replace(/È/g, 'È').replace(/è/g, 'è')
|
||||||
.replace(/Ó/g, 'Ó')
|
.replace(/Ì/g, 'Ì').replace(/ì/g, 'ì')
|
||||||
.replace(/Ú/g, 'Ú')
|
.replace(/Ò/g, 'Ò').replace(/ò/g, 'ò')
|
||||||
.replace(/ã/gi, 'ã')
|
.replace(/Ù/g, 'Ù').replace(/ù/g, 'ù')
|
||||||
.replace(/õ/gi, 'õ')
|
.replace(/Â/g, 'Â').replace(/â/g, 'â')
|
||||||
.replace(/ñ/gi, 'ñ')
|
.replace(/Ê/g, 'Ê').replace(/ê/g, 'ê')
|
||||||
.replace(/ç/gi, 'ç')
|
.replace(/Î/g, 'Î').replace(/î/g, 'î')
|
||||||
.replace(/Ç/g, 'Ç')
|
.replace(/Ô/g, 'Ô').replace(/ô/g, 'ô')
|
||||||
.replace(/â/gi, 'â')
|
.replace(/Û/g, 'Û').replace(/û/g, 'û')
|
||||||
.replace(/ê/gi, 'ê')
|
.replace(/Ã/g, 'Ã').replace(/ã/g, 'ã')
|
||||||
.replace(/î/gi, 'î')
|
.replace(/Õ/g, 'Õ').replace(/õ/g, 'õ')
|
||||||
.replace(/ô/gi, 'ô')
|
.replace(/Ç/g, 'Ç').replace(/ç/g, 'ç')
|
||||||
.replace(/û/gi, 'û')
|
.replace(/Ñ/g, 'Ñ').replace(/ñ/g, 'ñ')
|
||||||
.replace(/à/gi, 'à')
|
.replace(/Ü/g, 'Ü').replace(/ü/g, 'ü')
|
||||||
.replace(/è/gi, 'è')
|
// Outras entidades comuns
|
||||||
.replace(/ì/gi, 'ì')
|
|
||||||
.replace(/ò/gi, 'ò')
|
|
||||||
.replace(/ù/gi, 'ù')
|
|
||||||
.replace(/ü/gi, 'ü')
|
|
||||||
.replace(/Ü/g, 'Ü')
|
|
||||||
.replace(/ /gi, ' ')
|
.replace(/ /gi, ' ')
|
||||||
.replace(/&/gi, '&')
|
.replace(/&/gi, '&')
|
||||||
.replace(/"/gi, '"')
|
.replace(/"/gi, '"')
|
||||||
.replace(/'/g, "'")
|
.replace(/'/g, "'")
|
||||||
.replace(/</gi, '<')
|
.replace(/</gi, '<')
|
||||||
.replace(/>/gi, '>')
|
.replace(/>/gi, '>')
|
||||||
|
.replace(/–/gi, '–')
|
||||||
|
.replace(/—/gi, '—')
|
||||||
.replace(/&#(\d+);/g, (m, code) => String.fromCharCode(code));
|
.replace(/&#(\d+);/g, (m, code) => String.fromCharCode(code));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Processar variáveis do template e decodificar entidades HTML
|
// Processar variáveis do template e decodificar entidades HTML
|
||||||
|
// Mantém tags <b>, <strong>, <i>, <em>, <u>, <br>, <p>, <span style="text-indent:2em;">
|
||||||
const texto = decodeHtmlEntities(
|
const texto = decodeHtmlEntities(
|
||||||
this.processarTemplate(conteudo, escritorio)
|
this.processarTemplate(conteudo, escritorio)
|
||||||
.replace(/<br\s*\/?>/gi, '\n')
|
.replace(/<br\s*\/?>/gi, '\n')
|
||||||
.replace(/<\/p>/gi, '\n\n')
|
.replace(/<\/p>/gi, '\n\n')
|
||||||
.replace(/<[^>]+>/g, '') // Remove tags HTML
|
.replace(/<p[^>]*>/gi, '') // Remove apenas a tag de abertura <p>
|
||||||
|
.replace(/<span[^>]*text-indent[^>]*>/gi, '§§INDENT§§') // Marca recuo de parágrafo
|
||||||
|
.replace(/<\/span>/gi, '')
|
||||||
.replace(/\r\n/g, '\n')
|
.replace(/\r\n/g, '\n')
|
||||||
.replace(/\r/g, '\n')
|
.replace(/\r/g, '\n')
|
||||||
.replace(/\n{3,}/g, '\n\n')
|
.replace(/\n{3,}/g, '\n\n')
|
||||||
@@ -303,24 +303,41 @@ export class PdfService {
|
|||||||
const paragrafos = texto.split(/\n{2,}/).map(p => p.trim()).filter(Boolean);
|
const paragrafos = texto.split(/\n{2,}/).map(p => p.trim()).filter(Boolean);
|
||||||
|
|
||||||
// Função para quebrar texto em linhas respeitando largura máxima e manter quebras de linha simples
|
// Função para quebrar texto em linhas respeitando largura máxima e manter quebras de linha simples
|
||||||
function wrapText(text: string, font: any, fontSize: number, maxWidth: number): string[] {
|
function wrapTextStyled(text: string, font: any, fontSize: number, maxWidth: number) {
|
||||||
// Mantém quebras de linha simples para espaçamento
|
// Mantém quebras de linha simples para espaçamento
|
||||||
const rawLines = text.split('\n');
|
const rawLines = text.split('\n');
|
||||||
const lines: string[] = [];
|
const lines: { text: string, bold: boolean, italic: boolean, underline: boolean, indent: boolean }[] = [];
|
||||||
for (const rawLine of rawLines) {
|
for (const rawLine of rawLines) {
|
||||||
const words = rawLine.split(' ');
|
let line = rawLine;
|
||||||
|
let bold = false, italic = false, underline = false, indent = false;
|
||||||
|
// Detecta recuo de parágrafo
|
||||||
|
if (line.startsWith('§§INDENT§§')) {
|
||||||
|
indent = true;
|
||||||
|
line = line.replace('§§INDENT§§', '').trimStart();
|
||||||
|
}
|
||||||
|
// Detecta negrito, itálico, sublinhado (apenas no início e fim da linha)
|
||||||
|
if (/^<b>|^<strong>/.test(line)) { bold = true; line = line.replace(/^<b>|^<strong>/, ''); }
|
||||||
|
if (/^<i>|^<em>/.test(line)) { italic = true; line = line.replace(/^<i>|^<em>/, ''); }
|
||||||
|
if (/^<u>/.test(line)) { underline = true; line = line.replace(/^<u>/, ''); }
|
||||||
|
if (/<\/b>$|<\/strong>$/.test(line)) { bold = true; line = line.replace(/<\/b>$|<\/strong>$/, ''); }
|
||||||
|
if (/<\/i>$|<\/em>$/.test(line)) { italic = true; line = line.replace(/<\/i>$|<\/em>$/, ''); }
|
||||||
|
if (/<\/u>$/.test(line)) { underline = true; line = line.replace(/<\/u>$/, ''); }
|
||||||
|
// Remove tags intermediárias (não suporta aninhamento)
|
||||||
|
line = line.replace(/<\/?(b|strong|i|em|u)>/gi, '');
|
||||||
|
// Quebra em palavras para justificar
|
||||||
|
const words = line.split(' ');
|
||||||
let currentLine = '';
|
let currentLine = '';
|
||||||
for (const word of words) {
|
for (const word of words) {
|
||||||
const testLine = currentLine ? currentLine + ' ' + word : word;
|
const testLine = currentLine ? currentLine + ' ' + word : word;
|
||||||
const width = font.widthOfTextAtSize(testLine, fontSize);
|
const width = font.widthOfTextAtSize(testLine, fontSize);
|
||||||
if (width > maxWidth && currentLine) {
|
if (width > maxWidth && currentLine) {
|
||||||
lines.push(currentLine);
|
lines.push({ text: currentLine, bold, italic, underline, indent });
|
||||||
currentLine = word;
|
currentLine = word;
|
||||||
} else {
|
} else {
|
||||||
currentLine = testLine;
|
currentLine = testLine;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (currentLine) lines.push(currentLine);
|
if (currentLine) lines.push({ text: currentLine, bold, italic, underline, indent });
|
||||||
}
|
}
|
||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
@@ -370,41 +387,43 @@ export class PdfService {
|
|||||||
let firstLine = true;
|
let firstLine = true;
|
||||||
// Escrever texto na página
|
// Escrever texto na página
|
||||||
while (currentPar < paragrafos.length) {
|
while (currentPar < paragrafos.length) {
|
||||||
const lines = wrapText(paragrafos[currentPar], font, FONT_SIZE, MAX_WIDTH);
|
const styledLines = wrapTextStyled(paragrafos[currentPar], font, FONT_SIZE, MAX_WIDTH);
|
||||||
let lineIdx = 0;
|
let lineIdx = 0;
|
||||||
if (currentLine > 0) lineIdx = currentLine;
|
if (currentLine > 0) lineIdx = currentLine;
|
||||||
for (; lineIdx < lines.length; lineIdx++) {
|
for (; lineIdx < styledLines.length; lineIdx++) {
|
||||||
if (y < margens.bottom + LINE_HEIGHT) {
|
if (y < margens.bottom + LINE_HEIGHT) {
|
||||||
currentLine = lineIdx;
|
currentLine = lineIdx;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// Justificado: calcula espaçamento extra entre palavras (exceto última linha do parágrafo)
|
// Justificado: calcula espaçamento extra entre palavras (exceto última linha do parágrafo)
|
||||||
let text = lines[lineIdx];
|
let { text, bold, italic, underline, indent } = styledLines[lineIdx];
|
||||||
let x = margens.left;
|
let x = margens.left + (indent ? FONT_SIZE * 2 : 0);
|
||||||
let options: any = {
|
let options: any = {
|
||||||
x,
|
x,
|
||||||
y,
|
y,
|
||||||
size: FONT_SIZE,
|
size: FONT_SIZE,
|
||||||
font
|
font: bold ? await pdfDoc.embedFont(StandardFonts.HelveticaBold)
|
||||||
|
: italic ? await pdfDoc.embedFont(StandardFonts.HelveticaOblique)
|
||||||
|
: font
|
||||||
};
|
};
|
||||||
if (text.trim().length > 0 && lineIdx < lines.length - 1) {
|
|
||||||
// Justificar (exceto última linha do parágrafo)
|
// Justificar (exceto última linha do parágrafo)
|
||||||
|
if (text.trim().length > 0 && lineIdx < styledLines.length - 1) {
|
||||||
const words = text.split(' ');
|
const words = text.split(' ');
|
||||||
if (words.length > 1) {
|
if (words.length > 1) {
|
||||||
const textWidth = font.widthOfTextAtSize(text, FONT_SIZE);
|
const textWidth = options.font.widthOfTextAtSize(text, FONT_SIZE);
|
||||||
const spaceWidth = font.widthOfTextAtSize(' ', FONT_SIZE);
|
const spaceWidth = options.font.widthOfTextAtSize(' ', FONT_SIZE);
|
||||||
const totalSpaces = words.length - 1;
|
const totalSpaces = words.length - 1;
|
||||||
const extraSpace = (MAX_WIDTH - textWidth) / totalSpaces;
|
const extraSpace = (MAX_WIDTH - (indent ? FONT_SIZE * 2 : 0) - textWidth) / totalSpaces;
|
||||||
let cursor = x;
|
let cursor = x;
|
||||||
for (let w = 0; w < words.length; w++) {
|
for (let w = 0; w < words.length; w++) {
|
||||||
page.drawText(words[w], {
|
page.drawText(words[w], {
|
||||||
x: cursor,
|
x: cursor,
|
||||||
y,
|
y,
|
||||||
size: FONT_SIZE,
|
size: FONT_SIZE,
|
||||||
font
|
font: options.font
|
||||||
});
|
});
|
||||||
if (w < words.length - 1) {
|
if (w < words.length - 1) {
|
||||||
cursor += font.widthOfTextAtSize(words[w], FONT_SIZE) + spaceWidth + extraSpace;
|
cursor += options.font.widthOfTextAtSize(words[w], FONT_SIZE) + spaceWidth + extraSpace;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -413,15 +432,25 @@ export class PdfService {
|
|||||||
} else {
|
} else {
|
||||||
page.drawText(text, options);
|
page.drawText(text, options);
|
||||||
}
|
}
|
||||||
|
// Sublinhado
|
||||||
|
if (underline) {
|
||||||
|
const textWidth = options.font.widthOfTextAtSize(text, FONT_SIZE);
|
||||||
|
page.drawLine({
|
||||||
|
start: { x, y: y - 2 },
|
||||||
|
end: { x: x + textWidth, y: y - 2 },
|
||||||
|
thickness: 0.7,
|
||||||
|
color: rgb(0, 0, 0)
|
||||||
|
});
|
||||||
|
}
|
||||||
y -= LINE_HEIGHT;
|
y -= LINE_HEIGHT;
|
||||||
currentLine = 0;
|
currentLine = 0;
|
||||||
firstLine = false;
|
firstLine = false;
|
||||||
}
|
}
|
||||||
// Espaço extra entre parágrafos
|
// Espaço extra entre parágrafos
|
||||||
if (lineIdx >= lines.length) {
|
if (lineIdx >= styledLines.length) {
|
||||||
y -= LINE_HEIGHT * 0.5;
|
y -= LINE_HEIGHT * 0.5;
|
||||||
}
|
}
|
||||||
if (lineIdx < lines.length) {
|
if (lineIdx < styledLines.length) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
currentPar++;
|
currentPar++;
|
||||||
|
|||||||
Reference in new issue
Block a user