Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "pdf to text" converter using "pdf.js" and FileReader (works well)
- Loading branch information
Showing
8 changed files
with
63,802 additions
and
3 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
function getPageText(pageNum, PDFDocumentInstance) { | ||
// Return a Promise that is solved once the text of the page is retrieven | ||
return new Promise(function (resolve, reject) { | ||
PDFDocumentInstance.getPage(pageNum).then(function (pdfPage) { | ||
// The main trick to obtain the text of the PDF page, use the getTextContent method | ||
pdfPage.getTextContent().then(function (textContent) { | ||
var textItems = textContent.items; | ||
var finalString = ""; | ||
|
||
// Concatenate the string of the item to the final string | ||
for (var i = 0; i < textItems.length; i++) { | ||
var item = textItems[i]; | ||
|
||
finalString += item.str + " "; | ||
} | ||
|
||
// Solve promise with the text retrieven from the page | ||
resolve(finalString); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
function handlePdf(data){ | ||
|
||
document.getElementById('myText0').innerHTML = "* Début du fichier PDF*\n"; | ||
|
||
var fileList = this.files; | ||
var MyFile = data[0]; | ||
var fileReader = new FileReader(); | ||
|
||
fileReader.onload = function() { | ||
|
||
var typedarray = new Uint8Array(this.result); | ||
|
||
PDFJS.getDocument(typedarray).then(function(pdf) { | ||
|
||
var pdfDocument = pdf; | ||
var pagesPromises = []; | ||
|
||
for (var i = 0; i < pdf.pdfInfo.numPages; i++) { | ||
(function (pageNumber) { | ||
pagesPromises.push(getPageText(pageNumber, pdfDocument)); | ||
})(i + 1); | ||
} | ||
|
||
Promise.all(pagesPromises).then(function (pagesText) { | ||
document.getElementById('myText0').innerHTML += pagesText; //fill textarea with extracted text | ||
}); | ||
|
||
delete data; | ||
|
||
}, function (reason) { | ||
alert(reason); | ||
}); | ||
}; | ||
|
||
fileReader.readAsArrayBuffer(MyFile); | ||
} |
Oops, something went wrong.