最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Nodejs:使用同步回调循环文件和解析PDF

IT培训 admin 7浏览 0评论

Nodejs:使用同步回调循环文件和解析PDF

我是节点的新手,所以我正在努力解决它的异步性质。

我正在尝试创建一个脚本,它将解析目录中的pdfs并以另一个目录中的txt格式输出它们。

为此,我使用fs和pdf2json npm包。我将parseData函数作为loopingFiles函数中的回调传递。我遇到的唯一问题是节点的异步性质。

它将同时循环遍历所有文件,然后输出在最后一个文件索引中混乱。

我想同步处理这个,这样它一旦数据完成解析就会等待写入txt,然后再循环。

我已经尝试过承诺,但无济于事。任何帮助将非常感激!

var fs = require('fs'),
PDFParser = require("pdf2json");

let pdfParser = new PDFParser(this,1);

var parseData = function(pdf, index) {
  txtFile = "/Users/janet/node/pdf/Destination/".concat(index.toString().concat(".txt"))
  pdfFile = "/Users/janet/node/pdf/Source/".concat(pdf);
  pdfParser.loadPDF(pdfFile);
    // Parsing the pdf file in question
    pdfParser.on("pdfParser_dataError", errData => console.error(errData.parserError) );
    pdfParser.on("pdfParser_dataReady", pdfData => {
      fs.writeFile(txtFile, pdfParser.getRawTextContent());
});
  };



var loopingFiles = function(callback) {
  fs.readdir("/Users/janet/node/pdf/Source", function (err, files) {
  if (err) {
    console.log(err);
  } else {
    files.forEach( function(file, index) {
      callback(file, index);
    });
  };
  });
};


loopingFiles(parseData);
回答如下:

像这样的东西?

var fs = require("fs"),
  PDFParser = require("pdf2json");

let pdfParser = new PDFParser(this, 1);

var parseData = function(pdfs, index = 0) {
  // finished
  if (index >= pdfs.length) return;
  let pdf = pdfs[index];
  txtFile = "/Users/janet/node/pdf/Destination/".concat(
    index.toString().concat(".txt")
  );
  pdfFile = "/Users/janet/node/pdf/Source/".concat(pdf);
  // Parsing the pdf file in question
  pdfParser.on("pdfParser_dataError", errData => {
    console.error(errData.parserError)
    // not sure if you want to call this here to keep going or not
    parseData(pdfs, index + 1);
  });
  pdfParser.on("pdfParser_dataReady", pdfData => {
    fs.writeFile(txtFile, pdfParser.getRawTextContent(), function() {
      // when we're all done, call this function again, with the index of the next pdf
      parseData(pdfs, index + 1);
    });
  });
  pdfParser.loadPDF(pdfFile);
};

var loopingFiles = function(callback) {
  fs.readdir("/Users/janet/node/pdf/Source", function(err, files) {
    if (err) {
      console.log(err);
    } else {
      callback(files, 0);
    }
  });
};

loopingFiles(parseData);

主要区别在于将整个pdf数组传递给带索引的函数,并且只有在当前函数完成后才使用递增的索引再次调用该函数

Nodejs:使用同步回调循环文件和解析PDF

我是节点的新手,所以我正在努力解决它的异步性质。

我正在尝试创建一个脚本,它将解析目录中的pdfs并以另一个目录中的txt格式输出它们。

为此,我使用fs和pdf2json npm包。我将parseData函数作为loopingFiles函数中的回调传递。我遇到的唯一问题是节点的异步性质。

它将同时循环遍历所有文件,然后输出在最后一个文件索引中混乱。

我想同步处理这个,这样它一旦数据完成解析就会等待写入txt,然后再循环。

我已经尝试过承诺,但无济于事。任何帮助将非常感激!

var fs = require('fs'),
PDFParser = require("pdf2json");

let pdfParser = new PDFParser(this,1);

var parseData = function(pdf, index) {
  txtFile = "/Users/janet/node/pdf/Destination/".concat(index.toString().concat(".txt"))
  pdfFile = "/Users/janet/node/pdf/Source/".concat(pdf);
  pdfParser.loadPDF(pdfFile);
    // Parsing the pdf file in question
    pdfParser.on("pdfParser_dataError", errData => console.error(errData.parserError) );
    pdfParser.on("pdfParser_dataReady", pdfData => {
      fs.writeFile(txtFile, pdfParser.getRawTextContent());
});
  };



var loopingFiles = function(callback) {
  fs.readdir("/Users/janet/node/pdf/Source", function (err, files) {
  if (err) {
    console.log(err);
  } else {
    files.forEach( function(file, index) {
      callback(file, index);
    });
  };
  });
};


loopingFiles(parseData);
回答如下:

像这样的东西?

var fs = require("fs"),
  PDFParser = require("pdf2json");

let pdfParser = new PDFParser(this, 1);

var parseData = function(pdfs, index = 0) {
  // finished
  if (index >= pdfs.length) return;
  let pdf = pdfs[index];
  txtFile = "/Users/janet/node/pdf/Destination/".concat(
    index.toString().concat(".txt")
  );
  pdfFile = "/Users/janet/node/pdf/Source/".concat(pdf);
  // Parsing the pdf file in question
  pdfParser.on("pdfParser_dataError", errData => {
    console.error(errData.parserError)
    // not sure if you want to call this here to keep going or not
    parseData(pdfs, index + 1);
  });
  pdfParser.on("pdfParser_dataReady", pdfData => {
    fs.writeFile(txtFile, pdfParser.getRawTextContent(), function() {
      // when we're all done, call this function again, with the index of the next pdf
      parseData(pdfs, index + 1);
    });
  });
  pdfParser.loadPDF(pdfFile);
};

var loopingFiles = function(callback) {
  fs.readdir("/Users/janet/node/pdf/Source", function(err, files) {
    if (err) {
      console.log(err);
    } else {
      callback(files, 0);
    }
  });
};

loopingFiles(parseData);

主要区别在于将整个pdf数组传递给带索引的函数,并且只有在当前函数完成后才使用递增的索引再次调用该函数

发布评论

评论列表 (0)

  1. 暂无评论