fixing export dashboard to pdf

This commit is contained in:
Warunee Tamkoo 2025-02-27 09:18:43 +07:00
parent 6f8d1bff67
commit ab9b6e7bdc

View file

@ -36,16 +36,37 @@ async function main() {
const params = new URLSearchParams(req.query as Record<string, string>);
const newUrl = params.toString() ? `${id}?${params.toString()}` : id;
const APIAPI_DASHBOARD = `${process.env.API_DASHBOARD}/generate-pdf`;
const body = `${process.env.API_DASHBOARD}/d/${newUrl}`;
const API_DASHBOARD = `${process.env.VITE_API_REPORT_URL}/html`;
const url = `${process.env.API_DASHBOARD}/d/${newUrl}&kiosk`;
try {
// เรียก API generate-pdf
const apiResponse = await axios.post(APIAPI_DASHBOARD, { url: body });
const apiResponse = await axios.post(
API_DASHBOARD,
{
template: url,
reportName: "html-report",
htmlOption: {
querySelector: ".scrollbar-view",
},
},
{
headers: {
"Content-Type": "application/json",
accept: "application/pdf",
},
responseType: "arraybuffer",
},
);
// console.log("Response:", apiResponse.data.pdfUrl);
console.log("Response:", apiResponse.data);
res.redirect(apiResponse.data.pdfUrl);
const pdfBuffer = Buffer.from(apiResponse.data, "binary");
res.set({
"Content-Type": "application/pdf",
"Content-Disposition": "attachment; filename=dashboard.pdf",
});
res.send(pdfBuffer);
} catch (error) {
throw new Error("Failed to generate PDF: " + error);
}