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); }); }); // Delivery note router.get('/delivery-note/:ticketId/preview', requestToken, function(request, response) { let params = { ticketId: request.params.ticketId, isPreview: true }; template.get('delivery-note', params, (error, result) => { if (error) return response.status(400).json(error); response.send(result.body); }); }); router.get('/delivery-note/:ticketId', requestToken, function(request, response, next) { let params = { ticketId: request.params.ticketId }; template.get('delivery-note', 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="deliveryNote.pdf"'); //response.setHeader('Content-type', 'application/pdf'); stream.pipe(response); }); }); }); 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(); }); }); }); */