0

I want to convert my rdl report to pdf using javascript. It will be really good if I can only use OpenReport() and convert the pdf file there because then I have to convert it into ppt. I am using CRM online.

here is my code.

 function OpenReport() {
                debugger;
                var formType = Xrm.Page.ui.getFormType();
                if(formType != 1){
                Xrm.Page.ui.setFormNotification("Please wait while system is generating pdf...", "INFO", "pdfGen");
                var rdlName = "KalorikBrandPresentationv4.rdl"; //Replace with your report name
                var entityType = "10060"; //Replace
                var entityGuid = Xrm.Page.data.entity.getId();
                var recordId = entityGuid.replace("{","").replace("}","");
                var reportGuid = "ea721b27-44a6-ea11-9688-005056ba540f"; //Replace with your report guid
                var url = Xrm.Page.context.getClientUrl() + "/crmreports/viewer/viewer.aspx?action=run&context=records&helpID=" + rdlName + "&id=%7b" + reportGuid + "%7d&records=%7b" + recordId + "%7d&recordstype=" + entityType;


            var responseSession = getReportingSession(reportGuid,rdlName,recordId);
            convertResponseToPDF(responseSession,rdlName);
        }


function convertResponseToPDF(responseSession,reportName) {
        debugger;
        var pth = encodeURI(Xrm.Page.context.getClientUrl() + "/Reserved.ReportViewerWebControl.axd? 
        ReportSession=" + responseSession[0]+"&Culture=1033&CultureOverrides=True&UICulture=1033&UICultureOverrides=True&ReportStack=1&ControlID=" + responseSession[1] +"&OpType=Export&FileName=Public&ContentDisposition=OnlyHtmlInline&Format=PDF");
        var base64PDFString;
        var retrieveEntityReq = new XMLHttpRequest();
        retrieveEntityReq.open("GET", pth, true);
        retrieveEntityReq.setRequestHeader("Accept", "*/*");
        retrieveEntityReq.responseType = "arraybuffer";
        retrieveEntityReq.onreadystatechange = function () {
            if (retrieveEntityReq.readyState == 4 && retrieveEntityReq.status == 200) {
                var binary = "";
                var bytes = new Uint8Array(this.response);
    
                for (var i = 0; i < bytes.byteLength; i++) {
                    binary += String.fromCharCode(bytes[i]);
                }
                base64PDFString = btoa(binary);
                console.log(base64PDFString);
                createNotesAttachment(base64PDFString,reportName);
            }
        };
        
       
        retrieveEntityReq.send();
    }
James Z
  • 11,838
  • 10
  • 25
  • 41

1 Answers1

0

Did you followed the code/steps mentioned here

I remembers I worked on 2016 online for downloading report as PDF. This shall help.

You might want to change code a bit.

function runReportToPrint() {

    var invoicenumber = Xrm.Page.getAttribute("invoicenumber").getValue()
        if (invoicenumber != null) {
            invoicenumber = invoicenumber.substring(4, 9);
            var params = 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=" + invoicenumber + "&ContentDisposition=OnlyHtmlInline&Format=PDF";
            window.open(newPth, "_self");
            encodePdf(params);
        } else {
            alert("Invoice ID is Missing");
        }
}

function getReportingSession() {
    var recordId = Xrm.Page.data.entity.getId();
    recordId = recordId.replace('{', '').replace('}', '');

    var strParameterXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'><entity name='invoice'><all-attributes /><filter type='and'><condition attribute='invoiceid' operator='eq' value='" + recordId + "' /> </filter></entity></fetch>";
    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:CRM_invoice=" + 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;
}

function encodePdf(responseSession) {

    var retrieveEntityReq = new XMLHttpRequest();
    var pth = Xrm.Page.context.getClientUrl() + "/Reserved.ReportViewerWebControl.axd?ReportSession=" + responseSession[0] + "&Culture=1033&CultureOverrides=True&UICulture=1033&UICultureOverrides=True&ReportStack=1&ControlID=" + responseSession[1] + "&OpType=Export&FileName=Public&ContentDisposition=OnlyHtmlInline&Format=PDF";
    retrieveEntityReq.open("GET", pth, true);
    retrieveEntityReq.setRequestHeader("Accept", "*/*");
    retrieveEntityReq.responseType = "arraybuffer";
    retrieveEntityReq.onreadystatechange = function () {

        if (retrieveEntityReq.readyState == 4 && retrieveEntityReq.status == 200) {
            var binary = "";
            var bytes = new Uint8Array(this.response);

            for (var i = 0; i < bytes.byteLength; i++) {
                binary += String.fromCharCode(bytes[i]);
            }
            var bdy = btoa(binary);
            createNote(bdy);
        }
    };

    retrieveEntityReq.send();
}

function createNote(data) {

    var note = {};

    var recordId = Xrm.Page.data.entity.getId();
    recordId = recordId.replace('{', '').replace('}', '');

    var invoicenumber = Xrm.Page.getAttribute("invoicenumber").getValue()
        invoicenumber = invoicenumber.substring(4, 9);

    var refInvoice = new Object();
    refInvoice.LogicalName = "invoice";
    refInvoice.Id = recordId;

    note.ObjectId = refInvoice;
    note.ObjectTypeCode = refInvoice.LogicalName;

    note.Subject = "Invoice: " + invoicenumber;
    note.MimeType = "application/pdf";
    note.DocumentBody = data;
    note.FileName = invoicenumber + ".pdf";

    XrmServiceToolkit.Rest.Create(
        note,
        "AnnotationSet",
        function (result) {
        //Alert user
        alert("Note Created");
        //Refresh data so user sees newly created note
        Xrm.Page.data.refresh(false);

    },
        function (error) {
        alert(error.message);
    },
        true);
}
AnkUser
  • 4,351
  • 2
  • 7
  • 19