Hey,
currently I am adding the plugin PdfMake (https://ionicacademy.com/create-pdf-files-ionic-pdfmake/) to my app.
So far, it is working perfectly in my Browser (ionic serve -c) (it´s creating and downloading the pdf), but in an APK it isn´t working.
I don´t know whether it creates the pdf or not, but sure is that it isnt saving it or at least displaying. So if I trigger the pdf-generate and pdf-download function nothing happens.
I am building the app as “ionic cordova build --prod android”…
It isn´t working in the Ionic DevApp as well, but here due to console.log I can see that both functions are triggered.
This is my code:
import { NavController, NavParams, Platform } from 'ionic-angular';
import pdfMake from 'pdfmake/build/pdfmake';
import pdfFonts from 'pdfmake/build/vfs_fonts';
pdfMake.vfs = pdfFonts.pdfMake.vfs;
import { File } from '@ionic-native/file';
import { FileOpener } from '@ionic-native/file-opener';
...other imports...
export class pdfPage{
pdfObj = null;
constructor(private platform: Platform, public navCtrl: NavController, public navParams: NavParams, private file: File, private fileOpener: FileOpener) {
}
obj:any;
content = ["test1", "test2", "test3", "test4"];
createPdf() {
console.log("CreatePdf() triggered")
var docDefinition = {
content: {
margin: [15, 5, 15, 15],
table: {
widths: [ '*', '*', '*', '*' ],
body: this.content
}
},
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);
this.downloadPdf();
}
downloadPdf() {
console.log("DownloadPdf() triggered")
if (this.platform.is('cordova')) {
this.pdfObj.getBuffer((buffer) => {
var blob = new Blob([buffer], { type: 'application/pdf' });
// Save the PDF to the data Directory of our App
this.file.writeFile(this.file.dataDirectory, 'test.pdf', blob, { replace: true }).then(fileEntry => {
// Open the PDf with the correct OS tools
this.fileOpener.open(this.file.dataDirectory + 'test.pdf', 'application/pdf');
})
});
} else {
// On a browser simply use download!
this.pdfObj.download();
}
}
}
So with this code I expect it to create the pdf, save it and then open it automatically.
So this is the shortend code of the page, if you need to see more, just tell me.
I guess the code for the creation is correct but not the one for saving it on a device, correct me if I am wrong.
I would love to hear how I have to correct my code to get it working.
Greeings