• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by ibrahimAlfors · Jun 09, 2014 at 06:11 PM · androidioswwwpdf

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

Comment

People who like this

0 Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Giantbean · Jun 10, 2014 at 04:42 PM 0
Share

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 streaming 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 streaming 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.PlayFullScreenMovie ("filename", Color.black, FullScreenMovieControlMode.CancelOnInput);

but PDF files wont load using Application.OpenURL(Jar path...

Has anyone been able to load a PDF on an Android?

avatar image Giantbean · Jun 11, 2014 at 05:34 PM 0
Share

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.

avatar image ibrahimAlfors · Jun 14, 2014 at 10:21 AM 0
Share

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

2 Replies

  • Sort: 
avatar image

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

Comment
nicloay
itsharshdeep

People who like this

2 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nicloay · Dec 21, 2015 at 07:07 AM 0
Share

thanks for code. works for me as well.

avatar image

Answer by SlavaObninsk · Jan 31, 2015 at 05:16 PM

PDFReader available on the AssetStore http://u3d.as/7AX

Comment
danivdwerf

People who like this

-1 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to upload/download video from/to mobile 1 Answer

WWW http request loads way to long on IOS 0 Answers

unity3d www download of large texture mobile 0 Answers

IOS http request loads slower at the first call of the scene 0 Answers

Build stripping and scenes in Asset Bundles 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges