1

I want to have pdf export in my ionic app. In my .ts file, I have this function:

exportPDF() {
      var docDefinition = {
        content: [
                {
                  layout: 'lightHorizontalLines', // optional
                  table: {
                    // headers are automatically repeated if the table spans over multiple pages
                    // you can declare how many rows should be treated as headers
                    headerRows: 1,
                    widths: [ '*', 'auto', 100, '*', 'auto' ],

                    body: [
                      [ 'Title 1', 'Title 2', 'Title 3', 'Title 4', 'Title 5' ],
                      [ 'Value 1', 'Value 2', 'Value 3', 'Value 4', 'Value 5' ],
                      [ 'Value 1', 'Value 2', 'Value 3', 'Value 4', 'Value 5' ],
                    ]
                  }
                }
              ]
      }
      pdfMake.createPdf(docDefinition).open();
      pdfMake.createPdf(dd).download();

            pdfMake.createPdf(docDefinition).getBuffer((buffer) => {
                var utf8 = new Uint8Array(buffer); // Convert to UTF-8...
                var binaryArray = utf8.buffer; // Convert to Binary...
                this.file.writeFile(this.file.dataDirectory, "example.pdf", binaryArray, true).then((success) => {
                        alert(success.toURL());
                    }, (error) => {
                        alert(error);
                });
            });
}

this two lines here :

  pdfMake.createPdf(docDefinition).open();
  pdfMake.createPdf(dd).download();

works only on browser but not on mobile.

Now, how can I download pdf file using pdfmake and cordova-file-plugin and cordova-file-transfer-plugin?

I tried to search, but there is no definite solution.

Please help if you know the solution. Thank you very much!!!

Codeblooded Saiyan
  • 1,457
  • 4
  • 28
  • 54

1 Answers1

0

Try the following code. It works for both cordova and non-cordova environment.

You can go through following link for more details :

Code

var docDefinition = ''; //your doc definition

if (!window.cordova)
  pdfMake.createPdf(docDefinition).open();

else {
    pdfMake.createPdf(docDefinition).getBuffer(function (buffer) {
        var utf8 = new Uint8Array(buffer);
        binaryArray = utf8.buffer;

        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
    });
}

function fail(error) {
    console.log(error.code);
};

function gotFS(fileSystem) {
    var fileName = "filename.pdf";

    fileSystem.root.getFile(fileName, {
        create: true,
        exclusive: false
    }, gotFileEntry, fail);
}
Akshay Soam
  • 1,580
  • 3
  • 21
  • 39