Skip to content

Instantly share code, notes, and snippets.

@ahlusar1989 ahlusar1989/route.js
Last active May 15, 2018

Embed
What would you like to do?
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();
})
@ahlusar1989

This comment has been minimized.

Copy link
Owner Author

commented May 15, 2018

fetchReportTemplate.js

const fs = require("fs");
const path = require("path");


function fetchReportTemplate(templateName) {
    const file = path.join(__dirname, `${templateName}.html`);
    const content = fs.readFileSync(file, "utf8");
    return content;
}

module.exports = { fetchReportTemplate }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.