使用 pdfmake 在默认应用程序中打开 pdf

问题描述 投票:0回答:1

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

pdfMake.createPdf(dd).open();
cordova-plugins ionic3 pdfmake
1个回答
2
投票

好的。经过三天的摸索,我终于找到了解决方案并在这里分享,以便其他面临这个问题的人可以得到帮助。我正在创建 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();
        });
    });
© www.soinside.com 2019 - 2024. All rights reserved.