tenta fix pdfservice.js

This commit is contained in:
rodrigo committed 2025-04-22 15:06:49 -03:00
1 parent e753ec8718
commit 5b25414b9c
1 file changed
+37 -37
+37 -37
View File
@@ -1,6 +1,11 @@
import puppeteer from 'puppeteer';
import { prisma } from '../prismaClient';
// Função utilitária para converter pixels em milímetros (1px ≈ 0.264583 mm)
function pxToMm(px: number): string {
return (px * 0.264583).toFixed(2) + 'mm';
}
export class PdfService {
private async getConfiguracao() {
try {
@@ -17,12 +22,12 @@ export class PdfService {
timbradoMarginBottom: true,
timbradoMarginLeft: true,
timbradoMarginRight: true,
cabecalhoHtml: true,
rodapeHtml: true,
criadoEm: true
}
});
console.log('Configuração encontrada:', config); // Debug
// Retornar configuração padrão se não existir
return {
cabecalhoHtml: config?.cabecalhoHtml || `
@@ -54,8 +59,7 @@ export class PdfService {
? `${process.env.FRONTEND_URL || 'http://localhost:5000'}${config.timbradoUrl}`
: '';
console.log('Gerando PDF com timbrado:', timbradoUrl);
// CSS para background em todas as páginas
return `
<!DOCTYPE html>
<html>
@@ -64,32 +68,39 @@ export class PdfService {
<link href="https://fonts.googleapis.com/css2?family=Fira+Sans:wght@400;500;600&display=swap" rel="stylesheet">
<style>
@page {
margin: 0;
margin-top: ${pxToMm(config.timbradoMarginTop)};
margin-bottom: ${pxToMm(config.timbradoMarginBottom)};
margin-left: ${pxToMm(config.timbradoMarginLeft)};
margin-right: ${pxToMm(config.timbradoMarginRight)};
size: A4;
}
body {
html, body {
height: 100%;
margin: 0;
padding: 0;
font-family: 'Fira Sans', Arial, sans-serif;
font-size: 11pt;
line-height: 1.5;
background: none;
}
body {
position: relative;
min-height: 100vh;
}
.page {
position: relative;
width: 21cm;
min-height: 29.7cm;
padding: ${config.timbradoMarginTop || 100}px ${config.timbradoMarginRight || 50}px ${config.timbradoMarginBottom || 50}px ${config.timbradoMarginLeft || 50}px;
/* Timbrado como background de todas as páginas */
body::before {
content: '';
position: fixed;
top: 0; left: 0; right: 0; bottom: 0;
width: 100vw;
height: 100vh;
z-index: 0;
background: ${timbradoUrl ? `url('${timbradoUrl}')` : 'none'};
background-size: cover;
background-position: center;
background-repeat: no-repeat;
box-sizing: border-box;
pointer-events: none;
}
.content {
position: relative;
z-index: 1;
@@ -97,10 +108,8 @@ export class PdfService {
</style>
</head>
<body>
<div class="page">
<div class="content">
${content}
</div>
<div class="content">
${content}
</div>
</body>
</html>
@@ -109,42 +118,33 @@ export class PdfService {
async gerarPDF(conteudo: string, escritorio: any) {
try {
const config = await prisma.configuracaoDocumento.findFirst({
orderBy: { criadoEm: 'desc' }
});
console.log('Configuração do PDF:', config);
const config = await this.getConfiguracao();
const html = this.gerarHTML(conteudo, config);
const browser = await puppeteer.launch({
headless: 'new',
args: ['--no-sandbox']
});
const page = await browser.newPage();
await page.setViewport({
width: 794, // A4 width in pixels (72 dpi)
height: 1123,
deviceScaleFactor: 2
});
await page.setContent(html, {
waitUntil: ['networkidle0', 'load'],
timeout: 30000
});
// Margens convertidas para mm
const margin = {
top: pxToMm(config.timbradoMarginTop),
bottom: pxToMm(config.timbradoMarginBottom),
left: pxToMm(config.timbradoMarginLeft),
right: pxToMm(config.timbradoMarginRight)
};
const pdfBuffer = await page.pdf({
format: 'A4',
printBackground: true,
margin: { top: '0', right: '0', bottom: '0', left: '0' },
margin,
preferCSSPageSize: true
});
await browser.close();
return Buffer.from(pdfBuffer);
} catch (error) {
console.error('Erro ao gerar PDF:', error);
throw new Error('Falha ao gerar PDF');