0

I am trying to add a component that allows the user to upload a PDF file they have saved on their device, and then extract the text from it into the app.

I have found many duplicates of this question, but none seem to have really been resolved.

There seems to have once been a library "react-native-pdf2json", but it doesn't seem to exist anymore.

I am using Expo, and this is all the code I have so far:

import React, { useState } from "react"
import { StyleSheet, Text, View, Button, TouchableOpacity } from "react-native"
import * as DocumentPicker from "expo-document-picker"

const UploadFile = () => {
  const pickDocument = async () => {
    try {
      const res = await DocumentPicker.getDocumentAsync({
        type: "*/*",
        copyToCacheDirectory: true,
        multiple: false,
      })

      console.log(res)
      var lastThree = res.name.substr(res.name.length - 3)
      if (res == undefined) {
        return
      } else if (
        lastThree == "pdf" ||
        lastThree == "epub" ||
        lastThree == "doc"
      ) {
        if (res.type == "success") {
          //do stuff
        }
      } else {
        alert("Please select supported file type")
      }
    } catch (error) {
      //alert(error)
    }
  }

  return (
    <View style={styles.background}>
      <Text style={styles.file}>Upload PDF, EPUB, or DOC file.</Text>
      <View style={styles.button}>
        <TouchableOpacity>
          <Button
            title="upload your file"
            color="black"
            onPress={pickDocument}
          />
        </TouchableOpacity>
      </View>
    </View>
  )
}

const styles = StyleSheet.create({
  background: {
    backgroundColor: "#5ff5fa",
  },
  file: {
    color: "black",
    marginHorizontal: 30,
  },
  button: {
    marginHorizontal: 60,
  },
})

export default UploadFile

0 Answers0