Преобразование rdl в PDF с помощью javascript

#javascript #dynamics-crm #crm #xrm

#javascript #dynamics-crm #crm #xrm

Вопрос:

Я хочу преобразовать свой отчет rdl в PDF с помощью javascript. Будет действительно хорошо, если я смогу использовать только OpenReport() и конвертировать PDF-файл туда, потому что тогда мне придется конвертировать его в ppt. Я использую CRM онлайн.

вот мой код.

  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=runamp;context=recordsamp;helpID="   rdlName   "amp;id={"   reportGuid   "}amp;records={"   recordId   "}amp;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] "amp;Culture=1033amp;CultureOverrides=Trueamp;UICulture=1033amp;UICultureOverrides=Trueamp;ReportStack=1amp;ControlID="   responseSession[1]  "amp;OpType=Exportamp;FileName=Publicamp;ContentDisposition=OnlyHtmlInlineamp;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 amp;amp; 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();
    }
  

Ответ №1:

Вы следовали коду / шагам, упомянутым здесь

Я помню, что работал над 2016 онлайн для загрузки отчета в формате PDF. Это должно помочь.

Возможно, вам захочется немного изменить код.

 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]   "amp;Culture=1033amp;CultureOverrides=Trueamp;UICulture=1033amp;UICultureOverrides=Trueamp;ReportStack=1amp;ControlID="   params[1]   "amp;OpType=Exportamp;FileName="   invoicenumber   "amp;ContentDisposition=OnlyHtmlInlineamp;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={"   reportGuid   "}amp;uniquename="   Xrm.Page.context.getOrgUniqueName()   "amp;iscustomreport=trueamp;reportnameonsrs=amp;reportName="   reportName   "amp;isScheduledReport=falseamp;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]   "amp;Culture=1033amp;CultureOverrides=Trueamp;UICulture=1033amp;UICultureOverrides=Trueamp;ReportStack=1amp;ControlID="   responseSession[1]   "amp;OpType=Exportamp;FileName=Publicamp;ContentDisposition=OnlyHtmlInlineamp;Format=PDF";
    retrieveEntityReq.open("GET", pth, true);
    retrieveEntityReq.setRequestHeader("Accept", "*/*");
    retrieveEntityReq.responseType = "arraybuffer";
    retrieveEntityReq.onreadystatechange = function () {

        if (retrieveEntityReq.readyState == 4 amp;amp; 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);
}