Skip to content
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 · 9 comments
Closed

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

prabodh25 opened this issue Jun 3, 2017 · 9 comments

Comments

@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
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
Copy link
Author

@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
Copy link
Author

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
Copy link
Author

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

    });

@rendy14s
Copy link

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

Cannot read property 'resolveDirectoryUrl' of undefined

thank you

@dtmp
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
Copy link

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

@jeslove
Copy link

jeslove commented Jul 16, 2021

When i call pdfMaker.createPdf(documentDefinition) i get error: is not assignable to parameter of type 'TDocumentDefinitions'. Types of property 'content' are incompatible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants