tenta fix pdfservice.js

This commit is contained in:
rodrigo committed 2025-04-21 19:36:52 -03:00
1 parent a202bdc445
commit 926a2934e4
1 file changed
+12 -16
+12 -16
View File
@@ -287,11 +287,18 @@ export class PdfService {
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();
}
@@ -303,27 +310,19 @@ export class PdfService {
let currentPar = 0;
let currentLine = 0;
let pageIndex = 0;
const pages: any[] = [];
while (currentPar < paragrafos.length) {
// Adicionar página do timbrado ou em branco
let page;
if (timbradoPdf && timbradoPageCount > 0) {
// Copiar a página do timbrado para o novo PDF
const [timbradoPage] = await pdfDoc.copyPages(
timbradoPdf,
[pageIndex % timbradoPageCount]
);
page = pdfDoc.addPage([PAGE_WIDTH, PAGE_HEIGHT]);
// Desenhar o timbrado como fundo
page.drawPage(timbradoPage, {
let page = pdfDoc.addPage([PAGE_WIDTH, PAGE_HEIGHT]);
// Desenhar o timbrado como fundo (usando embeddedPage)
if (timbradoPages.length > 0) {
const embedded = timbradoPages[pageIndex % timbradoPages.length];
page.drawPage(embedded, {
x: 0,
y: 0,
width: PAGE_WIDTH,
height: PAGE_HEIGHT
});
} else {
page = pdfDoc.addPage([PAGE_WIDTH, PAGE_HEIGHT]);
}
let y = PAGE_HEIGHT - margens.top;
// Escrever texto na página
@@ -333,7 +332,6 @@ export class PdfService {
if (currentLine > 0) lineIdx = currentLine;
for (; lineIdx < lines.length; lineIdx++) {
if (y < margens.bottom + LINE_HEIGHT) {
// Próxima página
currentLine = lineIdx;
break;
}
@@ -347,13 +345,11 @@ export class PdfService {
currentLine = 0;
}
if (lineIdx < lines.length) {
// Não coube tudo, continuar na próxima página
break;
}
currentPar++;
}
pageIndex++;
pages.push(page);
}
const pdfBytes = await pdfDoc.save();