tenta fix pdfservice.js
This commit is contained in:
1 parent
a208caec55
commit
7f3313f3f3
1 file changed
+20
@@ -226,8 +226,10 @@ export class PdfService {
|
||||
}
|
||||
try {
|
||||
await fs.access(timbradoPath);
|
||||
console.log('Timbrado encontrado em:', timbradoPath);
|
||||
timbradoExists = true;
|
||||
} catch (e) {
|
||||
console.error('Arquivo de timbrado não encontrado:', timbradoPath, e);
|
||||
timbradoExists = false;
|
||||
}
|
||||
}
|
||||
@@ -240,6 +242,8 @@ export class PdfService {
|
||||
bottom: config.timbradoMarginBottom || 50
|
||||
};
|
||||
|
||||
console.log('Usando margens:', margens);
|
||||
|
||||
// Parâmetros de página
|
||||
const PAGE_WIDTH = 595.28; // A4
|
||||
const PAGE_HEIGHT = 841.89; // A4
|
||||
@@ -628,22 +632,36 @@ export class PdfService {
|
||||
let timbradoPageCount = 0;
|
||||
let timbradoPages: any[] = [];
|
||||
if (timbradoExists) {
|
||||
try {
|
||||
console.log('Carregando timbrado PDF...');
|
||||
const timbradoBytes = await fs.readFile(timbradoPath);
|
||||
timbradoPdf = await PDFDocument.load(timbradoBytes);
|
||||
timbradoPageCount = timbradoPdf.getPageCount();
|
||||
console.log(`Timbrado carregado com ${timbradoPageCount} páginas`);
|
||||
|
||||
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 pageIndex = 0;
|
||||
console.log(`Gerando PDF com ${paragrafos.length} parágrafos`);
|
||||
|
||||
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]);
|
||||
|
||||
if (timbradoPages.length > 0) {
|
||||
const embedded = timbradoPages[pageIndex % timbradoPages.length];
|
||||
console.log(`Adicionando timbrado na página ${pageIndex + 1}`);
|
||||
page.drawPage(embedded, {
|
||||
x: 0,
|
||||
y: 0,
|
||||
@@ -699,6 +717,7 @@ export class PdfService {
|
||||
);
|
||||
|
||||
if (yAfter === null) {
|
||||
console.log(`Página ${pageIndex + 1} está cheia, criando nova página`);
|
||||
pageIndex++;
|
||||
break;
|
||||
}
|
||||
@@ -709,6 +728,7 @@ export class PdfService {
|
||||
pageIndex++;
|
||||
}
|
||||
|
||||
console.log(`PDF gerado com ${pageIndex} páginas`);
|
||||
const pdfBytes = await pdfDoc.save();
|
||||
return Buffer.from(pdfBytes);
|
||||
} catch (error) {
|
||||
|
||||
Reference in new issue
Block a user