微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

nodejs pdf解析在特定字符串后获取值

如何解决nodejs pdf解析在特定字符串后获取值

我的目标是在预定义文本之后获取某个字符串。在这种情况下,我想读取以下值:

required value

我发现这可以使用正则表达式,因此我尝试了这个:

 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));

 })();

我收到错误,我的数组在位置 1 为空:

无法读取 null 的属性“1”

谢谢

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。