Skip to content

Instantly share code, notes, and snippets.

@khriztianmoreno
Last active June 6, 2016 15:49
Show Gist options
  • Save khriztianmoreno/cda41564adffe2e01756 to your computer and use it in GitHub Desktop.
Save khriztianmoreno/cda41564adffe2e01756 to your computer and use it in GitHub Desktop.
Export to PDF, MEAN Stack
$http({
url: '/api/v1/reports/age-gender',
method: 'GET',
responseType: 'arraybuffer'
})
.success(function (data, status) {
var file = new Blob([data], {
type: 'application/pdf'
});
var fileURL = URL.createObjectURL(file);
window.open(fileURL);
})
.error(function (data, status) {
console.log('data', data);
console.log('status', status);
});
'use strict';
var config = require('../../config/environment');
var wkhtmltopdf = require('wkhtmltopdf');
var fs = require('fs');
var layoutHtml = fs.readFileSync(config.root + '/server/views/report.ejs', 'utf8');
var ejs = require('ejs');
var eventService = require('../event/event.service');
exports.ageGender = function (req, res) {
var html = '';
var dataTemplate = {
header: ['Rango Edad', 'Hombre', 'Mujer', 'Total', '% Participación'],
values: [
['<10', '5', '5', '10', '10'],
['11 - 20', '10', '10', '20', '20'],
['21 - 30', '25', '25', '50', '50'],
['31 - 40', '5', '5', '10', '10'],
['>41', '5', '5', '10', '10']
]
}
html = ejs.render(layoutHtml, dataTemplate);
var filename = "ageGender.pdf";
var fileStream = wkhtmltopdf(html, {
pageSize: 'letter',
orientation: 'landscape',
output: filename,
footerRight: '[page]/[toPage]'
});
fileStream.on('finish', function () {
fs.readFile(filename, function (err, data) {
res.writeHead(200, {
'Content-Type': 'application/pdf',
'Content-Length': data.size,
'Content-Disposition': 'attachment; filename="ageGender.pdf"'
});
res.end(data, 'binary');
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment