tenta fix pdfservice.js
This commit is contained in:
1 parent
5b25414b9c
commit
705ef97ff8
1 file changed
+18
-36
@@ -2,51 +2,37 @@ 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 {
|
||||
function pxToMm(px: number | null | undefined): string {
|
||||
if (!px) return '0mm';
|
||||
return (px * 0.264583).toFixed(2) + 'mm';
|
||||
}
|
||||
|
||||
export class PdfService {
|
||||
// Busca configuração do documento, usando apenas campos válidos do schema
|
||||
private async getConfiguracao() {
|
||||
try {
|
||||
// Buscar configuração ou usar valores padrão
|
||||
const config = await prisma.configuracaoDocumento.findFirst({
|
||||
orderBy: { criadoEm: 'desc' },
|
||||
select: {
|
||||
id: true,
|
||||
headerImageUrl: true,
|
||||
footerImageUrl: true,
|
||||
logoUrl: true,
|
||||
timbradoUrl: true,
|
||||
timbradoMarginTop: true,
|
||||
timbradoMarginBottom: true,
|
||||
timbradoMarginLeft: true,
|
||||
timbradoMarginRight: true,
|
||||
cabecalhoHtml: true,
|
||||
rodapeHtml: true,
|
||||
templatePadrao: true,
|
||||
margensTemplate: true,
|
||||
criadoEm: true
|
||||
}
|
||||
});
|
||||
|
||||
// Retornar configuração padrão se não existir
|
||||
// Retorna configuração padrão se não existir
|
||||
return {
|
||||
cabecalhoHtml: config?.cabecalhoHtml || `
|
||||
<div style="text-align: center; padding: 20px;">
|
||||
<h2 style="margin:0;font-size:14pt">{{escritorio.nome}}</h2>
|
||||
</div>
|
||||
`,
|
||||
rodapeHtml: config?.rodapeHtml || `
|
||||
<div style="text-align: center; font-size: 10pt;">
|
||||
{{escritorio.endereco}}<br>
|
||||
Tel: {{escritorio.telefone}} | Email: {{escritorio.email}}
|
||||
</div>
|
||||
`,
|
||||
logoUrl: config?.logoUrl || '',
|
||||
timbradoUrl: config?.timbradoUrl || '',
|
||||
timbradoMarginTop: config?.timbradoMarginTop || 100,
|
||||
timbradoMarginBottom: config?.timbradoMarginBottom || 50,
|
||||
timbradoMarginLeft: config?.timbradoMarginLeft || 50,
|
||||
timbradoMarginRight: config?.timbradoMarginRight || 50
|
||||
timbradoMarginTop: config?.timbradoMarginTop ?? 100,
|
||||
timbradoMarginBottom: config?.timbradoMarginBottom ?? 50,
|
||||
timbradoMarginLeft: config?.timbradoMarginLeft ?? 50,
|
||||
timbradoMarginRight: config?.timbradoMarginRight ?? 50,
|
||||
templatePadrao: config?.templatePadrao || '',
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Erro ao buscar configuração:', error);
|
||||
@@ -54,12 +40,11 @@ export class PdfService {
|
||||
}
|
||||
}
|
||||
|
||||
// Gera HTML com timbrado e margens
|
||||
private gerarHTML(content: string, config: any) {
|
||||
const timbradoUrl = config.timbradoUrl
|
||||
const timbradoUrl = config.timbradoUrl
|
||||
? `${process.env.FRONTEND_URL || 'http://localhost:5000'}${config.timbradoUrl}`
|
||||
: '';
|
||||
|
||||
// CSS para background em todas as páginas
|
||||
return `
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
@@ -116,21 +101,18 @@ export class PdfService {
|
||||
`;
|
||||
}
|
||||
|
||||
async gerarPDF(conteudo: string, escritorio: any) {
|
||||
// Gera PDF a partir do HTML, respeitando margens e timbrado
|
||||
async gerarPDF(conteudo: string) {
|
||||
try {
|
||||
const config = await this.getConfiguracao();
|
||||
const html = this.gerarHTML(conteudo, config);
|
||||
|
||||
const browser = await puppeteer.launch({
|
||||
headless: 'new',
|
||||
args: ['--no-sandbox']
|
||||
});
|
||||
// Usa templatePadrao se não houver conteúdo
|
||||
const html = this.gerarHTML(conteudo || config.templatePadrao, config);
|
||||
const browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox'] });
|
||||
const page = await browser.newPage();
|
||||
await page.setContent(html, {
|
||||
waitUntil: ['networkidle0', 'load'],
|
||||
timeout: 30000
|
||||
});
|
||||
// Margens convertidas para mm
|
||||
const margin = {
|
||||
top: pxToMm(config.timbradoMarginTop),
|
||||
bottom: pxToMm(config.timbradoMarginBottom),
|
||||
|
||||
Reference in new issue
Block a user