web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

WhatsApp business Cloud API integration to send PDF

(0) ShareShare
ReportReport
Posted on by 5

I have done the WhatsApp Business API integration(graph.facebook.com) with d65fo to send pdf through URL, But it is send .bin extension file instead of .pdf extension.

WhatsApp-Image-2022_2D00_12_2D00_25-at-11.15.19-AM.jpeg

I am getting the URL from file stream below is the controller class code. how I can get .pdf extension?

public static void main(Args _args)
{
ZTSalesInvoiceControllerWhatsapp controller = new ZTSalesInvoiceControllerWhatsapp();
controller.parmReportName(ssrsReportStr(ZTSalesInvoiceZCopy,Report));

ZTProformaContractWhatsapp contract = new ZTProformaContractWhatsapp();
SRSPrintDestinationSettings settings;
container binData;
Binary binaryData;
Filename fileName = 'invoice.pdf';
str ret;
System.Byte[] reportBytes = new System.Byte[0]();
SRSProxy srsProxy;
SRSReportRunService srsReportRunService = new SrsReportRunService();
Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[] parameterValueArray;
Map reportParametersMap;
SRSReportExecutionInfo executionInfo = new SRSReportExecutionInfo();
contract.parmSalesId('SO-645459');
controller.parmLoadFromSysLastValue(false);
controller.parmReportContract().parmRdpContract(contract);
settings = controller.parmReportContract().parmPrintSettings();
settings.printMediumType(SRSPrintMediumType::File);
settings.fileName(fileName);
settings.fileFormat(SRSReportFileFormat::PDF);
settings.parmSendToPrinterAsPdf(true);
settings.pack();

// Below is a part of code responsible for rendering the report
//contract = this.parmReportContract().parmRdpContract() as ZTSalesInvoiceControllerWhatsapp;

controller.parmReportContract().parmReportServerConfig(SRSConfiguration::getDefaultServerConfiguration());

controller.parmReportContract().parmReportExecutionInfo(executionInfo);

srsReportRunService.getReportDataContract(controller.parmreportcontract().parmReportName());

srsReportRunService.preRunReport(controller.parmreportcontract());

reportParametersMap = srsReportRunService.createParamMapFromContract(controller.parmReportContract());

parameterValueArray = SrsReportRunUtil::getParameterValueArray(reportParametersMap);

srsProxy = SRSProxy::constructWithConfiguration(controller.parmReportContract().parmReportServerConfig());

// Actual rendering to byte array
reportBytes = srsproxy.renderReportToByteArray
(controller.parmreportcontract().parmreportpath(),
parameterValueArray,
settings.fileFormat(SRSReportFileFormat::PDF),
settings.deviceinfo());



System.IO.Stream mstream = new System.IO.MemoryStream(reportBytes);

str link = File::SendFileToTempStore(mstream, fileName);

str destinationUrl = 'https://graph.facebook.com/v15.0/{}/messages';
str requestJson,responseJson;
System.Net.HttpWebRequest request;
System.Net.HttpWebResponse response = new System.Net.HttpWebResponse();
CLRObject clrObj;
System.Byte[] bytes;
System.Text.Encoding utf8;
System.IO.Stream requestStream, responseStream;
System.IO.StreamReader streamReader;
System.Exception ex;
System.Net.WebHeaderCollection httpHeader;
System.Net.ServicePoint servicePt;
str byteStr;
System.Byte[] byteArray;
str num = '9665';

str jsonStr = "{\"to\":\""   num   "\",\"messaging_product\":\"whatsapp\", \"type\":\"template\",\"template\":{ \"components\":[{ \"type\":\"header\",\"parameters\":[{ \"type\":\"document\", \"document\":{";
jsonStr = jsonStr   "\"link\":\""  link  "\"}}]}, {\"type\":\"body\"}],\"language\":{ \"policy\":\"deterministic\",\"code\":\"AR\"},\"name\":\"invoice_arabic\" \}}";

try
{
new InteropPermission(InteropKind::ClrInterop).assert();
httpHeader = new System.Net.WebHeaderCollection();
request = System.Net.WebRequest::Create(destinationUrl);
utf8 = System.Text.Encoding::get_UTF8();
bytes = utf8.GetBytes(jsonStr);
request.set_Method("POST");
str token = " ";
httpHeader.add(@'Authorization: Bearer ' token);
request.set_Headers(httpHeader);
request.ContentType = 'application/json';
request.set_ContentLength(bytes.get_Length());
requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.get_Length());
response = request.GetResponse();
responseStream = response.GetResponseStream();
streamReader = new System.IO.StreamReader(responseStream);
responseJson = streamReader.ReadToEnd();
info(responseJson);

}

