Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

C# pdf generation and loading on iOS help

Discussion in 'Scripting' started by DottorFeelgood, Aug 19, 2015.

  1. DottorFeelgood

    DottorFeelgood

    Joined:
    Jan 5, 2015
    Posts:
    5
    Hello,

    I was trying to figure out how to generate and display a pdf in a Unity app.
    I would like the app, and pdf to be available for iOS, Android and web app.
    Following this tutorial I was able to generate a pdf file and store in the app main folder within the simulator.

    http://www.devindia.biz/unity-pdf-generation-with-sharppdf-plugin/

    Unfortunately when I try to open the pdf inside the app using

    Code (CSharp):
    1.     Application.OpenURL("FILE://" + path);
    It did not work.

    Then I was able to partially solve the problem:

    I was able to generate the .pdf file in Unity Editor and in Standalone application on OSX. I realized I had trouble on storing/loading files in Unity.

    Code (CSharp):
    1.    // this is working on Editor. Application.dataPath are reached by the application and files are stored in Assets/StreamingAssets folder under project in Editor  
    2.  
    3.           #if UNITY_EDITOR || UNITY_EDITOR_64
    4.             myDoc.createPDF(Application.dataPath + "/StreamingAssets/" + attacName);
    5.  
    6.          
    7.             myTable = null;
    8.  
    9.             #endif
    10.  
    11.     // This is working on Mac standalone application. The file is correctly saved under Application.app/Contents folder
    12.  
    13.       #if UNITY_STANDALONE
    14.          
    15.         myDoc.createPDF(Application.dataPath + "/" + attacName);
    16.         myTable = null;
    17.             #endif
    The code to retrieve and open the file:

    Code (CSharp):
    1.     public void openPdf() {
    2.         string path;
    3.  
    4.             // tested working on mac
    5.  
    6.             #if UNITY_EDITOR || UNITY_EDITOR_64
    7.  
    8.          
    9.             path = "file:" +Application.dataPath + "/StreamingAssets/" + attacName;
    10.  
    11.  
    12.             #endif
    13.  
    14.             #if UNITY_STANDALONE
    15.          
    16.             path = "file:" +Application.dataPath + "/" + attacName;
    17.          
    18.             #endif
    19.  
    20.             Application.OpenURL(path);
    21.             Debug.Log ("Button Clicked");
    22.  
    23.         }
    So far everything works perfectly.

    The pain comes with ios.

    I was able to generate a pdf file with the above code in Application.persistentDataPath and getting it back from iTunes (to verify it was created) enabling UIFileSharingEnabled under info.plist. Here's the code:



    Code (CSharp):
    1.     #if UNITY_IOS
    2.           myDoc.createPDF(Application.persistentDataPath + "/" + attacName);
    3.          myTable = null;
    4.         #endif
    The trouble is when I try to Application.OpenUrl(path). It seems that c# code is blind to Application.persistentDataPath. Which by the way is /var/mobile/Containers/Data/Application/"ApplicationName"/Documents/"pdfName".pdf using



    Code (CSharp):
    1.     path = "file://" + Application.persistentDataPath + "/" + attacName;
    2.         Application.OpenURL(path);
    I tried also to save the file in StreamingAssets folder but Xcode didn't like it:

    UnauthorizedAccessException: Access to the path "/private/var/mobile/Containers/Bundle/Application/F105683E-B2EA-4B0C-81B7-61302C58D56C/PdfReader.app/Data/newPDF.pdf" is denied.
    at Mono.Security.Cryptography.RSAManaged.GenerateKeyPair () [0x00000] in <filename unknown>:0
    at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in <filename unknown>:0
    at System.IO.FileStream..ctor (System.String path, FileMode mode) [0x00000] in <filename unknown>:0
    at sharpPDF.pdfDocument.createPDF (System.String outputFile) [0x00000] in <filename unknown>:0
    at simplepdf+<CreatePDF>c__Iterator1.MoveNext () [0x00000] in <filename unknown>:0

    (Filename: currently not available on il2cpp Line: -1)



    I then decided to get this wrapper

    https://www.assetstore.unity3d.com/en/#!/content/17591

    but it can only copy already formatted pdf file from StreamingAssets folder to /Raw folder on iOS device.

    I am thinking I have to write my own wrapper to load data from Application.persistentDataPath on iOS, have you got any suggestion to start with?

    Thank you again.
     
    Last edited: Aug 19, 2015
  2. sz-Bit-Barons

    sz-Bit-Barons

    Joined:
    Nov 12, 2013
    Posts:
    150
    there are some cool tools in the forum editor. If you would use them your post would be much more readable.


    please edit your post... I will not read it like this.
     
  3. DottorFeelgood

    DottorFeelgood

    Joined:
    Jan 5, 2015
    Posts:
    5
    Sorry about that. I've edited the post, check it out.
     
  4. sramos_eurecat

    sramos_eurecat

    Joined:
    Oct 28, 2015
    Posts:
    6
    Hi,
    I'm struggling with the same issue on IOS. Did you find a soluton?
     
unityunity