2017-07-15 65 views
1

我想在我的離子應用程序中輸出pdf。 在我的.ts文件,我有這樣的功能:pdfmake不使用離子2在移動設備上下載PDF

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

這兩條線在這裏:

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

僅適用於瀏覽器,但無法在手機上。

現在,我該如何使用pdfmake和cordova-file-plugin以及cordova-file-transfer-plugin來下載pdf文件?

我試圖搜索,但沒有明確的解決方案。

如果您知道解決方案,請幫助。非常感謝你!!!

+0

你見過這個答案嗎? https://stackoverflow.com/questions/36130527/save-this-pdf-in-the-cache-local-storage-on-ionic – misha130

+0

我試過但沒有發生 – JSmith

回答

0

請嘗試下面的代碼。它適用於科爾多瓦和非科爾多瓦的環境。

你可以通過下面的鏈接瞭解詳情:

代碼

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); 
} 
相關問題