Public
Express
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
// NOT FUNCTIONAL
// THE PDF LIBRARY PROBABLY WON'T WORK WITH THE CODE AS IT IS
export const parsePdfData = (req: express.Request, res: express.Response) => {
console.log("start parsePdfData");
// A visit from a web browser? Serve a HTML page with a form
if (req.method === "GET") {
return res.send(`
<!DOCTYPE html>
<html>
<head>
<title>Fillable Pdf Form</title>
</head>
<body>
<h1>Upload PDF Form</h1>
<form action="https://speepo-parsepdfdata.express.val.run/" method="post" enctype="multipart/form-data">
<input type="file" name="pdf" accept=".pdf" required>
<button type="submit">Upload</button>
</form>
</body>
</html>
`);
}
console.log("past html");
const handle = async () => {
console.log("in async iife");
const multer = await import("npm:multer");
const PDFParser = await import("npm:pdf-parse");
console.log("imports awaited");
const upload = multer();
console.log("multer()");
const onUpload = (req, res) => {
console.log("enter onUpload");
// Get the uploaded PDF file
const pdfFile = req.file.buffer;
console.log("got pdfFile: ", { pdfFile });
// Use the PDFParser library to parse the PDF data
PDFParser.loadPDF(pdfFile)
.then((pdfData) => {
console.log("got pdfData: ", { pdfData });
// Check if the PDF has fillable form data
const hasFormData = pdfData.formImage.Pages.some((page) =>
page.Fields.length > 0
);
if (!hasFormData) {
return res.status(400).send(
"The PDF does not have fillable form data.",
);
}
// Extract the form data and send it as the response
const formData = pdfData.formImage.Fields;
res.json(formData);
})
.catch((error) => {
console.error(error);
res.status(500).send("Failed to parse the PDF file.");
});
};
upload(req, res, onUpload);
};
console.log("about to handle");
handle();
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
speepo-parsepdfdata.express.val.run
October 23, 2023