- Home /
Open a PDF on Android and iOS
Okay this has been haunting me for a couple of days, I have this project one of its features is that it would have this button that would open a local pdf file for the user. However, the project is intended for Android and iOS.
What I achieved so far is locating the pdf file from the .apk on android (I haven't tested it on iOS yet). I used the WWW class to do so, and I checked for the size and it returned the true size of the PDF (Which proves that I have the correct file).
The problem is that I can only access the file through the WWW class (to get the size for example), How do I open it through www.assetBundle.mainAsset?
My thoughts were that I should first save the pdf file in a temporary location and use the OpenURL or maybe the process.start to open it. But I can't figure out how to do that either.
I have done tones of research and got this far and I can't find anything online that would help me to take this last step, so if anyone can help me with it, please tell me.
Thanks
EDIT: The code I used is
IEnumerator Start() {
if(Application.platform == RuntimePlatform.Android){
path =Application.streamingAssetsPath + "/The_100.pdf";
}else{
path = "file://"+Application.streamingAssetsPath + "/The_100.pdf";
}
WWW www = new WWW(path);
yield return www;
}
when I use www.size it returns the correct size of the pdf file
I would like to add to this conversation as I am fighting the same issue with loading a PDF on Android. The documentation on data paths does not mention Android and strea$$anonymous$$g assets gives a path that I have not been able to get working.
path = "jar:file://" + Application.dataPath + "!/assets/" + "filename";
Then they suggest the WWW load method in application strea$$anonymous$$g assets
It seems you have to use the jar path with the WWW but I'm not sure how. Another Question has some ideas but it is still unclear. Android can play video full screen with.
Handheld.PlayFullScreen$$anonymous$$ovie ("filename", Color.black, FullScreen$$anonymous$$ovieControl$$anonymous$$ode.CancelOnInput);
but PDF files wont load using Application.OpenURL(Jar path...
Has anyone been able to load a PDF on an Android?
I ended up faking it with NGUI plugin however you may also want to check out this thread or if the device will have a network conection you can count on you can still use
Application.OpenURL("web pdf address");
Good luck to you.
Thanks :) its a good idea but my problem is that the only way I can access this pdf file is through the www class and I don't know how to add a reference to it, like object pdfObject = (Object) www.assetBundle.mainAsset; I have tried that and it didn't work but you get the idea of what I want
Answer by ibrahimAlfors · Jun 14, 2014 at 02:49 PM
I have figured out how to save the file to the Application.persistentDataPath The code I used:
IEnumerator Start() {
if(Application.platform == RuntimePlatform.Android){
path =Application.streamingAssetsPath + "/The_100.pdf";
savePath = Application.persistentDataPath;
}else{
path = "file://"+Application.streamingAssetsPath + "/The_100.pdf";
savePath = "C:/Users/IBRAHI/Desktop/SaveTestUnity";
}
WWW www = new WWW(path);
yield return www;
error = www.error;
byte[] bytes = www.bytes;
result = "Done, bytes downloaded, File size : "+bytes.Length;
try{
File.WriteAllBytes(savePath+"/The_100.pdf", bytes);
error = "No Errors so far";
}catch(Exception ex){
error = ex.Message;
}
Application.OpenURL(savePath+"/The_100.pdf");
}
Thanks
Answer by SlavaObninsk · Jan 31, 2015 at 05:16 PM
PDFReader available on the AssetStore http://u3d.as/7AX