PDF make not working . Why?


#1

Friends,

I used a working code from a project in my project with same code for creating a custom PDF file using code

 createPdf(epayTransID,status,ReceiptDate,TaxPeriod,TotAmount,BankRefNo) {
    var docDefinition = {
      content: [
        { text: 'Payment Receipt', style: 'header' },
        { text: new Date().toTimeString(), alignment: 'right' },
 
        { text: 'Transaction ID', style: 'subheader' },
        { text: epayTransID },
 
        { text: 'Status', style: 'subheader' },
        { text: status },


        { text: 'Receipt Date', style: 'subheader'},
        { text: ReceiptDate },

        { text: 'Tax Period', style: 'subheader'},
        { text: TaxPeriod },

        { text: 'Total Amount', style: 'subheader'},
        { text: TotAmount},

        { text: 'Bank Reference no', style: 'subheader'},
        { text: BankRefNo},
        
      ],
      styles: {
        header: {
          fontSize: 18,
          bold: true,
        },
        subheader: {
          fontSize: 14,
          bold: true,
          margin: [0, 15, 0, 0]
        },
        story: {
          italic: true,
          alignment: 'center',
          width: '50%',
        }
      }
    }
    this.pdfObj = pdfMake.createPdf(docDefinition).getBlob(buffer => {
      console.log("step 1 : " + this.file.externalRootDirectory); 
      this.file.resolveDirectoryUrl(this.file.externalRootDirectory)
        .then(dirEntry => {
          console.log("step 2 : " + JSON.stringify(dirEntry));  
          this.file.getFile(dirEntry, 'paymentreceipt.pdf', { create: true })
            .then(fileEntry => {
              console.log("step 3 : " + JSON.stringify(fileEntry));  
              console.log("step 3.1 : " + JSON.stringify(fileEntry.toURL()));
              
              fileEntry.createWriter(writer => {
                console.log("dsd"+JSON.stringify(writer));
                console.log("step 3.2 : Enter within create writer");
               
                writer.onwritestart = function() { console.log('onwritestart'); };

                writer.onabort = function() { console.log('onabort'); };
                writer.write(buffer);
                console.log("buffer is after writing: "+JSON.stringify(buffer));
                writer.onwrite = () => {
                  console.log("step 4 : " + fileEntry.toURL());
                  this.fileOpener.open(fileEntry.toURL(), 'application/pdf')
                    .then(res => { console.log("step 5 :  completed : " + res); })
                    .catch(err => {
                      console.log("step 6 : error : " + err);

                    });
                }
               writer.write(buffer);
                console.log("step 7 : writing : " + JSON.stringify(buffer));
                console.log("step 7.1 : "+fileEntry.toURL());
              }, function(error){
                console.log("Error occurred while writing to file. Error code is: " + error.code+ ":error: "+error);
            })
            })
            .catch(err => {
              console.log("step1 catch: "+err);
            });
        })
        .catch(err => {
          console.log("step2 catch: "+err);
        });

    });
  }

Here got empty value for object buffer …

Also no PDF file created .

it’s console output of above function is

step 1 : file:///storage/emulated/0/

step 2 : {"isFile":false,"isDirectory":true,"name":"","fullPath":"/","filesystem":"<FileSystem: sdcard>","nativeURL":"file:///storage/emulated/0/"}

step 3 : {"isFile":true,"isDirectory":false,"name":"paymentreceipt.pdf","fullPath":"/paymentreceipt.pdf","filesystem":"<FileSystem: sdcard>","nativeURL":"file:///storage/emulated/0/paymentreceipt.pdf"}

step 3.1 : "file:///storage/emulated/0/paymentreceipt.pdf"
 dsd{"fileName":"","length":0,"localURL":"cdvfile://localhost/sdcard/paymentreceipt.pdf","position":0,"readyState":0,"result":null,"error":null,"onwritestart":null,"onprogress":null,"onwrite":null,"onwriteend":null,"onabort":null,"onerror":null}

step 3.2 : Enter within create writer

buffer is after writing: {}

step 7 : writing : {}

step 7.1 : file:///storage/emulated/0/paymentreceipt.pdf

The function not going to Step 4 and Step 5 .

Please advise how this file write operation can done successfully…

Thanks

Anes


#2

Dear Hari,

I have another Sample project with same code on that after the step

step 7.1 : file:///storage/emulated/0/paymentreceipt.pdf

two more steps also came in console (means it’s working)

that order of code is

Step 4 and Step 5 .

In our current case that two steps skipped . That’s my problem …

I suspect there is a broken file plugin issue as mentioned here : https://github.com/ionic-team/ionic/issues/5638

My used plugin list is

```com.telerik.plugins.nativepagetransitions 0.6.5 "Native Page Transitions"
cordova-plugin-apprate 1.3.0 "AppRate"
cordova-plugin-browsertab 0.2.0 "cordova-plugin-browsertab"
cordova-plugin-compat 1.2.0 "Compat"
cordova-plugin-console 1.1.0 "Console"
cordova-plugin-device 2.0.1 "Device"
cordova-plugin-dialogs 1.3.4 "Notification"
cordova-plugin-dns 1.0.2 "DNS"
cordova-plugin-file 6.0.1 "File"
cordova-plugin-file-opener2 2.0.19 "File Opener2"
cordova-plugin-file-transfer 1.7.1 "File Transfer"
cordova-plugin-globalization 1.0.9 "Globalization"
cordova-plugin-google-analytics 1.8.3 "Google Universal Analytics Plugin"
cordova-plugin-inappbrowser 1.7.2 "InAppBrowser"
cordova-plugin-market 1.2.0 "Market"
cordova-plugin-network-information 1.3.4 "Network Information"
cordova-plugin-sim 1.3.3 "SIM"
cordova-plugin-splashscreen 4.1.0 "Splashscreen"
cordova-plugin-statusbar 2.4.1 "StatusBar"
cordova-plugin-whitelist 1.3.1 "Whitelist"
cordova-plugin-x-socialsharing 5.2.1 "SocialSharing"
cordova-plugin-x-toast 2.6.0 "Toast"
cordova-sqlite-storage 2.2.0 "Cordova sqlite storage plugin"
cordova.plugins.diagnostic 4.0.3 "Diagnostic"
es6-promise-plugin 4.1.0 "Promise"
ionic-plugin-keyboard 2.2.1 "Keyboard"```


Please advise

Thanks

Anes


#3

Hi @anespa ,
if you still have the issue
Can you create a github repo with a sample project that replicates your issue ?
so i can use it to try and solve your issue .