【问题标题】:Phonegap/Cordova: Plugin for displaying PDFs and providing function for Linkhandler (iOS / Android)Phonegap/Cordova:用于显示 PDF 并为 Linkhandler (iOS / Android) 提供功能的插件
【发布时间】:2018-05-17 11:05:08
【问题描述】:

我正在为我的 Phonegap 应用寻找一个插件,它允许我在我的应用中显示远程 PDF 文件。我发现了几个过时的问题,我想知道 2018 年是否有任何新的解决方案。

我的需求:

  • 向用户显示 PDF
  • iOS 和 Android
  • 主要是手机,还有平板电脑
  • 捏合(放大、缩小)
  • Link-Handler:我在 PDF 中有自定义 URI,我必须在应用程序中处理这些 URI。链接代表应用程序本身的导航逻辑。不 需要打开外部源/浏览器/等。
  • 我只在 PDF 中显示一个页面
  • PDF 由我自己创建

我在 Github 上找到了几个插件,但没有一个真正适合。插件要么过时且有缺陷,要么主要用于平板电脑。

我目前有一个 PDF.js 解决方案。但是,捏支持无法正常工作。

根据我的阅读,我正在考虑使用 cordova-plugin-inappbrowser,但不确定它是否适用于 iOS 和 Android。另外,我不确定链接处理程序部分。

有什么想法吗?高度赞赏。

提前致谢。

【问题讨论】:

    标签: cordova cordova-plugins phonegap-plugins phonegap-build phonegap


    【解决方案1】:

    我遇到了同样的问题,确实没有插件可以正确显示 PDF。

    所以我做了不同的事情:我在本地下载 PDF,然后让操作系统使用其默认应用程序打开 PDF。这样一来,PDF 应用程序就会接管,并且该应用程序通常包含我们可能需要的所有内容。

    为了请求打开文件,我使用了文件打开器 2 插件:https://github.com/pwlin/cordova-plugin-file-opener2

    这是我的代码:

            var me = this;
            var directory = null;
            // Request permission to store a file
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
                // The URL defined by cordova according to the operating system
                directory = fs.root.nativeURL;
    
                // Creating a temporary file in this memory area
                fs.root.getFile(fileName, {
                    create: true,
                    exclusive: false
                }, function(fileEntry) {
                    // Opening a channel to write to this file
                    fileEntry.createWriter(function(fileWriter) {
                        var sliceSize = 1024;
                        var byteCharacters = atob(me.cordovaBinaryValue);
                        var bytesLength = byteCharacters.length;
                        var slicesCount = Math.ceil(bytesLength / sliceSize);
                        var byteArrays = new Array(slicesCount);
                        for (var sliceIndex = 0; sliceIndex < slicesCount; ++sliceIndex) {
                            var begin = sliceIndex * sliceSize;
                            var end = Math.min(begin + sliceSize, bytesLength);
    
                            var bytes = new Array(end - begin);
                            for (var offset = begin, i = 0; offset < end; ++i, ++offset) {
                                bytes[i] = byteCharacters[offset].charCodeAt(0);
                            }
                            byteArrays[sliceIndex] = new Uint8Array(bytes);
                        }
    
                        // Creating our binary PDF structure object
                        var binaryData = new Blob(byteArrays, {
                            type: 'application/pdf'
                        });
    
                        // Write the content in the pdf file
                        fileWriter.write(binaryData);
    
                        fileWriter.onwriteend = function(e) {
                            // Now that the file is locally registered, you have to open it with the OS
                            cordova.plugins.fileOpener2.open(
                            directory + fileName, 'application/pdf', {
                                error: function(e) {
                                    alert('Error status: ' + e.status + ' - Error message: ' + e.message);
                                },
                                success: function() {
                                 // Normally we have nothing to do here since it is the moment when the pdf opens
                                }
                            });
                        };
    
                        // Write error in the file
                        fileWriter.onerror = function(e) {
                            alert('Write failed: ' + e.toString());
                        };
                    });
                },
                function(err) {
                    alert('error getting file! ' + err);
                });
            },
            // No writing permission
            function(err) {
                alert('error getting persistent fs! ' + err);
            });
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-03
      • 2015-08-09
      • 2012-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-20
      • 1970-01-01
      相关资源
      最近更新 更多