Last active
May 15, 2018 23:02
-
-
Save ahlusar1989/1347d2e6367375805b78e0f27d6ca4a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const jsreport = require("jsreport-core")(); | |
const { fetchReportTemplate } = require("../reports/report-template"); | |
const router = require('express').Router() | |
const fs = require('fs'); | |
const express = require('express'); | |
const path = require('path'); | |
const reportingApp = express(); | |
jsreport.use(require('jsreport-express')({ app: reportingApp })); | |
jsreport.use(require('jsreport-chrome-pdf')()); | |
jsreport.use(require('jsreport-handlebars')()); | |
jsreport.use(require('jsreport-templates')()); | |
jsreport.use(require('jsreport-assets')()); | |
let jsreportStarted = false; | |
const initReport = () => { | |
if (jsreportStarted) { | |
return Promise.resolve(jsreport); | |
} | |
console.log('initializing jsreport'); | |
jsreportStarted = true; | |
return jsreport.init(); | |
}; | |
router.get("/map-images", (req, res, next) => { | |
// readFiles(reqPath) | |
initReport().then(() => { | |
let assets = jsreport.documentStore.collection('assets').find(); | |
let resolved = assets.then((a) => a); | |
resolved = resolved.map(a => a.name).filter(a => /.png$/.test(a)) | |
return jsreport.render({ | |
template: { | |
content: fetchReportTemplate("map"), | |
engine: "handlebars", | |
recipe: "chrome-pdf" | |
}, | |
assets: resolved | |
}).then((out) => { | |
// const data = resp.content; | |
// // console.log(data.toString()) | |
res.writeHead(200, { | |
"Content-Type": "application/pdf", | |
"Content-disposition": "attachment;filename=" + "done", | |
"Content-Length": out.length | |
}); | |
// out.stream.pipe(res); | |
// res.end(new Buffer(data, "binary")); | |
// fs.writeFile('test.pdf', out.toString(), 'binary'); | |
res.send(new Buffer(data.toString(), 'binary')) | |
// res.write(data.toString(), 'binary'); | |
}); | |
}).catch(function (e) { | |
res.end(e.message) | |
}) | |
// next(); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fetchReportTemplate.js