How can I generate 20 txt relative to the pages of a pdf?
var fs = require('fs');
var pdfUtil = require('pdf-to-text');
var pdf_path = "/home/kevin/Downloads/principito.pdf";
var i=0;
for(i=0;i<=10;i++){
var option = {from:i, to:i};
var doc='./file'+i+'.txt';
//Omit option to extract all text from the pdf file
pdfUtil.pdfToText(pdf_path,option, function(err, data) {
fs.writeFile(doc, data, function(err) {
if( err ){
console.log( err );
}
else{
console.log(doc);
}
});
});
This is my code, but it generates the following:
./file20.txt
./file20.txt
./file20.txt
./file20.txt
./file20.txt
./file20.txt
./file20.txt
./file20.txt
./file20.txt
./file20.txt
./file20.txt
./file20.txt
./file20.txt
./file20.txt
./file20.txt
./file20.txt
./file20.txt
./file20.txt
./file20.txt
./file20.txt
./file20.txt
everything is concatenated, I guess it's because of the asynchronous behavior of nodejs , a handy.
The problem is that you are generating a new function inside your loop every time you call the function
pdfToText
.Each of these functions refer to the same variable
i
, as the code is not executed immediately by the time it is required to calculate the value of the variabledoc
, the loopfor
has already finished its execution and the value ofi
is 20The result is that all functions write to the same file.
This is a classic JavaScript problem that can be solved with a
IIFE
:An IIFE is a function that is declared and called immediately, we pass the variable as a parameter
doc
so that a copy is generated, in this way each function will have the correct value of the counteri
.This concept is also known as closure .
In ES6 the problem is more easily solved by using the keyword
let
to declare the variabledoc
.let
allows you to declare variables whose scope is the same block where they were declared.