1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
import type { NextApiRequest, NextApiResponse } from "next";
import type { File as FormidableFile } from "formidable";
import formidable from "formidable";
import fs from "fs/promises";
import { createReport } from "@/lib/pdftron/serverSDK/createReport";
export const config = {
api: {
bodyParser: false, // ✅ 이게 false면 안 됨!
},
};
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method !== "POST") {
return res.status(405).end();
}
try {
const form = formidable({ multiples: false });
form.parse(req, async (err, fields, files) => {
if (err) {
console.error(err);
return res.status(500).json({ error: "Error parsing form" });
}
try {
const fileName = fields?.customFileName?.[0] ?? "";
const reportDatas = JSON.parse(fields?.reportDatas?.[0] ?? "[]") as {
[key: string]: any;
}[];
const reportTempPath = fields?.reportTempPath?.[0] ?? "";
const reportCoverPage: FormidableFile | undefined = files?.file?.[0];
if (
!reportCoverPage ||
fileName.length === 0 ||
reportDatas.length === 0 ||
reportTempPath.length === 0
) {
return res.status(400).json({ error: "Invalid Report Data" });
}
const buffer = await fs.readFile(reportCoverPage.filepath);
const {
result,
buffer: pdfBuffer,
error,
} = await createReport(buffer, reportTempPath, reportDatas);
if (result && pdfBuffer) {
res.setHeader("Content-Type", "application/pdf");
res.setHeader(
"Content-Disposition",
`attachment; filename="${fileName}"`
);
return res.send(Buffer.from(pdfBuffer));
}
return res.status(200).json({
success: false,
message: "Report 생성에 실패하였습니다.",
error,
});
} catch (e) {
console.log(e);
return res.status(400).json({ error: "Invalid additionalData" });
}
});
} catch (err) {
return res.status(401).end();
}
}
|