Skip to content

Instantly share code, notes, and snippets.

@ahlusar1989
Last active May 15, 2018 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahlusar1989/1347d2e6367375805b78e0f27d6ca4a3 to your computer and use it in GitHub Desktop.
Save ahlusar1989/1347d2e6367375805b78e0f27d6ca4a3 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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