var express = require('express'); var router = new express.Router(); var template = require('../template.js'); var config = require('../config.js'); var pdf = require('html-pdf'); var path = require('path'); var auth = require('../auth.js'); // Auth middleware var requestToken = function(request, response, next) { auth.init(request, response, next); }; // Sepa core router.get('/sepa-core/:companyId/:clientId', requestToken, function(request, response, next) { let params = { clientId: request.params.clientId, companyId: request.params.companyId }; template.get('sepa-core', params, (error, result) => { if (error) return response.status(400).json({message: error.message}); pdf.create(result.body, config.pdf).toStream(function(error, stream) { if (error) throw Error(error); response.setHeader('Content-Disposition', 'attachment; filename="sepa-core.pdf"'); response.setHeader('Content-type', 'application/pdf'); stream.pipe(response); }); }); }); // Sepa core html preview router.get('/sepa-core/:companyId/:clientId/preview', requestToken, function(request, response, next) { let params = { clientId: request.params.clientId, companyId: request.params.companyId, isPreview: true }; template.get('sepa-core', params, (error, result) => { if (error) return response.status(400).json({message: error.message}); response.send(result.body); }); }); // Debtor letter router.get('/letter-debtor/:companyId/:clientId', requestToken, function(request, response, next) { let params = { clientId: request.params.clientId, companyId: request.params.companyId }; template.get('letter-debtor', params, (error, result) => { if (error) return response.status(400).json({message: error.message}); pdf.create(result.body, config.pdf).toStream(function(error, stream) { if (error) throw Error(error); response.setHeader('Content-Disposition', 'attachment; filename="extracto.pdf"'); response.setHeader('Content-type', 'application/pdf'); stream.pipe(response); }); }); }); // Debtor letter html preview router.get('/letter-debtor/:companyId/:clientId/preview', requestToken, function(request, response, next) { let params = { clientId: request.params.clientId, companyId: request.params.companyId, isPreview: true }; template.get('letter-debtor', params, (error, result) => { if (error) return response.status(400).json({message: error.message}); response.send(result.body); }); }); module.exports = router; // store pdf /* router.post('/sepa-core/:clientId', function(request, response, next) { template.get('sepa-core', {recipient: request.params.clientId}, (error, result) => { if (error) return response.status(400).json({message: error.message}); pdf.create(result.body).toFile('./tmp/test.pdf', function(error, result) { if (error) return response.status(400).json({message: error}); console.log(result); return response.json(); }); }); }); */