【问题标题】:Open pdf in default app using pdfmake使用 pdfmake 在默认应用中打开 pdf
【发布时间】:2017-06-07 08:11:43
【问题描述】:

我可以在我的 ionic 应用程序中创建 pdf,如果我在 chrome 中运行该应用程序,它会完美打开。但是,如果我在 android 设备上安装我的应用程序,它不会打开。下面是我的代码。如果我必须做一些额外的事情才能在设备上打开它,有人可以帮助我吗?我想用设备上的默认 pdf 应用程序打开它。

pdfMake.createPdf(dd).open();

【问题讨论】:

    标签: cordova-plugins ionic3 pdfmake


    【解决方案1】:

    好的。在将我的头撞到墙上 3 天后,我终于找到了解决方案并在这里分享,以便其他面临此问题的人可以得到帮助。我正在创建pdf并使用cordova文件插件保存它。成功保存后,我使用cordova文件打开器插件在默认应用程序中打开它。下面是我的代码。

    pdfMake.createPdf(YOUR_DEFINITION_HERE).getBlob(buffer => {
      this.file.resolveDirectoryUrl(this.file.externalRootDirectory)
       .then(dirEntry => {
          this.file.getFile(dirEntry, 'test1.pdf', { create: true })
            .then(fileEntry => {
              fileEntry.createWriter(writer => {
                writer.onwrite = () => {
                  this.fileOpener.open(fileEntry.toURL(), 'application/pdf')
                    .then(res => { })
                    .catch(err => {
                      const alert = this.alertCtrl.create({ message: 
    err.message, buttons: ['Ok'] });
                      alert.present();
                    });
                }
                writer.write(buffer);
              })
            })
            .catch(err => {
              const alert = this.alertCtrl.create({ message: err, buttons: ['Ok'] });
              alert.present();
            });
        })
        .catch(err => {
          const alert = this.alertCtrl.create({ message: err, buttons: ['Ok'] 
    });
          alert.present();
        });
    });
    

    【讨论】:

    • 谢谢。有了答案,我可以在我的 ionic2 应用程序中创建 pdf。但是是否可以在不创建文件的情况下显示 pdf。因为在我的应用程序中我可以选择预览pdf所以我不需要保存文件。我尝试根据我的要求修改您的代码,例如在创建和打开文件后我试图删除文件但我无法成功,因为它抛出错误,告诉文件不存在。你能建议我在ionic2应用程序中查看pdf而不保存它的方法吗
    • @abhilashreddy 我不确定这是否可能,但我会尝试让你知道。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-29
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    • 1970-01-01
    相关资源
    最近更新 更多