diff --git a/src/server/services/PdfService.ts b/src/server/services/PdfService.ts index e15b305..4d8f0d9 100644 --- a/src/server/services/PdfService.ts +++ b/src/server/services/PdfService.ts @@ -356,26 +356,23 @@ export class PdfService { return blocks; } - // Processar variáveis do template e decodificar entidades HTML - // Mantém tags , , , , ,
,

, , - const texto = decodeHtmlEntities( - this.processarTemplate(conteudo, escritorio) - .replace(//gi, '\n') - .replace(/<\/p>/gi, '\n\n') - .replace(/]*>/gi, '') // Remove apenas a tag de abertura

- // Marca recuo de parágrafo - .replace(/]*text-indent[^>]*>/gi, '§§INDENT§§') - // Marca centralização - .replace(/]*text-align\s*:\s*center[^>]*>/gi, '§§CENTER§§') - .replace(/<\/span>/gi, '') - .replace(/\r\n/g, '\n') - .replace(/\r/g, '\n') - .replace(/\n{3,}/g, '\n\n') - .trim() - ); + // Carregar o timbrado (se houver) e criar o PDF final + let timbradoPdf: PDFDocument | null = null; + let timbradoPageCount = 0; + let timbradoPages: any[] = []; + let pdfDoc: PDFDocument = await PDFDocument.create(); - // Quebra o texto em parágrafos (mantendo quebras de linha duplas como parágrafo) - const paragrafos = texto.split(/\n{2,}/).map(p => p.trim()).filter(Boolean); + if (timbradoExists) { + const timbradoBytes = await fs.readFile(timbradoPath); + timbradoPdf = await PDFDocument.load(timbradoBytes); + timbradoPageCount = timbradoPdf.getPageCount(); + // Copiar as páginas do timbrado como EMBEDDED PAGE (PDFEmbeddedPage) + timbradoPages = []; + for (let i = 0; i < timbradoPageCount; i++) { + const [embedded] = await pdfDoc.embedPages([timbradoPdf.getPage(i)]); + timbradoPages.push(embedded); + } + } // Fonte Fira Sans (se disponível), senão Helvetica let font = await pdfDoc.embedFont(StandardFonts.Helvetica); @@ -400,26 +397,6 @@ export class PdfService { // Parse TinyMCE HTML para blocos estilizados const blocks = parseHtmlToBlocks(conteudo); - // Carregar o timbrado (se houver) e criar o PDF final - let pdfDoc: PDFDocument; - let timbradoPdf: PDFDocument | null = null; - let timbradoPageCount = 0; - let timbradoPages: any[] = []; - if (timbradoExists) { - const timbradoBytes = await fs.readFile(timbradoPath); - timbradoPdf = await PDFDocument.load(timbradoBytes); - timbradoPageCount = timbradoPdf.getPageCount(); - pdfDoc = await PDFDocument.create(); - // Copiar as páginas do timbrado como EMBEDDED PAGE (PDFEmbeddedPage) - timbradoPages = []; - for (let i = 0; i < timbradoPageCount; i++) { - const [embedded] = await pdfDoc.embedPages([timbradoPdf.getPage(i)]); - timbradoPages.push(embedded); - } - } else { - pdfDoc = await PDFDocument.create(); - } - // Renderização dos blocos no PDF let pageIndex = 0; let y = PAGE_HEIGHT - margens.top;