pdf不要使用文件插件IONIC

时间:2019-04-28 03:07:28

标签: ionic-framework ionic3 ionic-native ionic-native-media

PDF不能在Android设备上使用文件插件来创建pdf。但是能够在网上创建和下载。

全局软件包:

Cordova CLI : 7.1.0 

本地软件包:

@ionic/app-scripts : 2.1.4
Cordova Platforms  : android 6.3.0
Ionic Framework    : ionic-angular 3.6.0

packages.json

"@ionic-native/file": "^4.20.0",
"@ionic-native/file-opener": "^5.0.0"

这是下面的代码

print() {

this.createPdf();

if (this.plt.is('cordova')) {
  this.pdfObj.getBuffer(function (buffer) {

    let utf8 = new Uint8Array(buffer);
    let binaryArray = utf8.buffer;

    var filename = this.formname + new Date().toDateString();



    this.file.writeFile(this.file.documentsDirectory, filename+'.pdf', binaryArray, { replace: true }).then(fileEntry => {

      const toast = this.toastCtrl.create({
        message: 'File saved to your device',
        duration: 3000,
        position: 'top'
      });
      toast.present();



      this.fileOpener.open(this.file.documentsDirectory + filename+'.pdf', 'application/pdf');
    })
  });

} else {

  this.pdfObj.download();

}


}




 createPdf() {


for (let item of this.columns) {

  this.columnRelDataForPdf.push(this.formData[item.columnDef])

}

console.log("Inside create pdf");

console.log(this.columnDataForPdf);
console.log(this.columnRelDataForPdf);


var bodyData = [];
var dataRowHeader = [];
dataRowHeader.push(this.formName)
dataRowHeader.push('')
bodyData.push(dataRowHeader);

for (let item of this.columns) {

  var dataRow = [];

  dataRow.push(item.header);
  dataRow.push(this.formdata[item.columnDef] == undefined ? "" : this.formdata[item.columnDef]);
  bodyData.push(dataRow)

}






var docDefinition = {
  content: [
    {
      layout: 'lightHorizontalLines', 
      table: {
        headerRows: 1,
        widths: ['*', '*'],

        body: bodyData
      }
    }
  ], styles: {
    header: {
      bold: true,
      fontSize: 20,
      alignment: 'right'
    }
  }
};


    this.pdfObj = pdfMake.createPdf(docDefinition);
  }

我也尝试了externalDataDirectory和其他选项,但是创建pdf文件没有运气。请让我知道我在哪里做错了。

谢谢。

1 个答案:

答案 0 :(得分:0)

要做:

File.writeFile(File.externalRootDirectory, filename+'.pdf', binaryArray, { replace: true }).then(fileEntry =>

而不是:

this.file.writeFile(this.file.documentsDirectory, filename+'.pdf', binaryArray, { replace: true }).then(fileEntry =>