whatsapp-web.js
whatsapp-web.js copied to clipboard
[Feature] Sending a PDF with a custom caption
Is there an existing issue for this?
- [X] I have searched the existing issues
Describe the bug
When sending a PDF in Base64 or by URL, a caption is sent with the name of the file

Expected behavior
Send the pdf with the name and the optional caption separately
with name only:
With only caption:
Steps to Reproduce the Bug or Issue
The pdf is sent by code through Base64 and the caption separately:
Relevant Code
No response
Browser Type
Chromium
WhatsApp Account Type
Standard
Does your WhatsApp account have multidevice enabled?
Yes, I am using Multi Device
Environment
OS: Mac Phone: Android - IOS whatsapp-web.js version 1.18.3-alpha.0
Additional context
No response
The caption feature is still new and has not been rolled out to everyone fully, when it has I am sure it will be fixed. I am also experiencing the bug
use options { sendMediaAsDocument: true }
thats enough!
@RC047 Yes, but what I want is for the same file name not to be sent as caption
If want same file name in the caption, you can try this: const media = MessageMedia.fromFilePath(fattach); //fattach is full file path await client.sendMessage(number, media, {caption: captionMedia});
The bug is not in the MessageMedia.... but in the sendMessage, since the name is being copied with the caption
Hi Everyone,
I found one location where I can change the filename who send .pdf file:
Maybe anybody can found the issue and fixes this.
On MessageMedia.js
/**
* Creates a MessageMedia instance from a local file path
* @param {string} filePath
* @returns {MessageMedia}
*/
static fromFilePath(filePath) {
const b64data = fs.readFileSync(filePath, {encoding: 'base64'});
const mimetype = mime.getType(filePath);
var filename = path.basename(filePath);
filename = filename.replace('.pdf', ''); //here
return new MessageMedia(mimetype, b64data, filename);
}
isn't my solution, but partialy have helped me at the moment.
See you
Hello @pondwar I already modified that part and it is not from there
Hey guys, I am also facing the same problem.
I haven't found the exact origin of bug in the code. However, after some debugging, I found out that overriding caption in src/util/Injected.js/sendMessage
function like this is fixing the issue:
let attOptions = {};
if (options.attachment) {
attOptions = options.sendMediaAsSticker
? await window.WWebJS.processStickerData(options.attachment)
: await window.WWebJS.processMediaData(options.attachment, {
forceVoice: options.sendAudioAsVoice,
forceDocument: options.sendMediaAsDocument,
forceGif: options.sendVideoAsGif
});
attOptions.caption = options.caption; // Here
content = options.sendMediaAsSticker ? undefined : attOptions.preview;
delete options.attachment;
delete options.sendMediaAsSticker;
}
Hello @pondwar I already modified that part and it is not from there
Hi, unfortunatly its not the solution, but I just raname the .pdf file to message I wanna send and works partialy.
But its not the solution just workaround.
Hey guys, I am also facing the same problem.
I haven't found the exact origin of bug in the code. However, after some debugging, I found out that overriding caption in
src/util/Injected.js/sendMessage
function like this is fixing the issue:let attOptions = {}; if (options.attachment) { attOptions = options.sendMediaAsSticker ? await window.WWebJS.processStickerData(options.attachment) : await window.WWebJS.processMediaData(options.attachment, { forceVoice: options.sendAudioAsVoice, forceDocument: options.sendMediaAsDocument, forceGif: options.sendVideoAsGif }); attOptions.caption = options.caption; // Here content = options.sendMediaAsSticker ? undefined : attOptions.preview; delete options.attachment; delete options.sendMediaAsSticker; }
We'll provide a PR when it is stable.
Thanks @PurpShell , yes it worked
Thanks @PurpShell , yes it worked
PDFs with captions are not stable yet in whatsapp
Thanks @PurpShell , yes it worked
PDFs with captions are not stable yet in whatsapp
It could be added to a new version to update to my project
I am having the same issue, waiting for the fix Thanks @PurpShell for your suggestion, will try it
Thanks @PurpShell , yes it worked
How to send like that? give me example code pls
@pedroslopez Could you add this modification to the repository?
I'm also interested in uploading this
Hi, is there any update on this issue? It looks like document caption is officially released a week ago
Can we put this update out now? It gives a lot of problems and it's a simple solution @pedroslopez
@pedroslopez amazing, good! thanks
I am trying to send a PDF file with some text as caption and custom filename as demonstrated in the example provided by @KJoaquin
I created the MessageMedia
object as follows:
const media = new MessageMedia('application/pdf', string_base64, caption_text);
where the string_base64 is the content of the PDF encoded in base64 format and caption_text is the text I want to put along the file as caption..
I did console.log(media)
and found out that filename for the media is undefined.
The following workaround did the job for me:
media.filename = 'attachment.pdf'; // this is the name I want my PDF to be
And then sending the message as follows:
await client.sendMessage(chatId, media, { caption: caption_text, attachment: media, sendMediaAsDocument: true });
I hope it helps.
I am trying to send a PDF file with some text as caption and custom filename as demonstrated in the example provided by @KJoaquin I created the
MessageMedia
object as follows:const media = new MessageMedia('application/pdf', string_base64, caption_text);
where the string_base64 is the content of the PDF encoded in base64 format and caption_text is the text I want to put along the file as caption..
I did
console.log(media)
and found out that filename for the media is undefined.The following workaround did the job for me:
media.filename = 'attachment.pdf'; // this is the name I want my PDF to be
And then sending the message as follows:
await client.sendMessage(chatId, media, { caption: caption_text, attachment: media, sendMediaAsDocument: true });
I hope it helps.
Work for me