67 lines
2.2 KiB
JavaScript
67 lines
2.2 KiB
JavaScript
|
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');
|
||
|
|
||
|
// Sepa core
|
||
|
router.post('/sepa-core/:clientId', function(request, response, next) {
|
||
|
template.get('sepa-core', {clientId: request.params.clientId}, (error, result) => {
|
||
|
if (error)
|
||
|
return response.status(400).json({message: error.message});
|
||
|
|
||
|
pdf.create(result.body).toStream(function(error, stream) {
|
||
|
if (error)
|
||
|
throw Error(error);
|
||
|
|
||
|
stream.pipe(response);
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
|
// 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();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
*/
|
||
|
// Sepa core preview
|
||
|
router.get('/sepa-core/:clientId', function(request, response, next) {
|
||
|
template.get('sepa-core', {clientId: request.params.clientId}, (error, result) => {
|
||
|
if (error)
|
||
|
return response.status(400).json({message: error.message});
|
||
|
|
||
|
let options = config.pdf;
|
||
|
pdf.create(result.body, options).toStream(function(error, stream) {
|
||
|
if (error)
|
||
|
throw Error(error);
|
||
|
|
||
|
response.setHeader('Content-Disposition', 'inline; filename="sepa-core.pdf"');
|
||
|
response.setHeader('Content-type', 'application/pdf');
|
||
|
stream.pipe(response);
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
|
router.get('/sepa-core-view/:clientId', function(request, response, next) {
|
||
|
template.get('sepa-core', {clientId: request.params.clientId}, (error, result) => {
|
||
|
if (error)
|
||
|
return response.status(400).json({message: error.message});
|
||
|
|
||
|
response.send(result.body);
|
||
|
});
|
||
|
});
|
||
|
|
||
|
module.exports = router;
|