cnjon / react-native-pdf-view

React Native PDF View
MIT License
404 stars 167 forks source link

Absolute Path #10

Closed bomalley closed 8 years ago

bomalley commented 8 years ago

Can you please show an example that refers to a PDF in your project bundle? I'm stuck on the 'absolute path' for the src attribute.

if I have this layout, what should be in my src attribute?

/index.android.js /My.pdf

Thanks.

bomalley commented 8 years ago

Sorry to be so persistent, but I'm really struggling here. Can anyone help?

Thanks in advance!

sibelius commented 8 years ago

You should try to get the bundle path

Take a look at this package https://github.com/johanneslumpe/react-native-fs

RNFS.MainBundlePath + '/My.pdf'

bomalley commented 8 years ago

That's very helpful, thank you @sibeliusseraphini.

Unfortunately that only seems to work if the PDF is in the files folder. If it's bundled in your APK, I can't see any way to get at it. Am I just missing something obvious?

sibelius commented 8 years ago

@bomalley how do you get the path information using Object-C/Swift on iOS, and in Java on Android?

bomalley commented 8 years ago

@sibeliusseraphini I'm trying to use the react-native-fs you recommended. It will allow me to get the data folder, but not the bundle/package folder, which is the problem.

sibelius commented 8 years ago

have you tried MainBundlePath? What is the result of RNFS.readDir?

In iOS you can use this to get the bundlePath:

NSString *path = [[NSBundle mainBundle] pathForResource:@"SomeFile" ofType:@"jpeg"];
bomalley commented 8 years ago

Okay, it took A LOT of tinkering, but I've finally figured out how to display a PDF that is pre-bundled in your Android APK. I documented the process on my website: How to display a PDF in React Native

In a nutshell:

  1. Copy the PDF from your APK into the files folder on the Android device
  2. Use react-native-fs to get an absolute URL to the PDF.
  3. Use react-native-pdf-view to show the PDF.
sibelius commented 8 years ago

@bomalley for now your hack works fine, but I think react-native-fs should handle this problem.

Try to submit a PR there using the code from your article :+1:

bomalley commented 8 years ago

@sibeliusseraphini Will do, though probably not until this weekend.