How to send a PDF to multiple contacts/groups using WhatsApp API

I made a video explaining how to do Whatsapp automation using Javascript. Someone has asked this question about how to send a PDF to multiple contacts or groups using whatsapp-web.js in the comments of that video. Let’s find out!

send pdf using whatsapp API

Here’s the example source code to send a PDF to multiple contacts or groups using WhatsApp API

Important: If you abuse this API and send bulk messages to bulk contacts as spam then you might get banned by WhatsApp so be careful when you send any kind of bulk messages.

If you run this code then a PDF named “sample.pdf” from the project folder will be sent to the list of chats defined in the myChats array. Modify the myChats array and PDF attachment name and path as per your requirement.

const { Client, LocalAuth, MessageMedia } = require("whatsapp-web.js");
const qrcode = require("qrcode-terminal");

const myChats = ["Contact1", "Contact2", "Group1"]; // Add a list of contact names or group names here

const client = new Client({
  authStrategy: new LocalAuth(),
});

client.on("qr", (qr) => {
  qrcode.generate(qr, { small: true });
});

client.on("ready", () => {
  console.log("Client is ready!");
  client.getChats().then((chats) => {
    myChats.forEach((chatName) => {
      const myChat = chats.find((chat) => chat.name === chatName);
      if(myChat){
        const attachmentPdf = MessageMedia.fromFilePath("./sample.pdf");
        client.sendMessage(myChat.id._serialized, attachmentPdf); 
      }
      else {
        console.log(`Chat ${chatName} not found`);
      }
    });
  });
});

client.initialize();

Ranjith kumar

9 Replies to “How to send a PDF to multiple contacts/groups using WhatsApp API”

  1. i tried the code by modifying contacts and pdf but getting error index.js:53 refrence error chant not defined

    1. I don’t know what’s in line number 53 of your code. The code in the example is only 30 lines of code.

      1. please find attached code
        this time it run but again some more errors popup whose screen shot is attached
        can you please guide what wrong i would ne doing ?

        // https://codingislove.com/send-pdf-multiple-contacts-groups-whatsapp-api/

        
        const { Client, LocalAuth, MessageMedia } = require("whatsapp-web.js");
        const qrcode = require("qrcode-terminal");
         
        const myChats = ["ICE SHIVKAILASH KUVARBHAN GARG", "+91 9822610809"]; // Add a list of contact names or group names here
         
        const client = new Client({
          authStrategy: new LocalAuth(),
        });
         
        client.on("qr", (qr) => {
          qrcode.generate(qr, { small: true });
        });
         
        client.on("ready", () => {
          console.log("Client is ready!");
          client.getChats().then((chats) => {
            myChats.forEach((chatName) => {
              const myChat = chats.find((chat) => chat.name === chatName);
              if(myChat){
                const attachmentPdf = MessageMedia.fromFilePath("./san.pdf");
                client.sendMessage(myChat.id._serialized, attachmentPdf); 
              }
              else {
                console.log(Chat ${chatName} not found);
              }
            });
          });
        });
         
        client.initialize();
        
        
        
        1. I don’t see the screenshot of the errors here but I’m guessing that the names given in myChats array are not matching with the name on your whatsapp. I see that you’ve given all capital letters but the name on WhatsApp doesn’t seem to have all capital letters.

  2. Awesome tutorial! im having a problem, the person who receives the file cant open, just open in some devices, the error inside their whatsapp app says “‘You may not have a proper app for viewing this content

    1. Hi Paul, If you are sending a PDF then check if the receiver has any pdf reader installed on his phone. If he doesn’t have any pdf reader installed then it is showing the right error and it’ll go away if he installs any pdf reader. The same goes for any other files as well. For ex: if you are sending a word document then the received should have a word document reader like Microsoft word or google docs installed. This is unrelated to the library and just general tech.

  3. Hola! Podrias pasarme un ejemplo con el codigo completo? No logro hacerlo funcionar me da errores. Es posible adjuntar un archivo que esta en la unidad c:\ejemplo.pdf ? Gracias!!!

Leave a Reply

Your email address will not be published. Required fields are marked *