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;
|
return blocks;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Processar variáveis do template e decodificar entidades HTML
|
// Carregar o timbrado (se houver) e criar o PDF final
|
||||||
// Mantém tags <b>, <strong>, <i>, <em>, <u>, <br>, <p>, <span style="text-indent:2em;">, <span style="text-align:center;">
|
let timbradoPdf: PDFDocument | null = null;
|
||||||
const texto = decodeHtmlEntities(
|
let timbradoPageCount = 0;
|
||||||
this.processarTemplate(conteudo, escritorio)
|
let timbradoPages: any[] = [];
|
||||||
.replace(/<br\s*\/?>/gi, '\n')
|
let pdfDoc: PDFDocument = await PDFDocument.create();
|
||||||
.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()
|
|
||||||
);
|
|
||||||
|
|
||||||
// Quebra o texto em parágrafos (mantendo quebras de linha duplas como parágrafo)
|
if (timbradoExists) {
|
||||||
const paragrafos = texto.split(/\n{2,}/).map(p => p.trim()).filter(Boolean);
|
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
|
// Fonte Fira Sans (se disponível), senão Helvetica
|
||||||
let font = await pdfDoc.embedFont(StandardFonts.Helvetica);
|
let font = await pdfDoc.embedFont(StandardFonts.Helvetica);
|
||||||
@@ -400,26 +397,6 @@ export class PdfService {
|
|||||||
// Parse TinyMCE HTML para blocos estilizados
|
// Parse TinyMCE HTML para blocos estilizados
|
||||||
const blocks = parseHtmlToBlocks(conteudo);
|
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
|
// Renderização dos blocos no PDF
|
||||||
let pageIndex = 0;
|
let pageIndex = 0;
|
||||||
let y = PAGE_HEIGHT - margens.top;
|
let y = PAGE_HEIGHT - margens.top;
|
||||||
|
|||||||
Reference in new issue
Block a user