catch (Exception::CLRError)

{
ex = CLRInterop::getLastException().GetBaseException();
error(ex.get_Message());
}

requestStream.Close();
streamReader.Close();
responseStream.Close();
response.Close();

controller.parmArgs(_args);
controller.parmShowDialog(False);
controller.startOperation();
}

I have the same question (0)
  • Martin Dráb Profile Picture
    239,022 Most Valuable Professional on at

    Your code is very difficult to read. First of all, let me add line indentation. It seems that you lost it somehow when pasting code here and than didn't notice that the result was wrong.

    public static void main(Args _args)
    {
    	ZTSalesInvoiceControllerWhatsapp controller = new ZTSalesInvoiceControllerWhatsapp();
    	controller.parmReportName(ssrsReportStr(ZTSalesInvoiceZCopy,Report));
    
    	ZTProformaContractWhatsapp contract = new ZTProformaContractWhatsapp();
    	SRSPrintDestinationSettings settings;
    	container binData;
    	Binary binaryData;
    	Filename fileName = 'invoice.pdf';
    	str ret;
    	System.Byte[] reportBytes = new System.Byte[0]();
    	SRSProxy srsProxy;
    	SRSReportRunService srsReportRunService = new SrsReportRunService();
    	Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[] parameterValueArray;
    	Map reportParametersMap;
    	SRSReportExecutionInfo executionInfo = new SRSReportExecutionInfo();
    	contract.parmSalesId('SO-645459');
    	controller.parmLoadFromSysLastValue(false);
    	controller.parmReportContract().parmRdpContract(contract);
    	settings = controller.parmReportContract().parmPrintSettings();
    	settings.printMediumType(SRSPrintMediumType::File);
    	settings.fileName(fileName);
    	settings.fileFormat(SRSReportFileFormat::PDF);
    	settings.parmSendToPrinterAsPdf(true);
    	settings.pack();
    
    	// Below is a part of code responsible for rendering the report
    	//contract = this.parmReportContract().parmRdpContract() as ZTSalesInvoiceControllerWhatsapp;
    	controller.parmReportContract().parmReportServerConfig(SRSConfiguration::getDefaultServerConfiguration());
    	controller.parmReportContract().parmReportExecutionInfo(executionInfo);
    
    	srsReportRunService.getReportDataContract(controller.parmreportcontract().parmReportName());
    	srsReportRunService.preRunReport(controller.parmreportcontract());
    
    	reportParametersMap = srsReportRunService.createParamMapFromContract(controller.parmReportContract());
    	parameterValueArray = SrsReportRunUtil::getParameterValueArray(reportParametersMap);
    
    	srsProxy = SRSProxy::constructWithConfiguration(controller.parmReportContract().parmReportServerConfig());
    
    	// Actual rendering to byte array
    	reportBytes = srsproxy.renderReportToByteArray(
    		controller.parmreportcontract().parmreportpath(),
    		parameterValueArray,
    		settings.fileFormat(SRSReportFileFormat::PDF),
    		settings.deviceinfo());
    
    	System.IO.Stream mstream = new System.IO.MemoryStream(reportBytes);
    
    	str link = File::SendFileToTempStore(mstream, fileName);
    
    	str destinationUrl = 'https://graph.facebook.com/v15.0/{}/messages';
    	str requestJson,responseJson;
    	System.Net.HttpWebRequest request;
    	System.Net.HttpWebResponse response = new System.Net.HttpWebResponse();
    	CLRObject clrObj;
    	System.Byte[] bytes;
    	System.Text.Encoding utf8;
    	System.IO.Stream requestStream, responseStream;
    	System.IO.StreamReader streamReader;
    	System.Exception ex;
    	System.Net.WebHeaderCollection httpHeader;
    	System.Net.ServicePoint servicePt;
    	str byteStr;
    	System.Byte[] byteArray;
    	str num = '9665';
    
    	str jsonStr = "{\"to\":\""   num   "\",\"messaging_product\":\"whatsapp\", \"type\":\"template\",\"template\":{ \"components\":[{ \"type\":\"header\",\"parameters\":[{ \"type\":\"document\", \"document\":{";
    	jsonStr = jsonStr   "\"link\":\""  link  "\"}}]}, {\"type\":\"body\"}],\"language\":{ \"policy\":\"deterministic\",\"code\":\"AR\"},\"name\":\"invoice_arabic\" \}}";
    
    	try
    	{
    		new InteropPermission(InteropKind::ClrInterop).assert();
    		httpHeader = new System.Net.WebHeaderCollection();
    		request = System.Net.WebRequest::Create(destinationUrl);
    		utf8 = System.Text.Encoding::get_UTF8();
    		bytes = utf8.GetBytes(jsonStr);
    		request.set_Method("POST");
    		str token = " ";
    		httpHeader.add(@'Authorization: Bearer ' token);
    		request.set_Headers(httpHeader);
    		request.ContentType = 'application/json';
    		request.set_ContentLength(bytes.get_Length());
    		requestStream = request.GetRequestStream();
    		requestStream.Write(bytes, 0, bytes.get_Length());
    		response = request.GetResponse();
    		responseStream = response.GetResponseStream();
    		streamReader = new System.IO.StreamReader(responseStream);
    		responseJson = streamReader.ReadToEnd();
    		info(responseJson);
    	}
    	catch (Exception::CLRError)
    	{
    		ex = CLRInterop::getLastException().GetBaseException();
    		error(ex.get_Message());
    	}
    
    	requestStream.Close();
    	streamReader.Close();
    	responseStream.Close();
    	response.Close();
    
    	controller.parmArgs(_args);
    	controller.parmShowDialog(False);
    	controller.startOperation();
    }

    There are still many things to improve. For example, you have declared many variables that you don't use at all, therefore they just pollute your code and makes it harder to follow.

    Where do you think that you specify the file name? I don't see any such a thing in the message. That you don't provide any file name at all would explain why the resulting file is called "null.bin". Just providing the link doesn't have to be sufficient; you may need to provide the file name too.

    gBy the way, this questions seems to be about WhatsApp API and not F&O, therefore this isn't really the right forum.

  • Abrar1551 Profile Picture
    5 on at

    I am passing the file link to API json body it sending the file but as .bin when I manually change .bin to .pdf it gives correct file.

    str link = File::SendFileToTempStore(mstream, fileName);

    str jsonStr = "{\"to\":\"" + num + "\",\"messaging_product\":\"whatsapp\", \"type\":\"template\",\"template\":{ \"components\":[{ \"type\":\"header\",\"parameters\":[{ \"type\":\"document\", \"document\":{";

    jsonStr = jsonStr + "\"link\":\"" +link+ "\"}}]}, {\"type\":\"body\"}],\"language\":{ \"policy\":\"deterministic\",\"code\":\"AR\"},\"name\":\"invoice_arabic\" \}}";

  • Martin Dráb Profile Picture
    239,022 Most Valuable Professional on at

    A link maybe something like https://myservice.net/file/12345. It's wrong to assume that it'll always contain a file name and that the WhatsApp API will try to extract it from there. The result you got proves that it's indeed not the case. You'll need provide a file name, not just a link to the file contents.

  • Abrar1551 Profile Picture
    5 on at

    can we get the direct link to pdf in controller class?

    e.ghttps://drive.google.com/uc?export=download&id=1ki7iqR7WYoJKMRLcE6nAjjwHbz8W8nW3

  • Martin Dráb Profile Picture
    239,022 Most Valuable Professional on at

    I'm sorry, but I don't understand your last question.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 617

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 461 Super User 2026 Season 1

#3
Syed Haris Shah Profile Picture

Syed Haris Shah 298 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans