# Pdf-Make not working on Device

**URL:** <https://forum.ionicframework.com/t/pdf-make-not-working-on-device/138757>\
**Category:** ionic-v3\
**Created:** [August 17, 2018, 10:59am UTC](https://forum.ionicframework.com/t/pdf-make-not-working-on-device/138757 "2018-08-17T10:59:23Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![RobertLinde](https://avatars.discourse-cdn.com/v4/letter/r/50afbb/32.png) [@RobertLinde](https://forum.ionicframework.com/u/RobertLinde)\
**Post date:** [August 17, 2018, 10:59am UTC](https://forum.ionicframework.com/t/pdf-make-not-working-on-device/138757/1 "2018-08-17T10:59:23Z")

</div>

Hey,

currently I am adding the plugin PdfMake ([https://ionicacademy.com/create-pdf-files-ionic-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:

```auto
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

---

<div class="post-metadata">

**Author:** ![uddyvky](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/uddyvky/32/121897_2.png) [@uddyvky](https://forum.ionicframework.com/u/uddyvky)\
**Post date:** [August 17, 2018, 11:18am UTC](https://forum.ionicframework.com/t/pdf-make-not-working-on-device/138757/2 "2018-08-17T11:18:54Z")

</div>

console log the dataDirectory to know the path where the pdf is saving.

---

<div class="post-metadata">

**Author:** ![RobertLinde](https://avatars.discourse-cdn.com/v4/letter/r/50afbb/32.png) [@RobertLinde](https://forum.ionicframework.com/u/RobertLinde)\
**Post date:** [August 17, 2018, 11:34am UTC](https://forum.ionicframework.com/t/pdf-make-not-working-on-device/138757/3 "2018-08-17T11:34:52Z")

</div>

Okay, I printed the dataDirectory on a label, it is

> file://data/user/0/io.ionic.starter.files/

Using my file manager, I cant find this directory or the file. Any ideas what I should do or where I could find it??

---

<div class="post-metadata">

**Author:** ![uddyvky](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/uddyvky/32/121897_2.png) [@uddyvky](https://forum.ionicframework.com/u/uddyvky)\
**Post date:** [August 17, 2018, 11:51am UTC](https://forum.ionicframework.com/t/pdf-make-not-working-on-device/138757/4 "2018-08-17T11:51:19Z")

</div>

yes, you’ll have to change the path which is “this.file.dataDirectory” in your case…  
try the below link:  
[https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/](https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/)

---

<div class="post-metadata">

**Author:** ![RobertLinde](https://avatars.discourse-cdn.com/v4/letter/r/50afbb/32.png) [@RobertLinde](https://forum.ionicframework.com/u/RobertLinde)\
**Post date:** [August 17, 2018, 11:57am UTC](https://forum.ionicframework.com/t/pdf-make-not-working-on-device/138757/5 "2018-08-17T11:57:42Z")

</div>

So if I want to save it in the android documents folder, which directory should I use??

---

<div class="post-metadata">

**Author:** ![uddyvky](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/uddyvky/32/121897_2.png) [@uddyvky](https://forum.ionicframework.com/u/uddyvky)\
**Post date:** [August 17, 2018, 12:01pm UTC](https://forum.ionicframework.com/t/pdf-make-not-working-on-device/138757/6 "2018-08-17T12:01:56Z")

</div>

Did you read the **_“where to save files”_**??  
All the options are provided there.

---

<div class="post-metadata">

**Author:** ![RobertLinde](https://avatars.discourse-cdn.com/v4/letter/r/50afbb/32.png) [@RobertLinde](https://forum.ionicframework.com/u/RobertLinde)\
**Post date:** [August 17, 2018, 12:13pm UTC](https://forum.ionicframework.com/t/pdf-make-not-working-on-device/138757/7 "2018-08-17T12:13:28Z")

</div>

I just tried “this.file.documentsDirectory”, but it isn´t working either ☹  
Also in “New Data” I can´t find it. But not only this, it isn’t opening as well, so I guess he isn’t saving the file…

---

<div class="post-metadata">

**Author:** ![uddyvky](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/uddyvky/32/121897_2.png) [@uddyvky](https://forum.ionicframework.com/u/uddyvky)\
**Post date:** [August 17, 2018, 12:16pm UTC](https://forum.ionicframework.com/t/pdf-make-not-working-on-device/138757/8 "2018-08-17T12:16:34Z")

</div>

In that case just try “this.file.externalApplicationStorageDirectory”.

---

<div class="post-metadata">

**Author:** ![uddyvky](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/uddyvky/32/121897_2.png) [@uddyvky](https://forum.ionicframework.com/u/uddyvky)\
**Post date:** [August 17, 2018, 12:17pm UTC](https://forum.ionicframework.com/t/pdf-make-not-working-on-device/138757/9 "2018-08-17T12:17:34Z")

</div>

And are you using it on a real device or emulator??

---

<div class="post-metadata">

**Author:** ![RobertLinde](https://avatars.discourse-cdn.com/v4/letter/r/50afbb/32.png) [@RobertLinde](https://forum.ionicframework.com/u/RobertLinde)\
**Post date:** [August 17, 2018, 12:29pm UTC](https://forum.ionicframework.com/t/pdf-make-not-working-on-device/138757/10 "2018-08-17T12:29:34Z")

</div>

Okay, now I found the pdf in “file://data/user/0/io.ionic.starter.files/” by using “this.file.dataDirectory”. The problem is then, that it still isn´t opening the file after creating.

I am testing on a real device with the apk

---

<div class="post-metadata">

**Author:** ![uddyvky](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/uddyvky/32/121897_2.png) [@uddyvky](https://forum.ionicframework.com/u/uddyvky)\
**Post date:** [August 17, 2018, 1:31pm UTC](https://forum.ionicframework.com/t/pdf-make-not-working-on-device/138757/11 "2018-08-17T13:31:34Z")

</div>

refer to [this.](https://forum.ionicframework.com/t/how-to-open-local-pdf-files-from-ng-click-for-an-android-app/29192/2)

---

<div class="post-metadata">

**Author:** ![RobertLinde](https://avatars.discourse-cdn.com/v4/letter/r/50afbb/32.png) [@RobertLinde](https://forum.ionicframework.com/u/RobertLinde)\
**Post date:** [August 17, 2018, 4:38pm UTC](https://forum.ionicframework.com/t/pdf-make-not-working-on-device/138757/12 "2018-08-17T16:38:27Z")

</div>

Just found the issue. In Fileopener´s plugin.xml, line 34 has to be changed.

---

<div class="post-metadata">

**Author:** ![RobertLinde](https://avatars.discourse-cdn.com/v4/letter/r/50afbb/32.png) [@RobertLinde](https://forum.ionicframework.com/u/RobertLinde)\
**Post date:** [August 17, 2018, 4:38pm UTC](https://forum.ionicframework.com/t/pdf-make-not-working-on-device/138757/13 "2018-08-17T16:38:47Z")

</div>

Thanks a lot for ya help.

---

<div class="post-metadata">

**Author:** ![jmoraw7](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jmoraw7](https://forum.ionicframework.com/u/jmoraw7)\
**Post date:** [April 26, 2019, 5:02pm UTC](https://forum.ionicframework.com/t/pdf-make-not-working-on-device/138757/15 "2019-04-26T17:02:49Z")

</div>

Hey!  
Specifically, you have to change the FileOpener plugin.xml?  
thanks!

---

<div class="post-metadata">

**Author:** ![Offionic](https://avatars.discourse-cdn.com/v4/letter/o/7ea924/32.png) [@Offionic](https://forum.ionicframework.com/u/Offionic)\
**Post date:** [May 18, 2019, 2:35pm UTC](https://forum.ionicframework.com/t/pdf-make-not-working-on-device/138757/16 "2019-05-18T14:35:58Z")

</div>

Olá,

Como você resolveu isso, estou com meu script e só consigo salvar e por sua vez imprimir no navegador, mais se tento utilizar pelo ionic DevApp ou se gero o APK, ele não funciona.

Segue meu home.ts

letterObj = {  
to: ‘’,  
from: ‘’,  
text: ‘’  
}

pdfObj = null;

constructor(public navCtrl: NavController, private plt: Platform, private file: File, private fileOpener: FileOpener) { }

createPdf() {  
var docDefinition = {  
content: [  
{ text: ‘REMINDER’, style: ‘header’ },  
{ text: new Date().toTimeString(), alignment: ‘right’ },

```
    { text: 'From', style: 'subheader' },
    { text: this.letterObj.from },

    { text: 'To', style: 'subheader' },
    this.letterObj.to,

    { text: this.letterObj.text, style: 'story', margin: [0, 20, 0, 20] },

    {
      ul: [
        'Bacon',
        'Rips',
        'BBQ',
      ]
    }
  ],
  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);

```

}

downloadPdf() {  
if (this.plt.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, 'myletter.pdf', blob, { replace: true }).then(fileEntry => {
      // Open the PDf with the correct OS tools
      this.fileOpener.open(this.file.dataDirectory + 'myletter.pdf', 'application/pdf');
    })
  });
} else {
  // On a browser simply use download!
  this.pdfObj.download();
}

```

}

}

---

<div class="post-metadata">

**Author:** ![sukantavap](https://avatars.discourse-cdn.com/v4/letter/s/858c86/32.png) [@sukantavap](https://forum.ionicframework.com/u/sukantavap)\
**Post date:** [May 21, 2019, 7:54am UTC](https://forum.ionicframework.com/t/pdf-make-not-working-on-device/138757/17 "2019-05-21T07:54:17Z")

</div>

Which kind of change you are done In Fileopener´s plugin.xml, line 34.  
Please share in details

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [July 19, 2021, 9:23pm UTC](https://forum.ionicframework.com/t/pdf-make-not-working-on-device/138757/18 "2021-07-19T21:23:04Z")

</div>

Closed on July 19, 2021, 9:23pm UTC


