pdf不要使用文件插件IONIC创建pdf

问题描述 投票:0回答:1

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文件没有运气。请让我知道我在哪里做错了。

谢谢。

ionic-framework ionic3 ionic-native ionic-native-media
1个回答
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 =>
© www.soinside.com 2019 - 2024. All rights reserved.