2

我试图弄清楚如何在 Unity 应用程序中生成和显示 pdf。我希望该应用程序和 pdf 可用于 iOS、Android 和网络应用程序。按照本教程,我能够生成一个 pdf 文件并存储在模拟器的应用程序主文件夹中。

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

不幸的是,当我尝试使用

Application.OpenURL("FILE://" + path);

这没用。

我还尝试了 iTextSharp,但结果很差,我相信它不再是免费的了。

任何建议都会受到欢迎。

谢谢

更新

所以我试图继续前进,但我有点卡在中间:我尝试了 Gallorini 修改后的 SharpPDF 程序,生成的 pdf 文档,我称之为“NewPDF.pdf”存储在主应用程序文件夹中。事实上,当我在编辑器中运行代码时,我能够在其中找到它。我构建了一个简单的 gui 按钮,并在其上附加了一个 void,运行此 c# 代码以将文档显示为您的建议:

public void openPdf(){
string path = Application.persistentDataPath + "/" + "newPDF.pdf";
Application.OpenURL(path);
Debug.log("Button Clicked");

}

当我单击按钮时,除了控制台记录按钮单击之外,什么也没有发生。

当我为网络播放器构建程序时,虽然 ApplicationOpenURL 工作并且我能够显示编辑器以前生成的 pdf,但没有生成 pdf ......我有点困惑。

当我为 Mac Osx 构建程序时,什么都没有发生。

在 iPhone 上,我从 Xcode 控制台获取此日志:

-> applicationDidBecomeActive()
Requesting Resolution: 1920x1080
Init: screen size 1920x1080
Initializing Metal device caps
Initialize engine version: 5.1.2f1 (afd2369b692a)

Adding Image Length 0 HEIGTH :8 WIDTH :8
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
sharpPDF.c__Iterator0:MoveNext()

(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)

UnauthorizedAccessException: Access to the path "/newPDF.pdf" is denied.
at Mono.Security.Cryptography.RSAManaged.GenerateKeyPair () [0x00000] in :0
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in :0
at System.IO.FileStream..ctor (System.String path, FileMode mode) [0x00000] in :0
at sharpPDF.pdfDocument.createPDF (System.String outputFile) [0x00000] in :0
at SimplePDF+c__Iterator1.MoveNext () [0x00000] in :0

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

Adding Image Length 0 HEIGTH :8 WIDTH :8
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
sharpPDF.c__Iterator0:MoveNext()

(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)

UnauthorizedAccessException: Access to the path "/newPDF.pdf" is denied.
at Mono.Security.Cryptography.RSAManaged.GenerateKeyPair () [0x00000] in :0
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in :0
at System.IO.FileStream..ctor (System.String path, FileMode mode) [0x00000] in :0
at sharpPDF.pdfDocument.createPDF (System.String outputFile) [0x00000] in :0
at SimplePDF+c__Iterator1.MoveNext () [0x00000] in :0

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

Button Clicked
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
SimplePDF:openPdf()
UnityEngine.Events.UnityAction:Invoke()
UnityEngine.Events.InvokableCall:Invoke(Object[])
UnityEngine.Events.InvokableCallList:Invoke(Object[])
UnityEngine.Events.UnityEventBase:Invoke(Object[])
UnityEngine.Events.UnityEvent:Invoke()
UnityEngine.UI.Button:Press()
UnityEngine.UI.Button:OnPointerClick(PointerEventData)
UnityEngine.EventSystems.ExecuteEvents:Execute(IPointerClickHandler, BaseEventData)
UnityEngine.EventSystems.EventFunction`1:Invoke(ICancelHandler, BaseEventData)
UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1)
UnityEngine.EventSystems.TouchInputModule:ProcessTouchPress(PointerEventData, Boolean, Boolean)
UnityEngine.EventSystems.TouchInputModule:ProcessTouchEvents()
UnityEngine.EventSystems.TouchInputModule:Process()
UnityEngine.EventSystems.EventSystem:Update()

(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)

我完全糊涂了。你能帮帮我吗?

再次感谢。

更新#2

我部分解决了这个问题:

我能够在 Unity Editor 和 OSX 上的独立应用程序中生成 .pdf 文件。我意识到我在 Unity 中存储/加载文件时遇到了麻烦。

// this is working on Editor. Application.dataPath are reached by the application and files are stored in Assets/StreamingAssets folder under project in Editor     

    #if UNITY_EDITOR || UNITY_EDITOR_64 
        myDoc.createPDF(Application.dataPath + "/StreamingAssets/" + attacName);


        myTable = null;

        #endif

// This is working on Mac standalone application. The file is correctly saved under Application.app/Contents folder

  #if UNITY_STANDALONE

    myDoc.createPDF(Application.dataPath + "/" + attacName);
    myTable = null;
        #endif

检索和打开文件的代码:

public void openPdf() {
    string path;

        // tested working on mac

        #if UNITY_EDITOR || UNITY_EDITOR_64


        path = "file:" +Application.dataPath + "/StreamingAssets/" + attacName;


        #endif

        #if UNITY_STANDALONE

         path = "file:" +Application.dataPath + "/" + attacName;

        #endif

        Application.OpenURL(path);
        Debug.Log ("Button Clicked");

    }

到目前为止,一切正常。

痛苦来自于ios。

我能够在 Application.persistentDataPath 中使用上述代码生成一个 pdf 文件,并从 iTunes 中取回它(以验证它是否已创建),从而在 info.plist 下启用 UIFileSharingEnabled。这是代码:

   #if UNITY_IOS 
      myDoc.createPDF(Application.persistentDataPath + "/" + attacName);
      myTable = null;
    #endif

问题是当我尝试 Application.OpenUrl(path) 时。似乎 c# 代码对 Application.persistentDataPath 视而不见。顺便说一下 /var/mobile/Containers/Data/Application/"ApplicationName"/Documents/"pdfName".pdf 使用

path = "file://" + Application.persistentDataPath + "/" + attacName;
    Application.OpenURL(path);

我也尝试将文件保存在 StreamingAssets 文件夹中,但 Xcode 不喜欢它:

UnauthorizedAccessException:对路径“/private/var/mobile/Containers/Bundle/Application/F105683E-B2EA-4B0C-81B7-61302C58D56C/PdfReader.app/Data/newPDF.pdf”的访问被拒绝。在 Mono.Security.Cryptography.RSAManaged.GenerateKeyPair () [0x00000] in :0 在 System.IO.FileStream..ctor (System.String 路径、FileMode 模式、FileAccess 访问、FileShare 共享、Int32 bufferSize、布尔匿名、FileOptions 选项) [0x00000] in :0 at System.IO.FileStream..ctor (System.String path, FileMode mode) [0x00000] in :0 at sharpPDF.pdfDocument.createPDF (System.String outputFile) [0x00000] in :0 at simplepdf+c__Iterator1.MoveNext () [0x00000] in :0

(文件名:目前在 il2cpp 线路上不可用:-1)

然后我决定买这个包装

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

但它只能将已格式化的 pdf 文件从 StreamingAssets 文件夹复制到 iOS 设备上的 /Raw 文件夹。

我在想我必须编写自己的包装器来从 iOS 上的 Application.persistentDataPath 加载数据,你有什么建议可以开始吗?

再次感谢你。

4

0 回答 0