tenta fix pdfservice.js
This commit is contained in:
1 parent
6d487b5834
commit
a202bdc445
1 file changed
+18
-8
@@ -285,12 +285,13 @@ export class PdfService {
|
||||
|
||||
// Carregar o timbrado (se houver) e criar o PDF final
|
||||
let pdfDoc: PDFDocument;
|
||||
let timbradoPages: any[] = [];
|
||||
let timbradoPdf: PDFDocument | null = null;
|
||||
let timbradoPageCount = 0;
|
||||
if (timbradoExists) {
|
||||
const timbradoBytes = await fs.readFile(timbradoPath);
|
||||
const timbradoPdf = await PDFDocument.load(timbradoBytes);
|
||||
timbradoPdf = await PDFDocument.load(timbradoBytes);
|
||||
timbradoPageCount = timbradoPdf.getPageCount();
|
||||
pdfDoc = await PDFDocument.create();
|
||||
timbradoPages = await pdfDoc.copyPages(timbradoPdf, Array.from({length: timbradoPdf.getPageCount()}, (_, i) => i));
|
||||
} else {
|
||||
pdfDoc = await PDFDocument.create();
|
||||
}
|
||||
@@ -307,11 +308,20 @@ export class PdfService {
|
||||
while (currentPar < paragrafos.length) {
|
||||
// Adicionar página do timbrado ou em branco
|
||||
let page;
|
||||
if (timbradoPages.length > 0 && pageIndex < timbradoPages.length) {
|
||||
page = pdfDoc.addPage(timbradoPages[pageIndex]);
|
||||
} else if (timbradoPages.length > 0) {
|
||||
// Se o timbrado tem só 1 página, repetir
|
||||
page = pdfDoc.addPage(timbradoPages[0]);
|
||||
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, {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: PAGE_WIDTH,
|
||||
height: PAGE_HEIGHT
|
||||
});
|
||||
} else {
|
||||
page = pdfDoc.addPage([PAGE_WIDTH, PAGE_HEIGHT]);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user