New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

where does pdf get saved on the device when we call download #1030

Closed
prabodh25 opened this Issue Jun 3, 2017 · 8 comments

Comments

Projects
None yet
6 participants
@prabodh25
Copy link

prabodh25 commented Jun 3, 2017

Hi,

First of all let me say that its a great library for creating pdf. Thanks a lot!

I am creating pdf in my Ionic 3 - angular 4 app and want to open it in default application after creating. Could you please tell me where does file get saved on the device and how to get its path. I am creating file like this.
pdfMake.createPdf(dd).download('mypdf.pdf')

@ghost

This comment has been minimized.

Copy link

ghost commented Jun 5, 2017

@prabodh25 - You need to implement cordova file plugin and file opener plugin to make this work on device with Ionic 2/3/x

@prabodh25

This comment has been minimized.

Copy link

prabodh25 commented Jun 5, 2017

@sunilkconsultant - Thanks for reply. Cordova file plugin expects path for the file to be opened. My question is how to get the dowloaded file path.

@prabodh25

This comment has been minimized.

Copy link

prabodh25 commented Jun 5, 2017

I checked the code for download method and there is callback function we can pass as argument to the method but it does not return anything. It would be good idea to return complete file path to callback function.

@prabodh25

This comment has been minimized.

Copy link

prabodh25 commented Jun 7, 2017

Ok. After banging my head on wall for 3 days I finally found the solution and sharing here so that other people who are facing this issue can get help. I am creating pdf and saving it using cordova file plugin. After successful save I am opening it in default application using cordova file opener plugin. Below is my code.

    pdfMake.createPdf(YOUR_DEFINITION_HERE).getBlob(buffer => {
      this.file.resolveDirectoryUrl(this.file.externalRootDirectory)
        .then(dirEntry => {
          this.file.getFile(dirEntry, 'test1.pdf', { create: true })
            .then(fileEntry => {
              fileEntry.createWriter(writer => {
                writer.onwrite = () => {
                  this.fileOpener.open(fileEntry.toURL(), 'application/pdf')
                    .then(res => { })
                    .catch(err => {
                      const alert = this.alertCtrl.create({ message: err.message, buttons: ['Ok'] });
                      alert.present();
                    });
                }
                writer.write(buffer);
              })
            })
            .catch(err => {
              const alert = this.alertCtrl.create({ message: err, buttons: ['Ok'] });
              alert.present();
            });
        })
        .catch(err => {
          const alert = this.alertCtrl.create({ message: err, buttons: ['Ok'] });
          alert.present();
        });

    });

@liborm85 liborm85 closed this Jun 14, 2017

@rendy14s

This comment has been minimized.

Copy link

rendy14s commented Jul 10, 2017

hai @prabodh25 can you help me ? i get this error :

Cannot read property 'resolveDirectoryUrl' of undefined

thank you

@dtmp

This comment has been minimized.

Copy link

dtmp commented May 11, 2018

@rendy14s change this.file.externalRootDirectory -> this.file.documentsDirectory

https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/

@LiXiaoLongsgithub

This comment has been minimized.

Copy link

LiXiaoLongsgithub commented May 18, 2018

when i print or open PDF( pdfMake.createPdf(context).print(); ), i get this error ,:
pdfmake.js:12775 Uncaught TypeError: Cannot create property 'href' on string 'about:blank'
at pdfmake.js:12775
at pdfmake.js:12841
at pdfmake.js:12850
at PDFDocument. (pdfmake.js:12723)
at PDFDocument.EventEmitter.emit (pdfmake.js:3341)
at endReadableNT (pdfmake.js:9603)
at afterTickTwo (pdfmake.js:3599)
at Item.run (pdfmake.js:2356)
at drainQueue (pdfmake.js:2326)
how to solve the problem ? thank you!!!

@Ananyag

This comment has been minimized.

Copy link

Ananyag commented Jul 13, 2018

hi, @prabodh25
can you help me for this
i am using pdfmake for my html need to export as PDF for 1 page means for small data it is working fine.

but for large data the pdf pages will split into two and they dont return any content . its just returns empty pdf pages

Can you please help me to sort out this @liborm85.

my code: vm.exportPDF = function () {
html2canvas(document.getElementById('exportthis'), {
onrendered: function (canvas) {
var data = canvas.toDataURL();
var docDefinition = {
content: [{
image: data,
width:500,
}],
pageBreakBefore: function (currentNode, followingNodesOnPage, nodesOnNextPage, previousNodesOnPage) {
return currentNode.headlineLevel === 1 && followingNodesOnPage.length === 0;
}
};
pdfMake.createPdf(docDefinition).download("BCQAM-Summary-report.pdf");
}
});
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment