mirror of
https://gitlab.com/wgp/dougal/software.git
synced 2025-12-06 07:37:08 +00:00
Add PDF output option for events log
This commit is contained in:
@@ -2,6 +2,7 @@ const json = require('./json');
|
||||
const geojson = require('./geojson');
|
||||
const seis = require('./seis');
|
||||
const html = require('./html');
|
||||
const pdf = require('./pdf');
|
||||
|
||||
module.exports = async function (req, res, next) {
|
||||
try {
|
||||
@@ -10,6 +11,7 @@ module.exports = async function (req, res, next) {
|
||||
"application/geo+json": geojson,
|
||||
"application/vnd.seis+json": seis,
|
||||
"text/html": html,
|
||||
"application/pdf": pdf
|
||||
};
|
||||
|
||||
const mimetype = (handlers[req.query.mime] && req.query.mime) || req.accepts(Object.keys(handlers));
|
||||
|
||||
42
lib/www/server/api/middleware/event/get/pdf.js
Normal file
42
lib/www/server/api/middleware/event/get/pdf.js
Normal file
@@ -0,0 +1,42 @@
|
||||
const fs = require('fs/promises');
|
||||
const Path = require('path');
|
||||
const crypto = require('crypto');
|
||||
const { event, sequence, configuration } = require('../../../../lib/db');
|
||||
const { transform } = require('../../../../lib/sse');
|
||||
const render = require('../../../../lib/render');
|
||||
const { url2pdf } = require('../../../../lib/selenium');
|
||||
|
||||
function tmpname (tmpdir="/dev/shm") {
|
||||
return Path.join(tmpdir, crypto.randomBytes(16).toString('hex')+".tmp");
|
||||
}
|
||||
|
||||
const pdf = async function (req, res, next) {
|
||||
const fname = tmpname();
|
||||
try {
|
||||
const query = req.query;
|
||||
query.sequence = req.params.sequence;
|
||||
const events = await event.list(req.params.project, query);
|
||||
const sequences = await sequence.list(req.params.project, query);
|
||||
const seis = transform(events, sequences, {projectId: req.params.project});
|
||||
const templates = await configuration.get(req.params.project, "sse/templates");
|
||||
const template = templates[0].template;
|
||||
|
||||
const html = await render(seis, template);
|
||||
|
||||
await fs.writeFile(fname, html);
|
||||
const pdf = Buffer.from(await url2pdf("file://"+fname), "base64");
|
||||
|
||||
res.status(200).send(pdf);
|
||||
next();
|
||||
} catch (err) {
|
||||
if (err.message.startsWith("template")) {
|
||||
next({message: err.message});
|
||||
} else {
|
||||
next(err);
|
||||
}
|
||||
} finally {
|
||||
await fs.unlink(fname);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = pdf;
|
||||
Reference in New Issue
Block a user