简体   繁体   English

pdfmake无法使用ionic 2在手机上下载pdf

[英]pdfmake not downloading pdf on mobile using ionic 2

I want to have pdf export in my ionic app. 我想在我的离子应用程序中导出pdf。 In my .ts file, I have this function: 在我的.ts文件中,我具有以下功能:

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? 现在,如何使用pdfmake和cordova-file-plugin和cordova-file-transfer-plugin下载pdf文件?

I tried to search, but there is no definite solution. 我尝试搜索,但是没有确定的解决方案。

Please help if you know the solution. 如果您知道解决方案,请提供帮助。 Thank you very much!!! 非常感谢你!!!

Try the following code. 请尝试以下代码。 It works for both cordova and non-cordova environment. 它适用于Cordova和非Cordova环境。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM