cordova-pluginsionic3pdfmake

Open pdf in default app using pdfmake


I am able to create pdf in my ionic app and if I run the app in chrome it opens perfectly. However if I install my app on the android device it doesn't open. Below is my code. Can someone please help me if I have to do something extra to open it on device. I want to open it with default pdf application on device.

pdfMake.createPdf(dd).open();

Solution

  • Ok. After banging my head on wall for 3 days I finally found the solution and sharing here so that other people who are facing this issue can get help. I am creating pdf and saving it using cordova file plugin. After successful save I am opening it in default application using cordova file opener plugin. Below is my code.

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