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.