使用pdf2json将pdf解析为json时未定义

问题描述 投票:0回答:1

我是Node JS的新手,我想将pdf数据解析为json。我将pdf文件保存在公共目录中,但获得的值未定义你们能帮忙吗?

const app = express();
const bodyParser = require('body-parser');
const path = require('path');
const fs = require("fs");
const PDFParser = require("pdf2json");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true
}));

app.use(express.static(__dirname + "/public"));
app.get("/", function(req, res) {

    let pdfParser = new PDFParser(this,1);

    pdfParser.on("pdfParser_dataError", errData => console.error(errData.parserError) );
    pdfParser.on("pdfParser_dataReady", pdfData => {
        fs.writeFile("sample.json", JSON.stringify(pdfData));
        res.send(JSON.stringify(pdfData));
    });

    pdfParser.loadPDF('/public/sample.pdf');
});
app.listen(3000)

node.js
1个回答
0
投票

尝试这样操作,我认为找不到适当的PDF文件。

PDFParser.pdf2json('./public/sample.pdf', function (error, pdf) {
    if(error != null){
        console.log(error);
    }else{
        console.log(JSON.stringify(pdf));
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.