Do you know how to save and open a pdf using Capacitor and Ionic 4?

I can generate a pdf (using pdfmake), save and open it on an android emulator fine with Cordova. There are a lot of examples of how to do this.
What I can’t seem to do is do this using Capacitor.

Any examples or suggestions would be massively appreciated :slight_smile:

See my attempt here:

1 Like

Hi @wekas
I am stuck on the same question from a couple of days… Have you solved this issue?
If yes, can I ask you how or (even better) to share a code snippet?

Hi @filippo-ots,

Yes, The code is in the link above. I am using it on a project at the moment so I will copy in here again.
Here is the link:

downloadPdf(pdfBase64: string) {
    const { Filesystem } = Plugins;

    if (this.plt.is('cordova')) {
        // Save the PDF to the device
        const fileName = 'timesheet.pdf';
        try {
          Filesystem.writeFile({
            path: fileName,
            data: pdfBase64,
            directory: FilesystemDirectory.Documents
            // encoding: FilesystemEncoding.UTF8
          }).then((writeFileResult) => {
            Filesystem.getUri({
                directory: FilesystemDirectory.Documents,
                path: fileName
            }).then((getUriResult) => {
                const path = getUriResult.uri;
                this.fileOpener.open(path, 'application/pdf')
                .then(() => console.log('File is opened'))
                .catch(error => console.log('Error openening file', error));
            }, (error) => {
                console.log(error);
            });
          });
        } catch (error) {
          console.error('Unable to write file', error);
        }
      } else {
      // On a browser simply use download
      this.pdfObj.download();
    }
  }
1 Like