my goal is to get a certain string after a predefined text. In this case i would like to read the following value:
I found out this is possible using regex, therefore i tried this:
const fs = require("fs");
const PDFParser = require("pdf2json");
// Get all the filenames from the patients folder
const files = fs.readdirSync("templates");
// All of the parse patients
let patients = [];
// Make a IIFE so we can run asynchronous code
(async () => {
// Await all of the patients to be passed
// For each file in the patients folder
await Promise.all(files.map(async (file) => {
// Set up the pdf parser
let pdfParser = new PDFParser(this, 1);
// Load the pdf document
pdfParser.loadPDF(`templates/${file}`);
// Parsed the patient
let patient = await new Promise(async (resolve, reject) => {
// On data ready
pdfParser.on("pdfParser_dataReady", (pdfData) => {
// The raw PDF data in text form
const raw = pdfParser.getRawTextContent().replace(/\r\n/g, " ");
// Return the parsed data
resolve({
gesamtbetrag: /Amount\s(:*?)--/i.exec(raw)[1].trim()
});
});
});
// Add the patient to the patients array
patients.push(patient);
}));
// Save the extracted information to a json file
fs.writeFileSync("patients.json", JSON.stringify(patients));
})();
I'm getting the error that my array is at position 1 null:
Cannot read property '1' of null
Thanks