Luiz Ricardo Cardoso
Luiz Ricardo Cardoso

Reputation: 1624

Ionic SocialSharing with pdf file for whatsapp

I am using pdfMaker to create an object of type Document, I would like to send this file to a contact I have saved on Whatsapp, it would be something similar to using the shareViaWhatsAppToReceiver function using SocialSharing.

The file's attachment would already come in the message.

   this.socialSharing.shareViaWhatsAppToReceiver(`+55 ${contact.phone}`, `PDF`,
      null, this.pdfObj).then(async () => {
        const toast = await this._toast.create({
          message: 'Send success.',
          duration: 3000
        });
      });

The way I am doing above does not work, I would like to send the document directly as a parameter so that the message on Whatsapp is attached.

Upvotes: 0

Views: 682

Answers (1)

Hiten Pingolia
Hiten Pingolia

Reputation: 11

const serverPdfUrl = filePath; //'https://example.com/path-to-your-pdf.pdf';  // URL of the PDF file on your server
const fileName = 'my_file_pdf.pdf'; // Name to save the file as on the device
const localPath = this.file.externalDataDirectory + fileName; // Path to save the file on the device

// Create a file transfer object
const transfer: FileTransferObject = this.fileTransfer.create();

// Download the PDF file
transfer.download(serverPdfUrl, localPath).then(
  (entry) => {
    alert('File downloaded successfully:'+ JSON.stringify(entry) );

    // After downloading, share the PDF file via WhatsApp
    this.socialSharing.share('title', 'message', localPath, 'whatsapp')
      .then(() => {
        alert('PDF file shared successfully!');
      })
      .catch((error) => {
        alert('Error sharing PDF file:'+ JSON.stringify(error) );
      });
  },
  (error) => {
    alert('Error downloading PDF file:'+ JSON.stringify(error));
  }
);

Upvotes: 0

Related Questions