salix/services/print/application/route/manuscript.js

143 lines
4.4 KiB
JavaScript
Raw Normal View History

2017-12-04 07:17:29 +00:00
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);
};
2017-12-04 07:17:29 +00:00
// 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) => {
2017-12-04 07:17:29 +00:00
if (error)
return response.status(400).json({message: error.message});
pdf.create(result.body, config.pdf).toStream(function(error, stream) {
2017-12-04 07:17:29 +00:00
if (error)
throw Error(error);
response.setHeader('Content-Disposition', 'attachment; filename="sepa-core.pdf"');
response.setHeader('Content-type', 'application/pdf');
2017-12-04 07:17:29 +00:00
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) => {
2017-12-04 07:17:29 +00:00
if (error)
return response.status(400).json({message: error.message});
response.send(result.body);
2017-12-04 07:17:29 +00:00
});
});
// 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"');
2017-12-04 07:17:29 +00:00
response.setHeader('Content-type', 'application/pdf');
stream.pipe(response);
});
2017-12-04 07:17:29 +00:00
});
});
// 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
};
2017-12-04 07:17:29 +00:00
template.get('letter-debtor', params, (error, result) => {
2017-12-04 07:17:29 +00:00
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);
});
});
});
2017-12-04 07:17:29 +00:00
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();
});
});
});
*/