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
|
// Carregar o timbrado (se houver) e criar o PDF final
|
||||||
let pdfDoc: PDFDocument;
|
let pdfDoc: PDFDocument;
|
||||||
let timbradoPages: any[] = [];
|
let timbradoPdf: PDFDocument | null = null;
|
||||||
|
let timbradoPageCount = 0;
|
||||||
if (timbradoExists) {
|
if (timbradoExists) {
|
||||||
const timbradoBytes = await fs.readFile(timbradoPath);
|
const timbradoBytes = await fs.readFile(timbradoPath);
|
||||||
const timbradoPdf = await PDFDocument.load(timbradoBytes);
|
timbradoPdf = await PDFDocument.load(timbradoBytes);
|
||||||
|
timbradoPageCount = timbradoPdf.getPageCount();
|
||||||
pdfDoc = await PDFDocument.create();
|
pdfDoc = await PDFDocument.create();
|
||||||
timbradoPages = await pdfDoc.copyPages(timbradoPdf, Array.from({length: timbradoPdf.getPageCount()}, (_, i) => i));
|
|
||||||
} else {
|
} else {
|
||||||
pdfDoc = await PDFDocument.create();
|
pdfDoc = await PDFDocument.create();
|
||||||
}
|
}
|
||||||
@@ -307,11 +308,20 @@ export class PdfService {
|
|||||||
while (currentPar < paragrafos.length) {
|
while (currentPar < paragrafos.length) {
|
||||||
// Adicionar página do timbrado ou em branco
|
// Adicionar página do timbrado ou em branco
|
||||||
let page;
|
let page;
|
||||||
if (timbradoPages.length > 0 && pageIndex < timbradoPages.length) {
|
if (timbradoPdf && timbradoPageCount > 0) {
|
||||||
page = pdfDoc.addPage(timbradoPages[pageIndex]);
|
// Copiar a página do timbrado para o novo PDF
|
||||||
} else if (timbradoPages.length > 0) {
|
const [timbradoPage] = await pdfDoc.copyPages(
|
||||||
// Se o timbrado tem só 1 página, repetir
|
timbradoPdf,
|
||||||
page = pdfDoc.addPage(timbradoPages[0]);
|
[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 {
|
} else {
|
||||||
page = pdfDoc.addPage([PAGE_WIDTH, PAGE_HEIGHT]);
|
page = pdfDoc.addPage([PAGE_WIDTH, PAGE_HEIGHT]);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user