Skip to content

Instantly share code, notes, and snippets.

@renatoargh
Created May 4, 2015 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save renatoargh/2ea8d7c60df1a78e135f to your computer and use it in GitHub Desktop.
Save renatoargh/2ea8d7c60df1a78e135f to your computer and use it in GitHub Desktop.
// http://www.highcharts.com/docs/export-module/render-charts-serverside
'use strict';
var PdfPrinter = require('pdfmake'),
printer,
moment = require('moment-timezone'),
path = require('path'),
fs = require('fs');
function resolvePath(relative) {
return path.join(__dirname, relative);
}
var margemEsquerda = 40,
margemDeCima = 20,
margemDireita = 40,
margemDeBaixo = 40,
fonts = {
times: {
normal: resolvePath('./fonts/times-new-roman.ttf'),
bold: resolvePath('./fonts/times-new-roman-bold.ttf'),
italics: resolvePath('./fonts/times-new-italic.ttf'),
bolditalics: resolvePath('./fonts/times-new-bold-italic.ttf')
}
},
estilos = {
cabecalho: {
fontSize: 18,
bold: true,
font: 'times'
},
subCabecalho: {
fontSize: 12,
bold: true,
margin: [0, 0, 0, 10],
color: 'gray'
},
pequeno: {
fontSize: 8,
bold: true,
margin: [ margemEsquerda, 0, 0, 0 ]
},
aindaMenor: {
fontSize: 7,
bold: true,
margin: [ margemEsquerda, 0, 0, 0 ]
},
tabela: {
margin: [0, 0, 0, 5]
},
cabecalhoDaTabela: {
bold: true,
fontSize: 10,
margin: [0, 0, 0, 0]
},
textoDaTabela: {
fontSize: 8,
margin: [0, 0, 0, 0]
},
defaultStyle: {
font: 'times'
}
},
conteudo = [],
header = function(paginaAtual, totalDePaginas) {
};
printer = new PdfPrinter(fonts);
module.exports = function(parametros, registros, callback) {
var empresa = parametros.empresa,
repositorioDeEmpresa = parametros.repositorioDeEmpresa,
usuario = parametros.usuario,
pkg = parametros.pkg,
agora = moment();
function gerarRelatorio() {
var conteudo = [
{ text: empresa.nome, style: 'cabecalho' },
{ text: parametros.nome, style: 'subCabecalho' },
];
parametros.colunas = parametros.colunas || [];
registros = registros || [];
if(parametros.colunasVisiveis && parametros.colunasVisiveis.length) {
parametros.colunas = parametros.colunas.filter(function(coluna) {
return parametros.colunasVisiveis.indexOf(coluna.propriedade) > -1;
});
}
var tabela = {
headerRows: 1,
keepWithHeaderRows: 1,
style: 'tabela',
widths: parametros.colunas.map(function(coluna) {
var largura;
if(coluna.largura) {
largura = parseFloat(coluna.largura);
}
if(isNaN(largura)) {
largura = '*';
}
if(coluna.largura === 'auto') {
largura = 'auto';
}
return largura;
}),
body: [
parametros.colunas.map(function(coluna) {
return {
text: coluna.titulo,
style: 'cabecalhoDaTabela'
};
})
]
}
if(parametros.contador) {
tabela.widths.unshift('auto');
tabela.body[0].unshift({
text: '#',
style: 'cabecalhoDaTabela'
});
}
conteudo.push({ table: tabela });
registros.forEach(function(registro, index) {
var linha = [];
if(parametros.contador) {
linha.push({
style: 'textoDaTabela',
text: (index + 1).toString()
});
}
parametros.colunas.forEach(function(coluna) {
var texto = registro[coluna.propriedade];
if(coluna.render) {
texto = coluna.render(texto, registro);
}
texto = texto || '';
linha.push({
style: 'textoDaTabela',
text: texto.toString ? texto.toString() : texto
});
});
tabela.body.push(linha);
});
var peloMenosUmRodape = false,
textoPadraoDoRodape = '',
rodape = [];
if(parametros.contador) {
rodape.push({
style: 'textoDaTabela',
text: textoPadraoDoRodape
});
}
parametros.colunas.forEach(function(coluna) {
var texto = textoPadraoDoRodape;
if(coluna.rodape) {
peloMenosUmRodape = true;
texto = coluna.rodape(registros);
}
texto = texto || textoPadraoDoRodape;
rodape.push({
style: 'textoDaTabela',
text: texto.toString ? texto.toString() : texto
});
});
if(peloMenosUmRodape) {
tabela.body.push(rodape);
}
var documento = {
pageSize: parametros.tamanhoDaPagina || 'A4',
pageOrientation: {
'retrato': 'portrait',
'paisagem': 'landscape'
}[parametros.orientacao || 'retrato'],
pageMargins: [ margemEsquerda, margemDeCima, margemDireita, margemDeBaixo ],
styles: estilos,
header: header,
content: conteudo,
footer: function(paginaAtual, totalDePaginas) {
return [{
text: '\n' + [
empresa.nome,
parametros.nome
].join(' - ') + '\n',
style: 'pequeno'
}, {
text: [
'Impresso por',
usuario.nome,
'em',
agora.format('DD/MM/YYYY [às] HH:mm'),
'- Página ' + paginaAtual + ' de ' + totalDePaginas,
'(' + registros.length + ' registros)\n'
].join(' '),
style: 'pequeno'
}, {
text: 'Gammasoft Desenvolvimento de Software Ltda - GammaERP v' + pkg.version,
style: 'aindaMenor',
color: 'gray'
}];
}
};
callback && callback(null, printer.createPdfKitDocument(documento));
}
if(repositorioDeEmpresa) {
repositorioDeEmpresa.carregar(function(err, _empresa) {
if(err) {
return callback(err);
}
empresa = _empresa;
gerarRelatorio();
});
} else {
gerarRelatorio();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment