0

I have this component that allows the user to pick a file from their device:

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 Something here
        }
      } 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: 145,
  },
  button: {
    marginHorizontal: 60,
  },
})

export default UploadFile

And can successfully use this to get the uri of a PDF, EPUB or DOC file, but I have no idea how to extract the text from these files. I tried something called react-native-pdf-extractor, but I couldn't get it to work. No idea what to try.

Note: I don't simply need to VIEW the pdf, I need the text from it so the app can style it a certain way.

Iva
  • 2,447
  • 1
  • 18
  • 28
  • If you're on iOS, the reason that library didn't work is likely that "Currently, this module is only compatible for Android devices" according to their documentation. If you *are* on Android, you might want to consider including the code you tried to get it to work. – jnpdx Oct 02 '22 at 22:27
  • @jnpdx I wasn't aware of that. I AM testing on Android right now, but I would like the solution to work on both platforms, so that isn't going to work anyway. =/ – DingleberrySmith Oct 02 '22 at 22:35
  • use `react-native-fs` for reading file, https://www.npmjs.com/package/react-native-fs – Kailash Oct 03 '22 at 04:49
  • @Kailash Thank you. Will that alone be enough to extract text? – DingleberrySmith Oct 03 '22 at 05:27

0 Answers0