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 pdfDoc: PDFDocument;
let timbradoPdf: PDFDocument | null = null; let timbradoPdf: PDFDocument | null = null;
let timbradoPageCount = 0; let timbradoPageCount = 0;
let timbradoPages: any[] = [];
if (timbradoExists) { if (timbradoExists) {
const timbradoBytes = await fs.readFile(timbradoPath); const timbradoBytes = await fs.readFile(timbradoPath);
timbradoPdf = await PDFDocument.load(timbradoBytes); timbradoPdf = await PDFDocument.load(timbradoBytes);
timbradoPageCount = timbradoPdf.getPageCount(); timbradoPageCount = timbradoPdf.getPageCount();
pdfDoc = await PDFDocument.create(); 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 { } else {
pdfDoc = await PDFDocument.create(); pdfDoc = await PDFDocument.create();
} }
@@ -303,27 +310,19 @@ export class PdfService {
let currentPar = 0; let currentPar = 0;
let currentLine = 0; let currentLine = 0;
let pageIndex = 0; let pageIndex = 0;
const pages: any[] = [];
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 = pdfDoc.addPage([PAGE_WIDTH, PAGE_HEIGHT]);
if (timbradoPdf && timbradoPageCount > 0) { // Desenhar o timbrado como fundo (usando embeddedPage)
// Copiar a página do timbrado para o novo PDF if (timbradoPages.length > 0) {
const [timbradoPage] = await pdfDoc.copyPages( const embedded = timbradoPages[pageIndex % timbradoPages.length];
timbradoPdf, page.drawPage(embedded, {
[pageIndex % timbradoPageCount]
);
page = pdfDoc.addPage([PAGE_WIDTH, PAGE_HEIGHT]);
// Desenhar o timbrado como fundo
page.drawPage(timbradoPage, {
x: 0, x: 0,
y: 0, y: 0,
width: PAGE_WIDTH, width: PAGE_WIDTH,
height: PAGE_HEIGHT height: PAGE_HEIGHT
}); });
} else {
page = pdfDoc.addPage([PAGE_WIDTH, PAGE_HEIGHT]);
} }
let y = PAGE_HEIGHT - margens.top; let y = PAGE_HEIGHT - margens.top;
// Escrever texto na página // Escrever texto na página
@@ -333,7 +332,6 @@ export class PdfService {
if (currentLine > 0) lineIdx = currentLine; if (currentLine > 0) lineIdx = currentLine;
for (; lineIdx < lines.length; lineIdx++) { for (; lineIdx < lines.length; lineIdx++) {
if (y < margens.bottom + LINE_HEIGHT) { if (y < margens.bottom + LINE_HEIGHT) {
// Próxima página
currentLine = lineIdx; currentLine = lineIdx;
break; break;
} }
@@ -347,13 +345,11 @@ export class PdfService {
currentLine = 0; currentLine = 0;
} }
if (lineIdx < lines.length) { if (lineIdx < lines.length) {
// Não coube tudo, continuar na próxima página
break; break;
} }
currentPar++; currentPar++;
} }
pageIndex++; pageIndex++;
pages.push(page);
} }
const pdfBytes = await pdfDoc.save(); const pdfBytes = await pdfDoc.save();