manually parsing pdf in node js

Solutions on MaxInterview for manually parsing pdf in node js by the best coders in the world

showing results for - "manually parsing pdf in node js"
Jessica
25 Sep 2017
1/*
2    This code comes from Vincent Lab
3    And it has a video version linked here: https://www.youtube.com/watch?v=b2dLDqmYT4I
4*/
5
6// Import dependencies
7const fs = require("fs");
8const PDFParser = require("pdf2json");
9
10// Get all the filenames from the patients folder
11const files = fs.readdirSync("patients");
12
13// All of the parse patients
14let patients = [];
15
16// Make a IIFE so we can run asynchronous code
17(async () => {
18
19    // Await all of the patients to be passed
20    // For each file in the patients folder
21    await Promise.all(files.map(async (file) => {
22
23        // Set up the pdf parser
24        let pdfParser = new PDFParser(this, 1);
25
26        // Load the pdf document
27        pdfParser.loadPDF(`patients/${file}`);
28
29        // Parsed the patient
30        let patient = await new Promise(async (resolve, reject) => {
31
32            // On data ready
33            pdfParser.on("pdfParser_dataReady", (pdfData) => {
34
35                // The raw PDF data in text form
36                const raw = pdfParser.getRawTextContent().replace(/\r\n/g, " ");
37
38                // Return the parsed data
39                resolve({
40                    name: /Name\s(.*?)Address/i.exec(raw)[1].trim(),
41                    address: /Address\s(.*?)Phone/i.exec(raw)[1].trim(),
42                    phone: /Phone\s(.*?)Birthday/i.exec(raw)[1].trim(),
43                    birthday: /Birthday\s(.*?)Email\sAddress/i.exec(raw)[1].trim(),
44                    emailAddress: /Email\sAddress\s(.*?)Blood\stype/i.exec(raw)[1].trim(),
45                    bloodType: /Blood\stype\s(.*?)Height/i.exec(raw)[1].trim(),
46                    height: /Height\s(.*?)Weight/i.exec(raw)[1].trim(),
47                    weight: /Weight\s(.*?)--/i.exec(raw)[1].trim()
48                });
49
50            });
51
52        });
53
54        // Add the patient to the patients array
55        patients.push(patient);
56
57    }));
58
59    // Save the extracted information to a json file
60    fs.writeFileSync("patients.json", JSON.stringify(patients));
61
62})();