tenta fix pdfservice.js

This commit is contained in:
rodrigo committed 2025-04-22 10:50:44 -03:00
1 parent a208caec55
commit 7f3313f3f3
1 file changed
+27 -7
+27 -7
View File
@@ -226,8 +226,10 @@ export class PdfService {
} }
try { try {
await fs.access(timbradoPath); await fs.access(timbradoPath);
console.log('Timbrado encontrado em:', timbradoPath);
timbradoExists = true; timbradoExists = true;
} catch (e) { } catch (e) {
console.error('Arquivo de timbrado não encontrado:', timbradoPath, e);
timbradoExists = false; timbradoExists = false;
} }
} }
@@ -240,6 +242,8 @@ export class PdfService {
bottom: config.timbradoMarginBottom || 50 bottom: config.timbradoMarginBottom || 50
}; };
console.log('Usando margens:', margens);
// Parâmetros de página // Parâmetros de página
const PAGE_WIDTH = 595.28; // A4 const PAGE_WIDTH = 595.28; // A4
const PAGE_HEIGHT = 841.89; // A4 const PAGE_HEIGHT = 841.89; // A4
@@ -628,22 +632,36 @@ export class PdfService {
let timbradoPageCount = 0; let timbradoPageCount = 0;
let timbradoPages: any[] = []; let timbradoPages: any[] = [];
if (timbradoExists) { if (timbradoExists) {
const timbradoBytes = await fs.readFile(timbradoPath); try {
timbradoPdf = await PDFDocument.load(timbradoBytes); console.log('Carregando timbrado PDF...');
timbradoPageCount = timbradoPdf.getPageCount(); const timbradoBytes = await fs.readFile(timbradoPath);
timbradoPages = []; timbradoPdf = await PDFDocument.load(timbradoBytes);
for (let i = 0; i < timbradoPageCount; i++) { timbradoPageCount = timbradoPdf.getPageCount();
const [embedded] = await pdfDoc.embedPages([timbradoPdf.getPage(i)]); console.log(`Timbrado carregado com ${timbradoPageCount} páginas`);
timbradoPages.push(embedded);
timbradoPages = [];
for (let i = 0; i < timbradoPageCount; i++) {
console.log(`Incorporando página ${i+1} do timbrado`);
const [embedded] = await pdfDoc.embedPages([timbradoPdf.getPage(i)]);
timbradoPages.push(embedded);
}
} catch (error) {
console.error('Erro ao carregar timbrado:', error);
timbradoExists = false;
} }
} }
let currentPar = 0; let currentPar = 0;
let pageIndex = 0; let pageIndex = 0;
console.log(`Gerando PDF com ${paragrafos.length} parágrafos`);
while (currentPar < paragrafos.length) { while (currentPar < paragrafos.length) {
console.log(`Criando página ${pageIndex + 1}, processando parágrafo ${currentPar + 1} de ${paragrafos.length}`);
let page = pdfDoc.addPage([PAGE_WIDTH, PAGE_HEIGHT]); let page = pdfDoc.addPage([PAGE_WIDTH, PAGE_HEIGHT]);
if (timbradoPages.length > 0) { if (timbradoPages.length > 0) {
const embedded = timbradoPages[pageIndex % timbradoPages.length]; const embedded = timbradoPages[pageIndex % timbradoPages.length];
console.log(`Adicionando timbrado na página ${pageIndex + 1}`);
page.drawPage(embedded, { page.drawPage(embedded, {
x: 0, x: 0,
y: 0, y: 0,
@@ -699,6 +717,7 @@ export class PdfService {
); );
if (yAfter === null) { if (yAfter === null) {
console.log(`Página ${pageIndex + 1} está cheia, criando nova página`);
pageIndex++; pageIndex++;
break; break;
} }
@@ -709,6 +728,7 @@ export class PdfService {
pageIndex++; pageIndex++;
} }
console.log(`PDF gerado com ${pageIndex} páginas`);
const pdfBytes = await pdfDoc.save(); const pdfBytes = await pdfDoc.save();
return Buffer.from(pdfBytes); return Buffer.from(pdfBytes);
} catch (error) { } catch (error) {