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

Cordova plugins 使用pdfmake在默认应用程序中打开pdf,cordova-plugins,ionic3,pdfmake,Cordova Plugins,Ionic3,Pdfmake,我可以在我的ionic应用程序中创建pdf,如果我在chrome中运行该应用程序,它会完美地打开。但是,如果我在android设备上安装我的应用程序,它不会打开。下面是我的代码。如果我需要做一些额外的事情才能在设备上打开它,请有人帮助我。我想在设备上用默认的pdf应用程序打开它 pdfMake.createPdf(dd).open(); 嗯。在我的头撞到墙上三天后,我终于找到了解决方案,并在这里分享,以便其他面临这个问题的人可以得到帮助。我正在创建pdf并使用cordova文件插件保存它。成功

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

pdfMake.createPdf(dd).open();

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

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();
    });
});

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

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而不保存it@abhilashreddy我不确定这是否可行,但我会尝试让你知道。谢谢。有了答案,我可以在我的ionic2应用程序中创建一个pdf。但是,是否可以在不显示pdf的情况下显示pdf创建文件。因为在我的应用程序中,我有一个预览pdf的选项,所以我不需要保存该文件。我尝试根据我的要求修改您的代码,例如在创建和打开文件后,我尝试删除该文件,但未能成功,因为它抛出错误,表明该文件不存在。您能建议我一种方法来删除该文件吗在ionic2应用程序中查看pdf而不保存it@abhilashreddy我不确定这是否可行,但我会尽力让你知道。