bpampuch / pdfmake

Client/server side PDF printing in pure JavaScript

Home Page:http://pdfmake.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

prabodh25 opened this issue · comments

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')

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

@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.

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.

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

    });

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

Cannot read property 'resolveDirectoryUrl' of undefined

thank you

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

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

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!!!

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

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