Search code examples
javascriptangularjsionic-frameworkionic2jspdf

Share a PDF in ionic app


I want to share a pdf from my ionic app that I have created using jsPDF. How can I use it along with SocialSharing plug-in?

doc.save("resume.pdf") 

This directly downloads the pdf and take me to the screen where I can't do anything.

 //share(message, subject, file, url)
 this.socialSharing.share("Test", null, doc.save("resume.pdf"), null); 

this also won't work. I am new to ionic and angular.

Update: I studied and followed this article which talks about making a pdf with pdfmake in the ionic app, but I still can't figure out how to share the same.


Solution

  • import { File } from '@ionic-native/file';
    
    var blob = doc.output('blob', {type: 'application/pdf'});
    let pdfUrl = {pdfUrl: URL.createObjectURL(blob)};
    
    if (this.file.checkFile(this.file.cacheDirectory, "myresume.pdf")) 
    {
     this.file.writeExistingFile(this.file.cacheDirectory, "myresume.pdf",blob)
             .then(() => console.log('overwrite done'))
             .catch(err => console.log(err +' old copies not removed'));
    } else{
        this.file.writeFile(this.file.cacheDirectory, "myresume.pdf", blob, true)
            .then(_ => console.log('write successful'))
            .catch(err => console.log(err+ " write failed"));
    }
    

    For sharing it,

    import { File } from '@ionic-native/file';
    import { SocialSharing } from '@ionic-native/social-sharing';
    export class ResumeView {
           pdfUrl : String;
           constructor(public navCtrl: NavController, 
                        public navParams: NavParams,
                        private socialSharing: SocialSharing,
                        private file: File
                       ) 
           {
               this.pdfUrl = this.navParams.get('pdfUrl'); 
           }
           regularShare(){
               this.socialSharing.share("test", null, this.file.cacheDirectory + filename, null)
            }