tenta fix pdfservice.js
This commit is contained in:
1 parent
3ccccc6792
commit
8a09720030
1 file changed
+16
-39
@@ -356,26 +356,23 @@ export class PdfService {
|
||||
return blocks;
|
||||
}
|
||||
|
||||
// Processar variáveis do template e decodificar entidades HTML
|
||||
// Mantém tags <b>, <strong>, <i>, <em>, <u>, <br>, <p>, <span style="text-indent:2em;">, <span style="text-align:center;">
|
||||
const texto = decodeHtmlEntities(
|
||||
this.processarTemplate(conteudo, escritorio)
|
||||
.replace(/<br\s*\/?>/gi, '\n')
|
||||
.replace(/<\/p>/gi, '\n\n')
|
||||
.replace(/<p[^>]*>/gi, '') // Remove apenas a tag de abertura <p>
|
||||
// Marca recuo de parágrafo
|
||||
.replace(/<span[^>]*text-indent[^>]*>/gi, '§§INDENT§§')
|
||||
// Marca centralização
|
||||
.replace(/<span[^>]*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;
|
||||
|
||||
Reference in new issue
Block a user