[SOLVED] “Corrupted report PDF generated using JavaScript” in Dynamics 365 V9.0
Introduction
We had developed a functionality for Dynamics CRM v8.2 on Quote Entity by adding a custom button and on clicking a button we run a report, capture its contents using JavaScript, convert it to a PDF and attach the PDF to an Email Record.
Below shown is the code snippet
quoteInvoice = { runReportToPrint: function () { debugger; var params = quoteInvoice.getReportingSession(); var newPth = Xrm.Page.context.getClientUrl() + "/Reserved.ReportViewerWebControl.axd?ReportSession=" + params[0] + "&Culture=1033&CultureOverrides=True&UICulture=1033&UICultureOverrides=True&ReportStack=1&ControlID=" + params[1] + "&OpType=Export&FileName=public&ContentDisposition=OnlyHtmlInline&Format=PDF"; //Calling the below function converts the report to PDF format. quoteInvoice.convertResponseToPDF(newPth); }, getReportingSession: function () { var selectedIds = Xrm.Page.data.entity.getId(); selectedIds = selectedIds.replace('{', '').replace('}', ''); var strParameterXML = "<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'><entity name= 'quote'><all-attributes/><filter type='and'><condition attribute='quoteid' value='" + selectedIds + "' operator='eq' /></filter></entity ></fetch >"; var reportGuid = "DAF05843-CA33-E711-811E-FC15B42827EC"; var reportName = "Quote Invoice.rdl"; var pth = Xrm.Page.context.getClientUrl() + "/CRMReports/rsviewer/QuirksReportViewer.aspx"; var retrieveEntityReq = new XMLHttpRequest(); retrieveEntityReq.open("POST", pth, false); retrieveEntityReq.setRequestHeader("Accept", "*/*"); retrieveEntityReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); retrieveEntityReq.send("id=%7B" + reportGuid + "%7D&uniquename=" + Xrm.Page.context.getOrgUniqueName() + "&iscustomreport=true&reportnameonsrs=&reportName=" + reportName + "&isScheduledReport=false&p:quoteid=" + strParameterXML); var x = retrieveEntityReq.responseText.lastIndexOf("ReportSession="); var y = retrieveEntityReq.responseText.lastIndexOf("ControlID="); var ret = new Array(); ret[0] = retrieveEntityReq.responseText.substr(x + 14, 24); ret[1] = retrieveEntityReq.responseText.substr(x + 10, 32); return ret; } }
The runReportToPrint() function is called when the custom button is clicked
Issue and Error Details
Once the environment was upgraded to V9.0 we were facing issues as the PDF that was generated was corrupted as show below.
While debugging the code we got the following message.
On further research we found that the URL has been changed and below shown is the new path that has to be used which works correctly on V9.0.
var pth = Xrm.Page.context.getClientUrl() + "/CRMReports/rsviewer/ReportViewer.aspx";
Once this change was done we did not get any error and the PDF of the report that was generated opened correctly with all of its contents.