Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  3] [ 2]  / answers: 1 / hits: 7062  / 5 Years ago, sun, july 28, 2019, 12:00:00

I'm creating a React Application with NodeJS and it needs to get some text from a PDF that the user upload.



I already tried to use: pdf-parse, pdf2json, pdf.js and react-pdf-js. The file should be selected by the user, and all those libraries use a Path to acess the file. What should I do?
PS1: I'm using a input type='file' button to get the file.



The code must work both NodeJS and Web Browser


More From » node.js

 Answers
5

I'm answering my own question. First I create a regular html input.


<input type='file'/>

I'm using React, so I use onChange attribute in place of id.
So, when the user enters with the file, a function is activated and I use the following code to get the file:


const file = event.target.files[0];

file not has a path, which is used by PDF.JS to get the real file.
Then I use a FileReader to convert the file int a Array of bits (I guess):


const fileReader = new FileReader();

Then we set a function at fileReader.onload the function can be found here


fileReader.onload = function() {...}

Finally we do this:


fileReader.readAsArrayBuffer(file);

Important PS: pdf.pdfInfo must be replaced with pdf at new PDF.JS versions.


Thanks for helping.


Extra PS: To use pdfjsLib as PDFJS in React I did this in index.html file:


window.PDFJS = pdfjsLib

[#6760] Thursday, July 25, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kamronr

Total Points: 749
Total Questions: 110
Total Answers: 122

Location: Dominica
Member since Sat, Nov 5, 2022
2 Years ago
kamronr questions
Mon, Dec 21, 20, 00:00, 3 Years ago
Fri, Oct 16, 20, 00:00, 4 Years ago
Sat, Oct 3, 20, 00:00, 4 Years ago
Mon, Mar 18, 19, 00:00, 5 Years ago
;