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