60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import { Request, Response } from "express";
|
|
import { PdfExport } from "../../../helpers/pdfExport";
|
|
import DynamicQueryBuilder from "../../../helpers/dynamicQueryBuilder";
|
|
import QueryStoreService from "../../../service/configuration/queryStoreService";
|
|
import InternalException from "../../../exceptions/internalException";
|
|
|
|
/**
|
|
* @description print list by query and template
|
|
* @param req {Request} Express req object
|
|
* @param res {Response} Express res object
|
|
* @returns {Promise<*>}
|
|
*/
|
|
export async function printListByQueryAndTemplate(req: Request, res: Response): Promise<any> {
|
|
const title = req.body.title;
|
|
const queryStore = req.body.queryStore;
|
|
const headerId = req.body.headerId ?? null;
|
|
const bodyId = req.body.bodyId ?? null;
|
|
const footerId = req.body.footerId ?? null;
|
|
const headerHeight = req.body.headerHeight ?? null;
|
|
const footerHeight = req.body.footerHeight ?? null;
|
|
|
|
let query = queryStore;
|
|
if (queryStore != "member" && queryStore != "memberByRunningMembership") {
|
|
query = (await QueryStoreService.getById(queryStore)).query;
|
|
}
|
|
|
|
let data = await DynamicQueryBuilder.executeQuery({
|
|
query: query.startsWith("{") ? JSON.parse(query) : query,
|
|
noLimit: true,
|
|
});
|
|
|
|
if (data.stats == "error") {
|
|
throw new InternalException("Failed executing Query");
|
|
}
|
|
|
|
let pdf = await PdfExport.renderFile({
|
|
title: title,
|
|
template: "listprint",
|
|
saveToDisk: false,
|
|
data: {
|
|
today: new Date(),
|
|
list: data.rows,
|
|
},
|
|
customTemplate: {
|
|
headerId,
|
|
footerId,
|
|
bodyId,
|
|
headerHeight,
|
|
footerHeight,
|
|
},
|
|
});
|
|
|
|
let pdfbuffer = Buffer.from(pdf);
|
|
|
|
res.setHeader("Content-Type", "application/pdf");
|
|
res.setHeader("Content-Length", pdfbuffer.byteLength);
|
|
res.setHeader("Content-Disposition", "inline; filename=preview.pdf");
|
|
|
|
res.send(pdfbuffer);
|
|
}
|